diff --git a/src/models/category.rs b/src/models/category.rs index e55bdaf..bcda9b2 100644 --- a/src/models/category.rs +++ b/src/models/category.rs @@ -1,3 +1,4 @@ +use crate::models::UserRecordId; use serde::{Deserialize, Serialize}; use surrealdb::sql::Thing; @@ -15,5 +16,5 @@ pub struct Category { #[serde(crate = "rocket::serde")] pub struct CreateCategory { pub name: String, - pub group: String, + pub owner: UserRecordId, } diff --git a/src/routes/category.rs b/src/routes/category.rs index 3a1b0ae..8fc809e 100644 --- a/src/routes/category.rs +++ b/src/routes/category.rs @@ -3,6 +3,7 @@ use crate::{ category::{Category, CreateCategory}, error::Error, response::{Empty, Response}, + UserRecordId, }, utils::{category, session}, Result, @@ -37,7 +38,7 @@ pub async fn create( ))); } - let cat = category::create(db, category.id, category.into_inner().cat) + let cat = category::create(db, category.into_inner().cat) .await .map_err(|e| Error::ServerError(Json(e.to_string().into())))? .ok_or(Error::ServerError(Json("Failed to create category".into())))?; @@ -79,19 +80,12 @@ pub async fn delete( .into()) } -#[derive(Serialize, Deserialize)] -#[serde(crate = "rocket::serde")] -pub struct GetByOwner { - pub group: String, - pub owner: String, -} - #[post("/get_by_owner", data = "")] pub async fn get_by_owner( db: &State>, - data: Json, + data: Json, ) -> Result> { - let result = category::get_by_owner(db, (data.group.clone(), data.owner.clone()).into()) + let result = category::get_by_owner(db, data.into_inner().into()) .await .map_err(|e| Error::ServerError(Json(e.to_string().into())))?; @@ -127,5 +121,5 @@ pub async fn get_by_name(db: &State>, data: Json) -> pub fn routes() -> Vec { use rocket::routes; - routes![create, delete] + routes![create, delete, get_by_name, get_by_owner] } diff --git a/src/utils/category.rs b/src/utils/category.rs index e621ba7..3e348eb 100644 --- a/src/utils/category.rs +++ b/src/utils/category.rs @@ -3,17 +3,13 @@ use surrealdb::{engine::remote::ws::Client, sql::Thing, Surreal}; use crate::models::category::{Category, CreateCategory}; -pub async fn create( - db: &Surreal, - id: &str, - cat: CreateCategory, -) -> Result> { +pub async fn create(db: &Surreal, cat: CreateCategory) -> Result> { Ok(db .create("category") .content(Category { id: None, name: cat.name, - owner: (cat.group, id.to_string()).into(), + owner: cat.owner.into(), created_at: chrono::Local::now().naive_local(), updated_at: chrono::Local::now().naive_local(), })