Skip to content

Commit

Permalink
feat(contest): remove contest rank (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
fu050409 authored Dec 10, 2024
1 parent 05a69e7 commit 27fd2c2
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 63 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

9 changes: 0 additions & 9 deletions src/models/contest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,3 @@ pub struct ContestProblem {
pub submitted_count: u32,
pub accepted_count: u32,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct ContestRank {
name: String,
problem_id: String,
accepted: bool,
wrongs: u32,
}
21 changes: 1 addition & 20 deletions src/routes/contest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use surrealdb::{engine::remote::ws::Client, sql::Thing, Surreal};

use crate::{
models::{
contest::{AddProblems, ContestProblem, ContestRank, CreateContest, UserContest},
contest::{AddProblems, ContestProblem, CreateContest, UserContest},
error::Error,
response::{Empty, Response},
Credentials, OwnedId,
Expand Down Expand Up @@ -117,25 +117,6 @@ pub async fn get(
}))
}

#[post("/rank/<id>", data = "<auth>")]
pub async fn rank(
db: &State<Surreal<Client>>,
id: &str,
auth: Json<Credentials<'_>>,
) -> Result<Vec<ContestRank>> {
if !session::verify(db, auth.id, auth.token).await {
return Err(Error::Unauthorized(Json("Invalid credentials".into())));
}

let rank = contest::rank(db, id).await?;

Ok(Json(Response {
success: true,
message: "Contest rank retrieved successfully".into(),
data: Some(rank),
}))
}

pub fn routes() -> Vec<rocket::Route> {
use rocket::routes;
routes![create, get, add_problems, list_problems, list_all]
Expand Down
34 changes: 1 addition & 33 deletions src/utils/contest.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::Result;
use surrealdb::{engine::remote::ws::Client, sql::Thing, Surreal};

use crate::models::contest::{Contest, ContestData, ContestProblem, ContestRank};
use crate::models::contest::{Contest, ContestData, ContestProblem};

pub async fn create(
db: &Surreal<Client>,
Expand Down Expand Up @@ -105,35 +105,3 @@ pub async fn remove_problem(
.await?
.take(0)?)
}

const RANK_QUERY: &str = r#"
SELECT VALUE array::map((SELECT VALUE id FROM type::thing("contest", $id).problems), |$problem| {
LET $submissions = SELECT judge_result.status.type AS status, created_at
FROM submission WHERE problem.id == $problem AND $parent.id == creator ORDER BY created_at ASC;
LET $first_accepted = array::find_index($submissions, |$submission| {
RETURN $submission.status == "accepted"
});
RETURN IF $first_accepted {
RETURN {
name: $problem.title,
problem_id: record::id($problem),
accepted: true,
wrongs: count($submissions) - $first_accepted - 1,
}
} ELSE {
RETURN {
name: $problem.title,
problem_id: record::id($problem),
accepted: false,
wrongs: count($submissions),
}
}
}) FROM array::distinct(SELECT VALUE creator FROM submission)
"#;
pub async fn rank(db: &Surreal<Client>, id: &str) -> Result<Vec<ContestRank>> {
Ok(db
.query(RANK_QUERY)
.bind(("id", id.to_string()))
.await?
.take(0)?)
}

0 comments on commit 27fd2c2

Please sign in to comment.