From 4a29049ce707a0086403de9ce683ef8c6a5743b7 Mon Sep 17 00:00:00 2001 From: Brendan Golden Date: Sat, 5 Aug 2023 21:51:09 +0100 Subject: [PATCH] feat: new function to get data from wolves (csv/json) and store it in teh db) --- Cargo.toml | 2 + src/bin/update_wolves.rs | 99 ++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 48 ++++++------------- 3 files changed, 115 insertions(+), 34 deletions(-) create mode 100644 src/bin/update_wolves.rs diff --git a/Cargo.toml b/Cargo.toml index 6eaea8c..08cf5f1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,8 @@ version = "0.1.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[[bin]] +name = "update_wolves" [[bin]] name = "update_groups" diff --git a/src/bin/update_wolves.rs b/src/bin/update_wolves.rs new file mode 100644 index 0000000..968659a --- /dev/null +++ b/src/bin/update_wolves.rs @@ -0,0 +1,99 @@ +use ldap3::{LdapConn, Scope, SearchEntry}; +use skynet_ldap_backend::{db_init, get_config, Accounts, Config}; +use sqlx::{Pool, Sqlite}; +use tide::prelude::{Serialize, Deserialize}; + +#[async_std::main] +async fn main() -> tide::Result<()> { + let config = get_config(); + let db = db_init(&config).await.unwrap(); + + let mut records = vec![]; + + if let Ok(accounts) = get_csv(&config){ + for account in accounts { + records.push(AccountWolves::from(account)); + } + } + + for account in records { + update_account(&db, &account).await; + } + + Ok(()) +} + +#[derive(Debug, Deserialize, Serialize, sqlx::FromRow)] +pub struct AccountWolves { + pub id_wolves: String, + pub id_student: String, + pub email: String, + pub expiry: String, + pub name_first: String, + pub name_second: String, +} + +#[derive(Debug, serde::Deserialize)] +struct RecordCSV { + #[serde(rename = "MemID")] + mem_id: String, + #[serde(rename = "Student Num")] + id_student: String, + #[serde(rename = "Contact Email")] + email: String, + #[serde(rename = "Expiry")] + expiry: String, + #[serde(rename = "First Name")] + name_first: String, + #[serde(rename = "Last Name")] + name_second: String, +} +impl From for AccountWolves { + fn from(input: RecordCSV) -> Self { + AccountWolves{ + id_wolves: input.mem_id, + id_student: input.id_student, + email: input.email, + expiry: input.expiry, + name_first: input.name_first, + name_second: input.name_second, + } + } +} + +fn get_csv(config: &Config) -> Result, Box> { + let mut records: Vec = 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: RecordCSV = result?; + if record.mem_id.is_empty() { + continue; + } + records.push(record); + } + } + + Ok(records) +} + + +async fn update_account(db: &Pool, account: &AccountWolves){ + sqlx::query_as::<_, AccountWolves>( + " + INSERT OR REPLACE INTO accounts_wolves (id_wolves, id_student, email, expiry, name_first, name_second) + VALUES (?1, ?2, ?3, ?4, ?5, ?6) + ", + ) + .bind(&account.id_wolves) + .bind(&account.id_student) + .bind(&account.email) + .bind(&account.expiry) + .bind(&account.name_first) + .bind(&account.name_second) + .fetch_optional(db) + .await + .ok(); +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 0d6eb3f..ce287ae 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -44,6 +44,20 @@ pub async fn db_init(config: &Config) -> Result, 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, 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, Box> { - let mut records: Vec = 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()