Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(problem): get account by its surrealdb id #20

Merged
merged 1 commit into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/refactor-problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"algohub-server": patch:refactor
---

Allow get account by its surrealdb id (`Thing`).
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/routes/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub async fn profile(db: &State<Surreal<Client>>, profile: Json<MergeProfile<'_>

#[get("/profile/<id>")]
pub async fn get_profile(db: &State<Surreal<Client>>, id: &str) -> Result<Profile> {
let profile = account::get_by_id::<Profile>(db, id)
let profile = account::get_by_identity::<Profile>(db, id)
.await
.map_err(|e| Error::ServerError(Json(e.to_string().into())))?
.ok_or(Error::NotFound(Json("Account not found".into())))?;
Expand Down
22 changes: 19 additions & 3 deletions src/routes/problem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -131,7 +132,7 @@ pub async fn get(
#[derive(Serialize, Deserialize)]
#[serde(crate = "rocket::serde")]
pub struct ListProblem {
pub id: Option<String>,
pub identity: Option<String>,
pub auth: Option<OwnedCredentials>,
pub limit: Option<u32>,
}
Expand All @@ -152,7 +153,22 @@ pub async fn list(

let data = data.into_inner();

let problems = problem::list_for_account::<Problem>(db, data.id, authed_id, data.limit)
let account_id = if let Some(identity) = data.identity.clone() {
Some(
account::get_by_identity::<Account>(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::<Problem>(db, account_id, authed_id, data.limit)
.await
.map_err(|e| Error::ServerError(Json(e.to_string().into())))?;

Expand Down
9 changes: 7 additions & 2 deletions src/utils/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,14 @@ pub async fn delete(db: &Surreal<Client>, id: &str) -> Result<()> {
Ok(())
}

pub async fn get_by_identity(db: &Surreal<Client>, identity: &str) -> Result<Option<Account>> {
pub async fn get_by_identity<M>(db: &Surreal<Client>, identity: &str) -> Result<Option<M>>
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)?)
Expand Down
4 changes: 2 additions & 2 deletions src/utils/session.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -41,7 +41,7 @@ pub async fn authenticate(
identity: &str,
password: &str,
) -> Result<Option<Session>> {
let account = account::get_by_identity(db, identity).await?;
let account = account::get_by_identity::<Account>(db, identity).await?;
if account.is_none() {
return Ok(None);
};
Expand Down
6 changes: 3 additions & 3 deletions tests/problem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use algohub_server::{
},
routes::{
account::{RegisterData, RegisterResponse},
problem::{ListProblem, CreateProblem, ProblemResponse},
problem::{CreateProblem, ListProblem, ProblemResponse},
},
};
use anyhow::Result;
Expand Down Expand Up @@ -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(),
Expand All @@ -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(),
Expand Down