Skip to content

Commit

Permalink
Merge branch 'main' into k8s-deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
tsunyoku authored Nov 16, 2023
2 parents c0c38ad + 96000e8 commit 4642819
Show file tree
Hide file tree
Showing 19 changed files with 178 additions and 184 deletions.
19 changes: 9 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ skill-rebalance = { package = "akatsuki-pp", git = "https://github.com/osuAkatsu
] }
rust-s3 = "0.33.0"
structured-logger = "1.0.3"
the = { package = "akatsuki-pp", git = "https://github.com/osuAkatsuki/akatsuki-pp-rs", rev = "4311fb4b4c735c1b1663959b067dfe7e28e46b61", features = [
the = { package = "akatsuki-pp", git = "https://github.com/osuAkatsuki/akatsuki-pp-rs", rev = "4ae182f4f294e6ac34eca1dc2e5f676748c4fd76", features = [
"async_tokio",
] }

Expand Down
25 changes: 25 additions & 0 deletions src/api/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use axum::{body::Body, http::Response, response::IntoResponse};

pub struct AppError(anyhow::Error);
pub type AppResult<T> = Result<T, AppError>;

impl IntoResponse for AppError {
type Body = Body;
type BodyError = <Self::Body as axum::body::HttpBody>::Error;

fn into_response(self) -> Response<Self::Body> {
Response::builder()
.status(axum::http::StatusCode::INTERNAL_SERVER_ERROR)
.body(Body::from("internal server error"))
.unwrap()
}
}

impl<E> From<E> for AppError
where
E: Into<anyhow::Error>,
{
fn from(err: E) -> Self {
Self(err.into())
}
}
1 change: 1 addition & 0 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use tower_http::trace::TraceLayer;

use crate::context::Context;

mod error;
mod routes;

fn api_router() -> Router {
Expand Down
9 changes: 4 additions & 5 deletions src/api/routes/reworks/leaderboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use axum::{
Json, Router,
};

use crate::{context::Context, models::leaderboard::Leaderboard, usecases};
use crate::{api::error::AppResult, context::Context, models::leaderboard::Leaderboard, usecases};

pub fn router() -> Router {
Router::new().route(
Expand All @@ -25,15 +25,14 @@ async fn get_rework_leaderboards(
Extension(ctx): Extension<Arc<Context>>,
Path(rework_id): Path<i32>,
Query(query): Query<LeaderboardQuery>,
) -> Json<Option<Leaderboard>> {
) -> AppResult<Json<Option<Leaderboard>>> {
let leaderboard = usecases::leaderboards::fetch_one(
rework_id,
(query.page.max(1) - 1) * query.amount,
query.amount,
ctx.clone(),
)
.await
.unwrap();
.await?;

Json(leaderboard)
Ok(Json(leaderboard))
}
10 changes: 4 additions & 6 deletions src/api/routes/reworks/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use axum::{
Json, Router,
};

use crate::{context::Context, models::queue::QueueResponse, usecases};
use crate::{api::error::AppResult, context::Context, models::queue::QueueResponse, usecases};

pub fn router() -> Router {
Router::new().route("/api/v1/reworks/:rework_id/queue", post(send_to_queue))
Expand All @@ -21,10 +21,8 @@ async fn send_to_queue(
Extension(ctx): Extension<Arc<Context>>,
Path(rework_id): Path<i32>,
Query(query): Query<QueueRequestQuery>,
) -> Json<QueueResponse> {
let response = usecases::sessions::enqueue(query.session, rework_id, ctx.clone())
.await
.unwrap();
) -> AppResult<Json<QueueResponse>> {
let response = usecases::sessions::enqueue(query.session, rework_id, ctx.clone()).await?;

Json(response)
Ok(Json(response))
}
17 changes: 7 additions & 10 deletions src/api/routes/reworks/reworks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,23 @@ use axum::{
Json, Router,
};

use crate::{context::Context, models::rework::Rework, usecases};
use crate::{api::error::AppResult, context::Context, models::rework::Rework, usecases};

pub fn router() -> Router {
Router::new()
.route("/api/v1/reworks", get(get_reworks))
.route("/api/v1/reworks/:rework_id", get(get_rework))
}

async fn get_reworks(Extension(ctx): Extension<Arc<Context>>) -> Json<Vec<Rework>> {
let reworks = usecases::reworks::fetch_all(ctx.clone()).await.unwrap();
Json(reworks)
async fn get_reworks(Extension(ctx): Extension<Arc<Context>>) -> AppResult<Json<Vec<Rework>>> {
let reworks = usecases::reworks::fetch_all(ctx.clone()).await?;
Ok(Json(reworks))
}

async fn get_rework(
Extension(ctx): Extension<Arc<Context>>,
Path(rework_id): Path<i32>,
) -> Json<Option<Rework>> {
let rework = usecases::reworks::fetch_one(rework_id, ctx.clone())
.await
.unwrap();

Json(rework)
) -> AppResult<Json<Option<Rework>>> {
let rework = usecases::reworks::fetch_one(rework_id, ctx.clone()).await?;
Ok(Json(rework))
}
27 changes: 13 additions & 14 deletions src/api/routes/reworks/scores.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use axum::{
};

use crate::{
api::error::AppResult,
context::Context,
models::{
beatmap::Beatmap,
Expand All @@ -24,10 +25,11 @@ pub fn router() -> Router {
async fn get_rework_scores(
ctx: Extension<Arc<Context>>,
Path((rework_id, user_id)): Path<(i32, i32)>,
) -> Json<Option<Vec<APIReworkScore>>> {
) -> AppResult<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,
"SELECT user_id, rework_scores.beatmap_id, rework_scores.beatmapset_id, beatmaps.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
Expand All @@ -42,26 +44,23 @@ async fn get_rework_scores(
)
.bind(user_id)
.bind(rework_id)
.fetch_all(ctx.database.get().await.unwrap().deref_mut())
.await
.unwrap();
.fetch_all(ctx.database.get().await?.deref_mut())
.await?;

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 {
beatmap_id: base_score.beatmap_id,
beatmapset_id: base_score.beatmapset_id,
song_name: base_score.song_name.clone(),
};

let score = APIReworkScore::from_base(base_score, beatmap);
scores.push(score);
}

match scores.is_empty() {
Ok(match scores.is_empty() {
true => Json(None),
false => Json(Some(scores)),
}
})
}
16 changes: 7 additions & 9 deletions src/api/routes/reworks/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use axum::{
};
use std::{ops::DerefMut, sync::Arc};

use crate::context::Context;
use crate::{api::error::AppResult, context::Context};

pub fn router() -> Router {
Router::new().route("/api/v1/reworks/:rework_id/users/search", get(search_users))
Expand All @@ -26,7 +26,7 @@ async fn search_users(
ctx: Extension<Arc<Context>>,
Path(rework_id): Path<i32>,
Query(query): Query<SearchQuery>,
) -> Json<Vec<SearchUser>> {
) -> AppResult<Json<Vec<SearchUser>>> {
let users: Vec<SearchUser> = sqlx::query_as(
"SELECT id user_id, username user_name FROM users WHERE username_safe LIKE ?",
)
Expand All @@ -38,19 +38,17 @@ async fn search_users(
.replace(" ", "_")
.replace(|c: char| !c.is_ascii(), "")
))
.fetch_all(ctx.database.get().await.unwrap().deref_mut())
.await
.unwrap();
.fetch_all(ctx.database.get().await?.deref_mut())
.await?;

let mut to_remove: Vec<i32> = Vec::new();
for user in &users {
let in_rework: bool =
sqlx::query_scalar("SELECT 1 FROM rework_stats WHERE user_id = ? AND rework_id = ?")
.bind(user.user_id)
.bind(rework_id)
.fetch_optional(ctx.database.get().await.unwrap().deref_mut())
.await
.unwrap()
.fetch_optional(ctx.database.get().await?.deref_mut())
.await?
.unwrap_or(false);

if !in_rework {
Expand All @@ -73,5 +71,5 @@ async fn search_users(
.unwrap()
});

Json(users)
Ok(Json(users))
}
18 changes: 10 additions & 8 deletions src/api/routes/reworks/sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use axum::{
use std::sync::Arc;

use crate::context::Context;
use crate::usecases;
use crate::{api::error::AppResult, usecases};

pub fn router() -> Router {
Router::new()
Expand All @@ -23,15 +23,17 @@ struct CreateSessionRequest {
async fn create_session(
Extension(ctx): Extension<Arc<Context>>,
Json(request): Json<CreateSessionRequest>,
) -> Json<usecases::sessions::CreateSessionResponse> {
) -> AppResult<Json<usecases::sessions::CreateSessionResponse>> {
let response =
usecases::sessions::create(request.username, request.password_md5, ctx.clone()).await;
usecases::sessions::create(request.username, request.password_md5, ctx.clone()).await?;

Json(response)
Ok(Json(response))
}

async fn delete_session(Extension(ctx): Extension<Arc<Context>>, Path(session): Path<String>) {
let _ = usecases::sessions::delete(session, ctx.clone())
.await
.unwrap();
async fn delete_session(
Extension(ctx): Extension<Arc<Context>>,
Path(session): Path<String>,
) -> AppResult<()> {
let _ = usecases::sessions::delete(session, ctx.clone()).await?;
Ok(())
}
Loading

0 comments on commit 4642819

Please sign in to comment.