-
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.
- Loading branch information
Showing
8 changed files
with
186 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
{ | ||
"words": [ | ||
"chrono", | ||
"covector", | ||
"farmfe", | ||
"ICPC", | ||
"serde", | ||
"signin", | ||
"surrealdb" | ||
] | ||
|
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 |
---|---|---|
@@ -1,25 +1,72 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use surrealdb::sql::Thing; | ||
|
||
use crate::routes::problem::ProblemData; | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
pub struct Sample { | ||
pub input: String, | ||
pub output: String, | ||
} | ||
|
||
#[derive(Debug, Default, Clone, Serialize, Deserialize)] | ||
pub enum Mode { | ||
#[default] | ||
ICPC, | ||
OI, | ||
} | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
pub struct Problem { | ||
pub id: Option<Thing>, | ||
pub sequence: String, | ||
|
||
pub title: String, | ||
pub content: String, | ||
pub input: String, | ||
pub output: String, | ||
pub samples: Vec<(String, String)>, | ||
pub description: String, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub input: Option<String>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub output: Option<String>, | ||
pub samples: Vec<Sample>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub hint: Option<String>, | ||
|
||
pub time_limit: i32, | ||
pub memory_limit: i32, | ||
pub test_cases: Vec<(String, String)>, | ||
pub test_cases: Vec<Sample>, | ||
|
||
pub creator: Thing, | ||
pub categories: Vec<String>, | ||
pub tags: Vec<String>, | ||
|
||
pub mode: Mode, | ||
pub private: bool, | ||
|
||
#[serde(skip)] | ||
pub created_at: chrono::NaiveDateTime, | ||
#[serde(skip)] | ||
pub updated_at: chrono::NaiveDateTime, | ||
} | ||
|
||
impl From<ProblemData<'_>> for Problem { | ||
fn from(val: ProblemData<'_>) -> Self { | ||
Problem { | ||
id: None, | ||
title: val.title.to_string(), | ||
description: val.description.to_string(), | ||
input: val.input, | ||
output: val.output, | ||
samples: val.samples, | ||
hint: val.hint, | ||
time_limit: val.time_limit, | ||
memory_limit: val.memory_limit, | ||
test_cases: val.test_cases, | ||
creator: ("account", val.id).into(), | ||
categories: val.categories, | ||
tags: val.tags, | ||
mode: val.mode, | ||
private: val.private, | ||
created_at: chrono::Local::now().naive_local(), | ||
updated_at: chrono::Local::now().naive_local(), | ||
} | ||
} | ||
} |
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,105 @@ | ||
use rocket::{serde::json::Json, State}; | ||
use serde::{Deserialize, Serialize}; | ||
use surrealdb::{engine::remote::ws::Client, Surreal}; | ||
|
||
use crate::{ | ||
models::{ | ||
error::Error, | ||
problem::{Mode, Problem, Sample}, | ||
response::Response, | ||
}, | ||
utils::{problem, session}, | ||
Result, | ||
}; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
#[serde(crate = "rocket::serde")] | ||
pub struct ProblemData<'r> { | ||
pub id: &'r str, | ||
pub token: &'r str, | ||
|
||
pub title: &'r str, | ||
pub description: &'r str, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub input: Option<String>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub output: Option<String>, | ||
pub samples: Vec<Sample>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub hint: Option<String>, | ||
|
||
pub time_limit: i32, | ||
pub memory_limit: i32, | ||
pub test_cases: Vec<Sample>, | ||
|
||
pub categories: Vec<String>, | ||
pub tags: Vec<String>, | ||
|
||
pub mode: Mode, | ||
pub private: bool, | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
#[serde(crate = "rocket::serde")] | ||
pub struct ProblemResponse { | ||
pub id: String, | ||
} | ||
|
||
#[post("/create", data = "<problem>")] | ||
pub async fn create( | ||
db: &State<Surreal<Client>>, | ||
problem: Json<ProblemData<'_>>, | ||
) -> Result<ProblemResponse> { | ||
if !session::verify(db, problem.id, problem.token).await { | ||
return Err(Error::Unauthorized(Json("Invalid token".into()))); | ||
} | ||
|
||
let problem = problem::create(db, problem.into_inner()) | ||
.await | ||
.map_err(|e| Error::ServerError(Json(e.to_string().into())))? | ||
.ok_or(Error::ServerError(Json( | ||
"Failed to create problem, please try again later.".into(), | ||
)))?; | ||
|
||
Ok(Json(Response { | ||
success: true, | ||
message: "Problem created successfully".to_string(), | ||
data: Some(ProblemResponse { | ||
id: problem.id.unwrap().id.to_string(), | ||
}), | ||
})) | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
#[serde(crate = "rocket::serde")] | ||
pub struct Authenticate<'r> { | ||
pub id: &'r str, | ||
pub token: &'r str, | ||
} | ||
|
||
#[post("/get", data = "<auth>")] | ||
pub async fn get(db: &State<Surreal<Client>>, auth: Json<Authenticate<'_>>) -> Result<Problem> { | ||
let problem = problem::get(db, auth.id) | ||
.await | ||
.map_err(|e| Error::ServerError(Json(e.to_string().into())))? | ||
.ok_or(Error::NotFound(Json( | ||
"Problem with specified id not found".into(), | ||
)))?; | ||
|
||
if problem.private && !session::verify(db, auth.id, auth.token).await { | ||
return Err(Error::Unauthorized(Json( | ||
"You have no permission to access this problem".into(), | ||
))); | ||
} | ||
|
||
Ok(Json(Response { | ||
success: true, | ||
message: "Problem found".to_string(), | ||
data: Some(problem), | ||
})) | ||
} | ||
|
||
pub fn routes() -> Vec<rocket::Route> { | ||
use rocket::routes; | ||
routes![create, get] | ||
} |
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