-
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
9 changed files
with
277 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"algohub-server": patch:feat | ||
--- | ||
|
||
Support create and delete solutions |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use surrealdb::sql::Thing; | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct Solution { | ||
pub id: Option<Thing>, | ||
|
||
pub problem_id: Thing, | ||
pub creator: Thing, | ||
pub title: String, | ||
pub content: String, | ||
|
||
pub created_at: chrono::NaiveDateTime, | ||
pub updated_at: chrono::NaiveDateTime, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
#[serde(crate = "rocket::serde")] | ||
pub struct SolutionData { | ||
pub title: String, | ||
pub content: String, | ||
pub problem_id: String, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
#[serde(crate = "rocket::serde")] | ||
pub struct CreateSolution<'r> { | ||
pub id: &'r str, | ||
pub token: &'r str, | ||
pub data: SolutionData, | ||
} |
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,63 @@ | ||
use crate::{ | ||
models::{ | ||
error::Error, | ||
response::{Empty, Response}, | ||
solution::CreateSolution, | ||
Credentials, OwnedId, | ||
}, | ||
utils::{session, solution}, | ||
Result, | ||
}; | ||
use rocket::{post, serde::json::Json, tokio::fs::remove_dir_all, State}; | ||
use std::path::Path; | ||
use surrealdb::{engine::remote::ws::Client, Surreal}; | ||
|
||
#[post("/create", data = "<sol>")] | ||
pub async fn create(db: &State<Surreal<Client>>, sol: Json<CreateSolution<'_>>) -> Result<OwnedId> { | ||
if !session::verify(db, sol.id, sol.token).await { | ||
return Err(Error::Unauthorized(Json( | ||
"Failed to grant permission".into(), | ||
))); | ||
} | ||
|
||
let solution = solution::create(db, sol.id, sol.into_inner().data) | ||
.await? | ||
.ok_or(Error::ServerError(Json("Failed to create solution".into())))?; | ||
|
||
Ok(Json(Response { | ||
success: true, | ||
message: "Solution created successfully".to_string(), | ||
data: Some(OwnedId { | ||
id: solution.id.unwrap().to_string(), | ||
}), | ||
})) | ||
} | ||
|
||
#[post("/delete/<id>", data = "<sol>")] | ||
pub async fn delete( | ||
db: &State<Surreal<Client>>, | ||
id: &str, | ||
sol: Json<Credentials<'_>>, | ||
) -> Result<Empty> { | ||
if !session::verify(db, sol.id, sol.token).await { | ||
return Err(Error::Unauthorized(Json( | ||
"Failed to grant permission".into(), | ||
))); | ||
} | ||
|
||
solution::delete(db, id).await?; | ||
|
||
remove_dir_all(Path::new("content/").join(id)).await?; | ||
|
||
Ok(Response { | ||
success: true, | ||
message: "Solution deleted successfully".to_string(), | ||
data: None, | ||
} | ||
.into()) | ||
} | ||
|
||
pub fn routes() -> Vec<rocket::Route> { | ||
use rocket::routes; | ||
routes![create, delete] | ||
} |
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,37 @@ | ||
use anyhow::Result; | ||
use serde::Deserialize; | ||
use surrealdb::{engine::remote::ws::Client, Surreal}; | ||
|
||
use crate::models::solution::{Solution, SolutionData}; | ||
|
||
pub async fn create( | ||
db: &Surreal<Client>, | ||
id: &str, | ||
data: SolutionData, | ||
) -> Result<Option<Solution>> { | ||
Ok(db | ||
.create("solution") | ||
.content(Solution { | ||
id: None, | ||
|
||
title: data.title, | ||
creator: ("account", id).into(), | ||
problem_id: ("problem".to_string(), data.problem_id).into(), | ||
content: data.content, | ||
|
||
created_at: chrono::Local::now().naive_local(), | ||
updated_at: chrono::Local::now().naive_local(), | ||
}) | ||
.await?) | ||
} | ||
|
||
pub async fn delete(db: &Surreal<Client>, id: &str) -> Result<Option<Solution>> { | ||
Ok(db.delete(("solution", id)).await?) | ||
} | ||
|
||
pub async fn get_by_id<M>(db: &Surreal<Client>, id: &str) -> Result<Option<M>> | ||
where | ||
for<'de> M: Deserialize<'de>, | ||
{ | ||
Ok(db.select(("solution", id)).await?) | ||
} |
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,135 @@ | ||
mod utils; | ||
|
||
use std::path::Path; | ||
|
||
use algohub_server::{ | ||
models::{ | ||
account::Register, | ||
problem::{CreateProblem, ProblemVisibility}, | ||
response::{Empty, Response}, | ||
solution::{CreateSolution, SolutionData}, | ||
Credentials, OwnedCredentials, OwnedId, Token, UserRecordId, | ||
}, | ||
routes::problem::ProblemResponse, | ||
}; | ||
use anyhow::Result; | ||
use rocket::local::asynchronous::Client; | ||
use utils::rocket; | ||
|
||
#[rocket::async_test] | ||
async fn test_solution() -> Result<()> { | ||
let rocket = rocket().await; | ||
let client = Client::tracked(rocket).await?; | ||
|
||
println!("Testing solution"); | ||
|
||
let response = client | ||
.post("/account/create") | ||
.json(&Register { | ||
username: "fu050409".to_string(), | ||
password: "password".to_string(), | ||
email: "[email protected]".to_string(), | ||
}) | ||
.dispatch() | ||
.await; | ||
|
||
assert_eq!(response.status().code, 200); | ||
|
||
let Response { | ||
success, | ||
message: _, | ||
data, | ||
} = response.into_json().await.unwrap(); | ||
let data: OwnedCredentials = data.unwrap(); | ||
|
||
let id = data.id.clone(); | ||
let token = data.token.clone(); | ||
|
||
assert!(success); | ||
|
||
let response = client | ||
.post("/problem/create") | ||
.json(&CreateProblem { | ||
id: &id, | ||
token: &token, | ||
title: "Test Problem", | ||
description: "Test Description".to_string(), | ||
input: Some("Test Input".to_string()), | ||
output: Some("Test Output".to_string()), | ||
samples: vec![], | ||
hint: None, | ||
owner: UserRecordId { | ||
tb: "account".to_string(), | ||
id: id.clone(), | ||
}, | ||
time_limit: 1000, | ||
memory_limit: 128, | ||
test_cases: vec![], | ||
categories: vec![], | ||
tags: vec![], | ||
visibility: ProblemVisibility::Public, | ||
}) | ||
.dispatch() | ||
.await; | ||
|
||
assert_eq!(response.status().code, 200); | ||
|
||
let Response { | ||
success, | ||
message: _, | ||
data, | ||
} = response.into_json().await.unwrap(); | ||
let problem_data: ProblemResponse = data.unwrap(); | ||
|
||
assert!(success); | ||
|
||
let response = client | ||
.post("/solution/create") | ||
.json(&CreateSolution { | ||
id: &id, | ||
token: &token, | ||
data: SolutionData { | ||
title: "test".to_string(), | ||
content: "test".to_string(), | ||
problem_id: problem_data.id, | ||
}, | ||
}) | ||
.dispatch() | ||
.await; | ||
|
||
assert_eq!(response.status().code, 200); | ||
|
||
let Response { | ||
success, | ||
message: _, | ||
data, | ||
} = response.into_json().await.unwrap(); | ||
let data: OwnedId = data.unwrap(); | ||
|
||
assert!(success); | ||
println!("Created organization: {}", data.id); | ||
|
||
let response = client | ||
.post(format!("/solution/delete/{}", data.id)) | ||
.json(&Credentials { | ||
id: &id, | ||
token: &token, | ||
}) | ||
.dispatch() | ||
.await; | ||
|
||
response.into_json::<Response<Empty>>().await.unwrap(); | ||
|
||
assert!(!Path::new("content").join(data.id.clone()).exists()); | ||
|
||
client | ||
.post(format!("/account/delete/{}", id)) | ||
.json(&Token { token: &token }) | ||
.dispatch() | ||
.await | ||
.into_json::<Response<Empty>>() | ||
.await | ||
.unwrap(); | ||
|
||
Ok(()) | ||
} |