Skip to content

Commit 6cdd342

Browse files
committed
fix
1 parent 892fbcd commit 6cdd342

4 files changed

Lines changed: 36 additions & 1 deletion

File tree

src/models/beatmaps/pending_beatmap/impl.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use super::query::{delete_by_id, find_by_hash, find_by_id, insert, last_pending_beatmap};
1+
use super::query::{
2+
bulk_insert, delete_by_id, find_by_hash, find_by_id, insert, last_pending_beatmap,
3+
};
24
use super::PendingBeatmapRow;
35
use sqlx::{Error as SqlxError, PgPool};
46

@@ -22,4 +24,8 @@ impl PendingBeatmapRow {
2224
pub async fn delete_by_id(pool: &PgPool, id: i32) -> Result<bool, SqlxError> {
2325
delete_by_id(pool, id).await
2426
}
27+
28+
pub async fn bulk_insert(pool: &PgPool, rows: &[PendingBeatmapRow]) -> Result<i64, SqlxError> {
29+
bulk_insert(pool, rows).await
30+
}
2531
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use crate::models::beatmaps::pending_beatmap::types::PendingBeatmapRow;
2+
use sqlx::{Error as SqlxError, PgPool};
3+
use validator::Validate;
4+
5+
pub async fn bulk_insert(pool: &PgPool, rows: &[PendingBeatmapRow]) -> Result<i64, SqlxError> {
6+
let filtered: Vec<&PendingBeatmapRow> = rows.iter().filter(|n| n.validate().is_ok()).collect();
7+
8+
if filtered.is_empty() {
9+
return Ok(0);
10+
}
11+
let hashes: Vec<&str> = filtered.iter().map(|r| r.osu_hash.as_str()).collect();
12+
let res = sqlx::query(
13+
"INSERT INTO pending_beatmap (osu_hash)
14+
SELECT DISTINCT UNNEST($1::text[])
15+
ON CONFLICT (osu_hash) DO NOTHING",
16+
)
17+
.bind(&hashes)
18+
.execute(pool)
19+
.await?;
20+
Ok(res.rows_affected() as i64)
21+
}

src/models/beatmaps/pending_beatmap/query/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
pub mod bulk_insert;
12
pub mod by_id;
23
pub mod delete_by_id;
34
pub mod insert;
45
pub mod last;
56

7+
pub use bulk_insert::*;
68
pub use by_id::*;
79
pub use delete_by_id::*;
810
pub use insert::*;

src/models/beatmaps/pending_beatmap/types.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,9 @@ pub struct PendingBeatmapRow {
3030
/// Timestamp when the pending beatmap was created.
3131
pub created_at: Option<NaiveDateTime>,
3232
}
33+
34+
#[derive(Debug, Clone)]
35+
pub struct PendingBeatmapNew {
36+
pub osu_hash: String,
37+
pub osu_id: Option<i32>,
38+
}

0 commit comments

Comments
 (0)