-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'refector/submission' of https://github.com/swpu-acm/onl…
…ine-judge into refector/submission
- Loading branch information
Showing
18 changed files
with
294 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use std::path::PathBuf; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
use surrealdb::sql::Thing; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct Asset { | ||
pub id: Option<Thing>, | ||
pub name: String, | ||
pub path: PathBuf, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use surrealdb::sql::Thing; | ||
|
||
use super::{OwnedCredentials, UserRecordId}; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
#[serde(rename_all = "lowercase")] | ||
pub enum Visibility { | ||
Public, | ||
Internal, | ||
Private, | ||
} | ||
|
||
#[derive(Debug, Default, Clone, Serialize, Deserialize)] | ||
pub enum Mode { | ||
#[default] | ||
ICPC, | ||
OI, | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct Contest { | ||
pub id: Option<Thing>, | ||
|
||
pub name: String, | ||
pub mode: Mode, | ||
pub visibility: Visibility, | ||
pub description: String, | ||
pub announcement: Option<String>, | ||
|
||
pub start_time: chrono::NaiveDateTime, | ||
pub end_time: chrono::NaiveDateTime, | ||
pub problems: Vec<Thing>, | ||
|
||
pub owner: Thing, | ||
pub creator: Thing, | ||
pub updaters: Vec<Thing>, | ||
pub participants: Vec<Thing>, | ||
|
||
pub created_at: chrono::NaiveDateTime, | ||
pub updated_at: chrono::NaiveDateTime, | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct ContestData { | ||
pub name: String, | ||
pub mode: Mode, | ||
pub visibility: Visibility, | ||
pub description: String, | ||
pub start_time: chrono::NaiveDateTime, | ||
pub end_time: chrono::NaiveDateTime, | ||
pub owner: UserRecordId, | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct CreateContest { | ||
pub auth: OwnedCredentials, | ||
pub data: ContestData, | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct AddProblems<'a> { | ||
pub auth: OwnedCredentials, | ||
pub contest_id: &'a str, | ||
pub problem_ids: Vec<&'a str>, | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct RemoveProblem { | ||
pub auth: OwnedCredentials, | ||
pub contest_id: Thing, | ||
pub problem_id: Thing, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
pub mod account; | ||
pub mod asset; | ||
pub mod contest; | ||
pub mod error; | ||
pub mod organization; | ||
pub mod problem; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use rocket::{serde::json::Json, State}; | ||
use serde::{Deserialize, Serialize}; | ||
use surrealdb::{engine::remote::ws::Client, sql::Thing, Surreal}; | ||
|
||
use crate::{ | ||
models::{ | ||
contest::{AddProblems, CreateContest}, | ||
error::Error, | ||
response::{Empty, Response}, | ||
}, | ||
utils::{contest, session}, | ||
Result, | ||
}; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct CreateResponse { | ||
pub id: String, | ||
} | ||
|
||
#[post("/create", data = "<contest>")] | ||
pub async fn create( | ||
db: &State<Surreal<Client>>, | ||
contest: Json<CreateContest>, | ||
) -> Result<CreateResponse> { | ||
if !session::verify(db, &contest.auth.id, &contest.auth.token).await { | ||
return Err(Error::Unauthorized(Json("Invalid session".into()))); | ||
} | ||
|
||
let contest = contest.into_inner(); | ||
let contest = contest::create(db, &contest.auth.id, contest.data) | ||
.await | ||
.map_err(|e| Error::ServerError(Json(e.into())))? | ||
.ok_or(Error::ServerError(Json("Failed to create contest".into())))?; | ||
|
||
Ok(Json(Response { | ||
success: true, | ||
message: "Contest created successfully".into(), | ||
data: Some(CreateResponse { | ||
id: contest.id.unwrap().id.to_string(), | ||
}), | ||
})) | ||
} | ||
|
||
#[post("/problems/add", data = "<data>")] | ||
pub async fn add_problem( | ||
db: &State<Surreal<Client>>, | ||
data: Json<AddProblems<'_>>, | ||
) -> Result<Empty> { | ||
if !session::verify(db, &data.auth.id, &data.auth.token).await { | ||
return Err(Error::Unauthorized(Json("Invalid session".into()))); | ||
} | ||
|
||
let problem = data.into_inner(); | ||
contest::add_problems( | ||
db, | ||
problem.contest_id, | ||
&problem | ||
.problem_ids | ||
.iter() | ||
.map(|&p| Thing::from(("problem", p))) | ||
.collect::<Vec<Thing>>(), | ||
) | ||
.await | ||
.map_err(|e| Error::ServerError(Json(e.into())))? | ||
.ok_or(Error::NotFound(Json("Contest not found".into())))?; | ||
|
||
Ok(Json(Response { | ||
success: true, | ||
message: "Problems added successfully".into(), | ||
data: None, | ||
})) | ||
} | ||
|
||
pub fn routes() -> Vec<rocket::Route> { | ||
use rocket::routes; | ||
routes![create, add_problem] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.