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)]
pub struct SkynetResult {
discord: String,
wolves_id: String,
id_wolves: String,
id_member: String,
}
async fn get_skynet(db: &Pool<Sqlite>, config: &Config) {
let url = format!("{}/ldap/discord?auth={}", &config.ldap_api, &config.auth);
@ -94,6 +95,7 @@ async fn get_skynet(db: &Pool<Sqlite>, config: &Config) {
}
}
async fn add_users_skynet(db: &Pool<Sqlite>, server: &GuildId, user: &SkynetResult) {
if !user.id_wolves.is_empty() {
match sqlx::query_as::<_, Accounts>(
"
UPDATE accounts
@ -103,7 +105,7 @@ async fn add_users_skynet(db: &Pool<Sqlite>, server: &GuildId, user: &SkynetResu
)
.bind(&user.discord)
.bind(*server.as_u64() as i64)
.bind(&user.wolves_id)
.bind(&user.id_wolves)
.fetch_optional(db)
.await
{
@ -113,6 +115,28 @@ async fn add_users_skynet(db: &Pool<Sqlite>, server: &GuildId, user: &SkynetResu
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);
}
}
}
}
#[derive(Debug, Deserialize)]
@ -141,7 +165,7 @@ 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, wolves_id, email, expiry)
INSERT OR REPLACE INTO accounts (server, id_wolves, email, expiry)
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)]
pub struct Accounts {
pub server: GuildId,
pub wolves_id: String,
pub id_wolves: String,
pub id_member: String,
pub email: String,
pub expiry: String,
pub discord: Option<String>,
@ -119,7 +120,8 @@ impl<'r> FromRow<'r, SqliteRow> for Accounts {
Ok(Self {
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")?,
expiry: row.try_get("expiry")?,
discord: row.try_get("discord")?,
@ -178,19 +180,20 @@ pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
sqlx::query(
"CREATE TABLE IF NOT EXISTS accounts (
server integer not null,
wolves_id text not null,
id_wolves text DEFAULT '',
id_member text DEFAULT '',
email text not null,
expiry text not null,
discord text,
minecraft text,
CONSTRAINT con_server_wolves PRIMARY KEY(server,wolves_id)
PRIMARY KEY(server,id_wolves,id_member)
)",
)
.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)
.await?;