Skip to content

Commit

Permalink
feat(submission): list by problem for account (#50)
Browse files Browse the repository at this point in the history
* feat(problem): optimize endpoints

* chore: fix code lint

* feat(submission: list by problem for account

* fix: code lint
  • Loading branch information
fu050409 authored Dec 5, 2024
1 parent 81269bf commit 8a48b23
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changes/problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"algohub-server": patch:feat
---

Optimize endpoints for problem updates.
5 changes: 5 additions & 0 deletions .changes/submission.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"algohub-server": patch:feat
---

List submissions by problem for account.
20 changes: 9 additions & 11 deletions src/routes/submission.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,17 @@ pub async fn list_by_problem(
}))
}

#[post("/list/contest/<contest_id>/<user_id>", data = "<_auth>")]
pub async fn list_within_contest(
#[post("/list/<id>/account/<user_id>", data = "<auth>")]
pub async fn list_by_problem_for_account(
db: &State<Surreal<Client>>,
contest_id: &str,
id: &str,
user_id: &str,
_auth: Json<Credentials<'_>>,
auth: Json<Credentials<'_>>,
) -> Result<Vec<Submission>> {
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,
Expand All @@ -139,7 +137,7 @@ pub fn routes() -> Vec<rocket::Route> {
get,
list_by_user,
list_by_contest,
list_within_contest,
list_by_problem_for_account,
list_by_problem,
]
}
16 changes: 16 additions & 0 deletions src/utils/submission.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,19 @@ pub async fn list_by_problem(db: &Surreal<Client>, problem: Thing) -> Result<Vec
.await?
.take(0)?)
}

const LIST_BY_PROBLEM_FOR_USER_QUERY: &str = r#"
SELECT * FROM submission WHERE record::id(problem) = $id AND record::id(creator) = $account_id ORDER BY created_at DESC
"#;
pub async fn list_by_problem_for_account(
db: &Surreal<Client>,
id: &str,
account_id: &str,
) -> Result<Vec<Submission>> {
Ok(db
.query(LIST_BY_PROBLEM_FOR_USER_QUERY)
.bind(("id", id.to_string()))
.bind(("account_id", account_id.to_string()))
.await?
.take(0)?)
}

0 comments on commit 8a48b23

Please sign in to comment.