#3 wolves api #10

Merged
silver merged 12 commits from #3-wolves-api into main 2023-09-16 19:10:07 +00:00
3 changed files with 132 additions and 6 deletions
Showing only changes of commit 3cf298f137 - Show all commits

4
.gitignore vendored
View file

@ -7,3 +7,7 @@ result
/result
*.db
tmp/
tmp.*
*.csv

View file

@ -12,13 +12,103 @@ async fn main() {
Err(_) => return,
};
// handle wolves api here
get_wolves_csv(&db, &config).await;
// handle wolves api here
get_wolves(&db).await;
// get from skynet for the compsoc server only
get_skynet(&db, &config).await;
}
async fn get_wolves_csv(db: &Pool<Sqlite>, config: &Config) {
if let Ok(accounts) = get_csv(config) {
for account in accounts {
add_users_wolves_csv(db, &config.skynet_server, &account).await;
}
}
}
#[derive(Debug, serde::Deserialize)]
struct RecordCSV {
#[serde(rename = "MemID")]
mem_id: String,
#[serde(rename = "Contact Email")]
email: String,
#[serde(rename = "Expiry")]
expiry: String,
}
impl From<RecordCSV> for Accounts {
fn from(input: RecordCSV) -> Self {
Self {
server: Default::default(),
id_wolves: "".to_string(),
id_member: input.mem_id,
email: input.email,
expiry: input.expiry,
discord: None,
minecraft: None,
}
}
}
fn get_csv(config: &Config) -> Result<Vec<Accounts>, Box<dyn std::error::Error>> {
let mut records: Vec<Accounts> = vec![];
let csv = format!("{}/{}", &config.home, &config.csv);
if let Ok(mut rdr) = csv::Reader::from_path(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(Accounts::from(record));
}
}
Ok(records)
}
async fn add_users_wolves_csv(db: &Pool<Sqlite>, server: &GuildId, user: &Accounts) {
let existing = match sqlx::query_as::<_, Accounts>(
r#"
SELECT *
FROM accounts
WHERE server = ? AND id_member = ?
"#,
)
.bind(*server.as_u64() as i64)
.bind(&user.id_member)
.fetch_one(db)
.await
{
Ok(acc) => acc.id_wolves,
Err(_) => String::new(),
};
match sqlx::query_as::<_, Accounts>(
"
INSERT OR REPLACE INTO accounts (server, id_wolves, id_member, email, expiry)
VALUES (?1, ?2, ?3, ?4, ?5)
",
)
.bind(*server.as_u64() as i64)
.bind(&existing)
.bind(&user.id_member)
.bind(&user.email)
.bind(&user.expiry)
.fetch_optional(db)
.await
{
Ok(_) => {}
Err(e) => {
println!("Failure to insert into {} {:?}", server.as_u64(), user);
println!("{:?}", e);
}
}
}
#[derive(Debug, Deserialize)]
pub struct SkynetResult {
discord: String,
@ -39,7 +129,7 @@ async fn add_users_skynet(db: &Pool<Sqlite>, server: &GuildId, user: &SkynetResu
"
UPDATE accounts
SET discord = ?
WHERE server = ? AND wolves_id = ?
WHERE server = ? AND id_wolves = ?
",
)
.bind(&user.discord)
@ -81,6 +171,7 @@ async fn add_users_skynet(db: &Pool<Sqlite>, server: &GuildId, user: &SkynetResu
#[derive(Debug, Deserialize)]
struct WolvesResult {
pub id_wolves: String,
pub id_member: String,
pub email: String,
pub expiry: String,
}
@ -88,12 +179,17 @@ async fn get_wolves(db: &Pool<Sqlite>) {
for server_config in get_server_config_bulk(db).await {
let Servers {
server,
wolves_api,
//wolves_api,
..
} = server_config;
// get the data here
let result: Vec<WolvesResult> = vec![];
let result: Vec<WolvesResult> = vec![WolvesResult {
id_wolves: "12345".to_string(),
id_member: "166425".to_string(),
email: "ul.wolves2@brendan.ie".to_string(),
expiry: "2024-08-31".to_string(),
}];
for user in result {
add_users_wolves(db, &server, &user).await;
@ -104,12 +200,33 @@ async fn get_wolves(db: &Pool<Sqlite>) {
async fn add_users_wolves(db: &Pool<Sqlite>, server: &GuildId, user: &WolvesResult) {
match sqlx::query_as::<_, Accounts>(
"
INSERT OR REPLACE INTO accounts (server, id_wolves, email, expiry)
VALUES (?1, ?2, ?3, ?4)
UPDATE accounts
SET id_wolves = ?
WHERE server = ? AND id_member = ?
",
)
.bind(&user.id_wolves)
.bind(*server.as_u64() as i64)
.bind(&user.id_member)
.fetch_optional(db)
.await
{
Ok(_) => {}
Err(e) => {
println!("Failure to update into {} {:?}", server.as_u64(), user);
println!("{:?}", e);
}
}
match sqlx::query_as::<_, Accounts>(
"
INSERT OR REPLACE INTO accounts (server, id_wolves, id_member, email, expiry)
VALUES (?1, ?2, ?3, ?4, ?5)
",
)
.bind(*server.as_u64() as i64)
.bind(&user.id_wolves)
.bind(&user.id_member)
.bind(&user.email)
.bind(&user.expiry)
.fetch_optional(db)

View file

@ -25,6 +25,7 @@ pub struct Config {
pub home: String,
pub database: String,
pub csv: String,
pub mail_smtp: String,
pub mail_user: String,
@ -53,6 +54,7 @@ pub fn get_config() -> Config {
home: ".".to_string(),
database: "database.db".to_string(),
csv: "wolves.csv".to_string(),
mail_smtp: "".to_string(),
mail_user: "".to_string(),
@ -84,6 +86,9 @@ pub fn get_config() -> Config {
if let Ok(x) = env::var("DATABASE") {
config.database = x.trim().to_string();
}
if let Ok(x) = env::var("CSV") {
config.csv = x.trim().to_string();
}
if let Ok(x) = env::var("EMAIL_SMTP") {
config.mail_smtp = x.trim().to_string();