diff --git a/.changes/refactor-problem.md b/.changes/refactor-problem.md new file mode 100644 index 0000000..b6f6140 --- /dev/null +++ b/.changes/refactor-problem.md @@ -0,0 +1,5 @@ +--- +"algohub-server": patch:refactor +--- + +Allow get account by its surrealdb id (`Thing`). diff --git a/Cargo.lock b/Cargo.lock index d05f982..efbeaf8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -71,7 +71,7 @@ dependencies = [ [[package]] name = "algohub-server" -version = "0.1.3" +version = "0.1.4" dependencies = [ "anyhow", "chrono", diff --git a/src/routes/account.rs b/src/routes/account.rs index f39a5d9..240415b 100644 --- a/src/routes/account.rs +++ b/src/routes/account.rs @@ -106,7 +106,7 @@ pub async fn profile(db: &State>, profile: Json #[get("/profile/")] pub async fn get_profile(db: &State>, id: &str) -> Result { - let profile = account::get_by_id::(db, id) + let profile = account::get_by_identity::(db, id) .await .map_err(|e| Error::ServerError(Json(e.to_string().into())))? .ok_or(Error::NotFound(Json("Account not found".into())))?; diff --git a/src/routes/problem.rs b/src/routes/problem.rs index d50497c..8653958 100644 --- a/src/routes/problem.rs +++ b/src/routes/problem.rs @@ -14,12 +14,13 @@ use surrealdb::{engine::remote::ws::Client, Surreal}; use crate::{ models::{ + account::Account, error::Error, problem::{Mode, Problem, ProblemDetail, Sample}, response::Response, OwnedCredentials, }, - utils::{problem, session}, + utils::{account, problem, session}, Result, }; @@ -131,7 +132,7 @@ pub async fn get( #[derive(Serialize, Deserialize)] #[serde(crate = "rocket::serde")] pub struct ListProblem { - pub id: Option, + pub identity: Option, pub auth: Option, pub limit: Option, } @@ -152,7 +153,22 @@ pub async fn list( let data = data.into_inner(); - let problems = problem::list_for_account::(db, data.id, authed_id, data.limit) + let account_id = if let Some(identity) = data.identity.clone() { + Some( + account::get_by_identity::(db, &identity) + .await + .map_err(|e| Error::ServerError(Json(e.to_string().into())))? + .ok_or(Error::Unauthorized(Json("Invalid identity".into())))? + .id + .unwrap() + .id + .to_string(), + ) + } else { + None + }; + + let problems = problem::list_for_account::(db, account_id, authed_id, data.limit) .await .map_err(|e| Error::ServerError(Json(e.to_string().into())))?; diff --git a/src/utils/account.rs b/src/utils/account.rs index 0316a66..9bf2f3f 100644 --- a/src/utils/account.rs +++ b/src/utils/account.rs @@ -59,9 +59,14 @@ pub async fn delete(db: &Surreal, id: &str) -> Result<()> { Ok(()) } -pub async fn get_by_identity(db: &Surreal, identity: &str) -> Result> { +pub async fn get_by_identity(db: &Surreal, identity: &str) -> Result> +where + for<'de> M: Deserialize<'de>, +{ Ok(db - .query("SELECT * FROM account WHERE username = $identity OR email = $identity") + .query( + "SELECT * FROM account WHERE username = $identity OR email = $identity OR record::id(id) = $identity" + ) .bind(("identity", identity.to_string())) .await? .take(0)?) diff --git a/src/utils/session.rs b/src/utils/session.rs index 8930ba1..a20f351 100644 --- a/src/utils/session.rs +++ b/src/utils/session.rs @@ -1,7 +1,7 @@ use anyhow::Result; use surrealdb::{engine::remote::ws::Client, sql::Thing, Surreal}; -use crate::models::account::Session; +use crate::models::account::{Account, Session}; use super::account; @@ -41,7 +41,7 @@ pub async fn authenticate( identity: &str, password: &str, ) -> Result> { - let account = account::get_by_identity(db, identity).await?; + let account = account::get_by_identity::(db, identity).await?; if account.is_none() { return Ok(None); }; diff --git a/tests/problem.rs b/tests/problem.rs index b0319d2..b5b2eae 100644 --- a/tests/problem.rs +++ b/tests/problem.rs @@ -6,7 +6,7 @@ use algohub_server::{ }, routes::{ account::{RegisterData, RegisterResponse}, - problem::{ListProblem, CreateProblem, ProblemResponse}, + problem::{CreateProblem, ListProblem, ProblemResponse}, }, }; use anyhow::Result; @@ -84,7 +84,7 @@ async fn test_problem() -> Result<()> { let response = client .post("/problem/list") .json(&ListProblem { - id: Some(id.clone()), + identity: Some(id.clone()), auth: Some(OwnedCredentials { id: id.clone(), token: token.clone(), @@ -109,7 +109,7 @@ async fn test_problem() -> Result<()> { let response = client .post("/problem/list") .json(&ListProblem { - id: Some(id.clone()), + identity: Some(id.clone()), auth: Some(OwnedCredentials { id: id.clone(), token: token.clone(),