use chrono::{DateTime, Utc}; use crate::database::models::DBUserId; pub struct ExternalLicense { pub id: i64, pub title: Option, pub status: String, pub link: Option, pub proof: Option, pub flame_project_id: Option, } impl ExternalLicense { pub async fn insert_many( exec: impl sqlx::PgExecutor<'_>, licenses: &[ExternalLicense], user_id: DBUserId, ) -> sqlx::Result<()> { let now = Utc::now(); let ids: Vec = licenses.iter().map(|x| x.id).collect(); let titles: Vec> = licenses.iter().map(|x| x.title.clone()).collect(); let statuses: Vec = licenses.iter().map(|x| x.status.clone()).collect(); let links: Vec> = licenses.iter().map(|x| x.link.clone()).collect(); let proofs: Vec> = licenses.iter().map(|x| x.proof.clone()).collect(); let flame_ids: Vec> = licenses.iter().map(|x| x.flame_project_id).collect(); let nows: Vec> = vec![now; licenses.len()]; let user_ids: Vec = vec![user_id.0; licenses.len()]; sqlx::query!( r#" INSERT INTO moderation_external_licenses (id, title, status, link, proof, flame_project_id, inserted_at, inserted_by, updated_at, updated_by) SELECT * FROM UNNEST ($1::bigint[], $2::varchar[], $3::varchar[], $4::varchar[], $5::varchar[], $6::integer[], $7::timestamptz[], $8::bigint[], $7::timestamptz[], $8::bigint[]) ON CONFLICT (id) DO UPDATE SET title = EXCLUDED.title, status = EXCLUDED.status, link = EXCLUDED.link, proof = EXCLUDED.proof, flame_project_id = EXCLUDED.flame_project_id, updated_at = EXCLUDED.updated_at, updated_by = EXCLUDED.updated_by "#, &ids, &titles as _, &statuses, &links as _, &proofs as _, &flame_ids as _, &nows, &user_ids, ) .execute(exec) .await?; Ok(()) } pub async fn insert_files( exec: impl sqlx::PgExecutor<'_>, hashes: &[Vec], filenames: &[Option], license_ids: &[i64], user_id: DBUserId, ) -> sqlx::Result<()> { let now = Utc::now(); let nows: Vec> = vec![now; license_ids.len()]; let user_ids: Vec = vec![user_id.0; license_ids.len()]; let filenames: Vec> = filenames.to_vec(); sqlx::query!( r#" INSERT INTO moderation_external_files (sha1, filename, external_license_id, inserted_at, inserted_by, updated_at, updated_by) SELECT * FROM UNNEST ($1::bytea[], $2::varchar[], $3::bigint[], $4::timestamptz[], $5::bigint[], $4::timestamptz[], $5::bigint[]) ON CONFLICT (sha1) DO UPDATE SET filename = COALESCE(EXCLUDED.filename, moderation_external_files.filename), external_license_id = EXCLUDED.external_license_id, updated_at = EXCLUDED.updated_at, updated_by = EXCLUDED.updated_by "#, hashes, &filenames as _, license_ids, &nows, &user_ids, ) .execute(exec) .await?; Ok(()) } }