-
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.
* refector: submission * chore: format the code * feat: remove `status` field * feat: get by id --------- Co-authored-by: 苏向夜 <[email protected]>
- Loading branch information
Showing
8 changed files
with
142 additions
and
132 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -6,5 +6,6 @@ pub mod organization; | |
pub mod problem; | ||
pub mod response; | ||
pub mod shared; | ||
pub mod submission; | ||
|
||
pub use shared::*; |
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,33 @@ | ||
use eval_stack::{compile::Language, judge::JudgeResult}; | ||
use serde::{Deserialize, Serialize}; | ||
use surrealdb::sql::Thing; | ||
|
||
#[derive(Serialize, Deserialize, Clone, Debug)] | ||
#[serde(untagged, rename_all = "snake_case")] | ||
pub enum Status { | ||
InQueue, | ||
Judging, | ||
Ready, | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct Submission { | ||
pub id: Option<Thing>, | ||
|
||
pub lang: Language, | ||
pub problem_id: String, | ||
pub code: String, | ||
pub status: Status, | ||
pub results: Vec<JudgeResult>, | ||
pub creator: Thing, | ||
|
||
pub created_at: chrono::NaiveDateTime, | ||
pub updated_at: chrono::NaiveDateTime, | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct UserSubmission<'r> { | ||
pub lang: Language, | ||
pub problem_id: &'r str, | ||
pub code: String, | ||
} |
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,70 @@ | ||
use rocket::{serde::json::Json, State}; | ||
use serde::{Deserialize, Serialize}; | ||
use surrealdb::{engine::remote::ws::Client, Surreal}; | ||
|
||
use crate::{ | ||
models::{ | ||
error::Error, | ||
response::Response, | ||
submission::{Submission, UserSubmission}, | ||
Credentials, | ||
}, | ||
utils::{session, submission}, | ||
Result, | ||
}; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct CreateSubmission<'r> { | ||
pub id: &'r str, | ||
pub token: &'r str, | ||
|
||
pub sub: UserSubmission<'r>, | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct SubmitResponse { | ||
pub id: String, | ||
} | ||
|
||
#[post("/submit", data = "<data>")] | ||
pub async fn submit( | ||
db: &State<Surreal<Client>>, | ||
data: Json<CreateSubmission<'_>>, | ||
) -> Result<SubmitResponse> { | ||
if !session::verify(db, data.id, data.token).await { | ||
return Err(Error::Unauthorized(Json("Invalid token".into()))); | ||
} | ||
|
||
let submission = submission::create(db, data.id, data.into_inner().sub) | ||
.await | ||
.map_err(|e| Error::ServerError(Json(e.to_string().into())))? | ||
.ok_or(Error::ServerError(Json( | ||
"Failed to submit, please try again later.".into(), | ||
)))?; | ||
|
||
Ok(Json(Response { | ||
success: true, | ||
message: "Submission created successfully".to_string(), | ||
data: Some(SubmitResponse { | ||
id: submission.id.unwrap().id.to_string(), | ||
}), | ||
})) | ||
} | ||
|
||
#[post("/get/<id>", data = "<_auth>")] | ||
pub async fn get( | ||
db: &State<Surreal<Client>>, | ||
id: &str, | ||
_auth: Json<Credentials<'_>>, | ||
) -> Result<Submission> { | ||
let submission = submission::get_by_id(db, id) | ||
.await | ||
.map_err(|e| Error::ServerError(Json(e.to_string().into())))? | ||
.ok_or(Error::NotFound(Json("Submission not found".into())))?; | ||
|
||
Ok(Json(Response { | ||
success: true, | ||
message: "Submission fetched successfully".to_string(), | ||
data: Some(submission.into()), | ||
})) | ||
} |
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,30 @@ | ||
use crate::models::submission::{Submission, UserSubmission}; | ||
use anyhow::Result; | ||
use surrealdb::{engine::remote::ws::Client, Surreal}; | ||
|
||
pub async fn create<'a>( | ||
db: &'a Surreal<Client>, | ||
id: &str, | ||
submission: UserSubmission<'a>, | ||
) -> Result<Option<Submission>> { | ||
Ok(db | ||
.create("submission") | ||
.content(Submission { | ||
id: None, | ||
lang: submission.lang, | ||
code: submission.code, | ||
problem_id: submission.problem_id.to_string(), | ||
status: crate::models::submission::Status::InQueue, | ||
|
||
creator: ("account", id).into(), | ||
results: vec![], | ||
|
||
created_at: chrono::Local::now().naive_local(), | ||
updated_at: chrono::Local::now().naive_local(), | ||
}) | ||
.await?) | ||
} | ||
|
||
pub async fn get_by_id(db: &Surreal<Client>, id: &str) -> Result<Option<Submission>> { | ||
Ok(db.select(("submission", id)).await?) | ||
} |