feat: nailing down teh format

This commit is contained in:
silver 2023-09-16 18:02:15 +01:00
parent 974173857c
commit 670a85d2ef
2 changed files with 47 additions and 20 deletions

View file

@ -83,7 +83,8 @@ async fn fetch_accounts(ctx: &Context) {
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct SkynetResult { pub struct SkynetResult {
discord: String, discord: String,
wolves_id: String, id_wolves: String,
id_member: String,
} }
async fn get_skynet(db: &Pool<Sqlite>, config: &Config) { async fn get_skynet(db: &Pool<Sqlite>, config: &Config) {
let url = format!("{}/ldap/discord?auth={}", &config.ldap_api, &config.auth); let url = format!("{}/ldap/discord?auth={}", &config.ldap_api, &config.auth);
@ -94,23 +95,46 @@ async fn get_skynet(db: &Pool<Sqlite>, config: &Config) {
} }
} }
async fn add_users_skynet(db: &Pool<Sqlite>, server: &GuildId, user: &SkynetResult) { async fn add_users_skynet(db: &Pool<Sqlite>, server: &GuildId, user: &SkynetResult) {
match sqlx::query_as::<_, Accounts>( if !user.id_wolves.is_empty() {
" match sqlx::query_as::<_, Accounts>(
"
UPDATE accounts UPDATE accounts
SET discord = ? SET discord = ?
WHERE server = ? AND wolves_id = ? WHERE server = ? AND wolves_id = ?
", ",
) )
.bind(&user.discord) .bind(&user.discord)
.bind(*server.as_u64() as i64) .bind(*server.as_u64() as i64)
.bind(&user.wolves_id) .bind(&user.id_wolves)
.fetch_optional(db) .fetch_optional(db)
.await .await
{ {
Ok(_) => {} Ok(_) => {}
Err(e) => { Err(e) => {
println!("Failure to insert into {} {:?}", server.as_u64(), user); println!("Failure to insert into {} {:?}", server.as_u64(), user);
println!("{:?}", e); println!("{:?}", e);
}
}
}
if !user.id_member.is_empty() {
match sqlx::query_as::<_, Accounts>(
"
UPDATE accounts
SET discord = ?
WHERE server = ? AND id_member = ?
",
)
.bind(&user.discord)
.bind(*server.as_u64() as i64)
.bind(&user.id_member)
.fetch_optional(db)
.await
{
Ok(_) => {}
Err(e) => {
println!("Failure to insert into {} {:?}", server.as_u64(), user);
println!("{:?}", e);
}
} }
} }
} }
@ -141,7 +165,7 @@ async fn get_wolves(db: &Pool<Sqlite>) {
async fn add_users_wolves(db: &Pool<Sqlite>, server: &GuildId, user: &WolvesResult) { async fn add_users_wolves(db: &Pool<Sqlite>, server: &GuildId, user: &WolvesResult) {
match sqlx::query_as::<_, Accounts>( match sqlx::query_as::<_, Accounts>(
" "
INSERT OR REPLACE INTO accounts (server, wolves_id, email, expiry) INSERT OR REPLACE INTO accounts (server, id_wolves, email, expiry)
VALUES (?1, ?2, ?3, ?4) VALUES (?1, ?2, ?3, ?4)
", ",
) )

View file

@ -105,7 +105,8 @@ fn str_to_num<T: FromStr + Default>(x: &str) -> T {
#[derive(Debug, Clone, Deserialize, Serialize)] #[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Accounts { pub struct Accounts {
pub server: GuildId, pub server: GuildId,
pub wolves_id: String, pub id_wolves: String,
pub id_member: String,
pub email: String, pub email: String,
pub expiry: String, pub expiry: String,
pub discord: Option<String>, pub discord: Option<String>,
@ -119,7 +120,8 @@ impl<'r> FromRow<'r, SqliteRow> for Accounts {
Ok(Self { Ok(Self {
server, server,
wolves_id: row.try_get("wolves_api")?, id_wolves: row.try_get("id_wolves")?,
id_member: row.try_get("id_member")?,
email: row.try_get("email")?, email: row.try_get("email")?,
expiry: row.try_get("expiry")?, expiry: row.try_get("expiry")?,
discord: row.try_get("discord")?, discord: row.try_get("discord")?,
@ -178,19 +180,20 @@ pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
sqlx::query( sqlx::query(
"CREATE TABLE IF NOT EXISTS accounts ( "CREATE TABLE IF NOT EXISTS accounts (
server integer not null, server integer not null,
wolves_id text not null, id_wolves text DEFAULT '',
id_member text DEFAULT '',
email text not null, email text not null,
expiry text not null, expiry text not null,
discord text, discord text,
minecraft text, minecraft text,
CONSTRAINT con_server_wolves PRIMARY KEY(server,wolves_id) PRIMARY KEY(server,id_wolves,id_member)
)", )",
) )
.execute(&pool) .execute(&pool)
.await?; .await?;
sqlx::query("CREATE INDEX IF NOT EXISTS index_server ON accounts (server)").execute(&pool).await?; sqlx::query("CREATE INDEX IF NOT EXISTS index_server ON accounts (server)").execute(&pool).await?;
sqlx::query("CREATE INDEX IF NOT EXISTS index_wolves_id ON accounts (wolves_id)") sqlx::query("CREATE INDEX IF NOT EXISTS index_id_wolves ON accounts (id_wolves)")
.execute(&pool) .execute(&pool)
.await?; .await?;