From 8a48b23c0c2821f0315b3745ff04aaf54e29c295 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E5=90=91=E5=A4=9C?= <46275354+fu050409@users.noreply.github.com> Date: Thu, 5 Dec 2024 17:42:23 +0800 Subject: [PATCH] feat(submission): list by problem for account (#50) * feat(problem): optimize endpoints * chore: fix code lint * feat(submission: list by problem for account * fix: code lint --- .changes/problem.md | 5 +++++ .changes/submission.md | 5 +++++ src/routes/submission.rs | 20 +++++++++----------- src/utils/submission.rs | 16 ++++++++++++++++ 4 files changed, 35 insertions(+), 11 deletions(-) create mode 100644 .changes/problem.md create mode 100644 .changes/submission.md diff --git a/.changes/problem.md b/.changes/problem.md new file mode 100644 index 0000000..01f3f8a --- /dev/null +++ b/.changes/problem.md @@ -0,0 +1,5 @@ +--- +"algohub-server": patch:feat +--- + +Optimize endpoints for problem updates. diff --git a/.changes/submission.md b/.changes/submission.md new file mode 100644 index 0000000..8780b65 --- /dev/null +++ b/.changes/submission.md @@ -0,0 +1,5 @@ +--- +"algohub-server": patch:feat +--- + +List submissions by problem for account. \ No newline at end of file diff --git a/src/routes/submission.rs b/src/routes/submission.rs index eabbe6d..0eaa1bc 100644 --- a/src/routes/submission.rs +++ b/src/routes/submission.rs @@ -111,19 +111,17 @@ pub async fn list_by_problem( })) } -#[post("/list/contest//", data = "<_auth>")] -pub async fn list_within_contest( +#[post("/list//account/", data = "")] +pub async fn list_by_problem_for_account( db: &State>, - contest_id: &str, + id: &str, user_id: &str, - _auth: Json>, + auth: Json>, ) -> Result> { - let submissions = submission::list_within_contest( - db, - ("contest", contest_id).into(), - ("account", user_id).into(), - ) - .await?; + if !session::verify(db, auth.id, auth.token).await { + return Err(Error::Unauthorized(Json("Invalid credentials".into()))); + } + let submissions = submission::list_by_problem_for_account(db, id, user_id).await?; Ok(Json(Response { success: true, @@ -139,7 +137,7 @@ pub fn routes() -> Vec { get, list_by_user, list_by_contest, - list_within_contest, + list_by_problem_for_account, list_by_problem, ] } diff --git a/src/utils/submission.rs b/src/utils/submission.rs index 4245b2e..6573d94 100644 --- a/src/utils/submission.rs +++ b/src/utils/submission.rs @@ -72,3 +72,19 @@ pub async fn list_by_problem(db: &Surreal, problem: Thing) -> Result, + id: &str, + account_id: &str, +) -> Result> { + Ok(db + .query(LIST_BY_PROBLEM_FOR_USER_QUERY) + .bind(("id", id.to_string())) + .bind(("account_id", account_id.to_string())) + .await? + .take(0)?) +}