Skip to content

Commit

Permalink
Eliminate extra queries on get_rework_scores endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
cmyui committed Nov 11, 2023
1 parent d410144 commit fe95c9e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
26 changes: 12 additions & 14 deletions src/api/routes/reworks/scores.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ async fn get_rework_scores(
) -> Json<Option<Vec<APIReworkScore>>> {
let base_scores: Vec<APIBaseReworkScore> =
sqlx::query_as(
"SELECT user_id, rework_scores.beatmap_id, rework_scores.beatmapset_id, rework_id, score_id, rework_scores.max_combo, mods, accuracy, score, num_300s, num_100s, num_50s, num_gekis,
num_katus, num_misses, old_pp, new_pp,
DENSE_RANK() OVER (ORDER BY old_pp DESC) old_rank, DENSE_RANK() OVER (ORDER BY new_pp DESC) new_rank
FROM
rework_scores
"SELECT user_id, rework_scores.beatmap_id, rework_scores.beatmapset_id, beatmap.song_name, rework_id, score_id, rework_scores.max_combo, mods, accuracy, score, num_300s, num_100s, num_50s, num_gekis,
num_katus, num_misses, old_pp, new_pp,
DENSE_RANK() OVER (ORDER BY old_pp DESC) old_rank, DENSE_RANK() OVER (ORDER BY new_pp DESC) new_rank
FROM
rework_scores
INNER JOIN beatmaps
ON rework_scores.beatmap_id = beatmaps.beatmap_id
WHERE
WHERE
user_id = ? AND rework_id = ?
ORDER BY
ORDER BY
new_pp DESC
LIMIT 100",
)
Expand All @@ -48,13 +48,11 @@ async fn get_rework_scores(

let mut scores: Vec<APIReworkScore> = Vec::new();
for base_score in base_scores {
let beatmap: Beatmap = sqlx::query_as(
"SELECT beatmap_id, beatmapset_id, song_name FROM beatmaps WHERE beatmap_id = ?",
)
.bind(base_score.beatmap_id)
.fetch_one(ctx.database.get().await.unwrap().deref_mut())
.await
.unwrap();
let beatmap = Beatmap {
beatmap_id: base_score.beatmap_id,
beatmapset_id: base_score.beatmapset_id,
song_name: base_score.song_name,
};

let score = APIReworkScore::from_base(base_score, beatmap);
scores.push(score);
Expand Down
1 change: 1 addition & 0 deletions src/models/score.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub struct APIBaseReworkScore {
pub score_id: i64,
pub beatmap_id: i32,
pub beatmapset_id: i32,
pub song_name: String,
pub user_id: i32,
pub rework_id: i32,
pub max_combo: i32,
Expand Down

0 comments on commit fe95c9e

Please sign in to comment.