feat: new function to get data from wolves (csv/json) and store it in teh db)

This commit is contained in:
silver 2023-08-05 21:51:09 +01:00
parent 6b901683da
commit 4a29049ce7
3 changed files with 115 additions and 34 deletions

View file

@ -44,6 +44,20 @@ pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
.connect_with(SqliteConnectOptions::from_str(&format!("sqlite://{}", database))?.create_if_missing(true))
.await?;
sqlx::query(
"CREATE TABLE IF NOT EXISTS accounts_wolves (
id_wolves text primary key,
id_student text not null,
email text not null,
expiry text not null,
name_first text not null,
name_surname integer not null
)",
)
.execute(&pool)
.await?;
sqlx::query(
"CREATE TABLE IF NOT EXISTS accounts_new (
mail text primary key,
@ -246,40 +260,6 @@ async fn update_accounts(pool: &Pool<Sqlite>, config: &Config) {
ldap.unbind().unwrap();
}
#[derive(Debug, serde::Deserialize)]
pub struct Record {
#[serde(rename = "MemID")]
pub mem_id: String,
#[serde(rename = "Student Num")]
pub id_student: String,
#[serde(rename = "Contact Email")]
pub email: String,
#[serde(rename = "Expiry")]
pub expiry: String,
#[serde(rename = "First Name")]
pub name_first: String,
#[serde(rename = "Last Name")]
pub name_second: String,
}
pub fn read_csv(config: &Config) -> Result<Vec<Record>, Box<dyn std::error::Error>> {
let mut records: Vec<Record> = vec![];
if let Ok(mut rdr) = csv::Reader::from_path(format!("{}/{}", &config.home, &config.csv)) {
for result in rdr.deserialize() {
// Notice that we need to provide a type hint for automatic
// deserialization.
let record: Record = result?;
if record.mem_id.is_empty() {
continue;
}
records.push(record);
}
}
Ok(records)
}
// from https://rust-lang-nursery.github.io/rust-cookbook/algorithms/randomness.html#create-random-passwords-from-a-set-of-alphanumeric-characters
pub fn random_string(len: usize) -> String {
thread_rng().sample_iter(&Alphanumeric).take(len).map(char::from).collect()