Skip to content

Commit

Permalink
chore: use UserRecordID
Browse files Browse the repository at this point in the history
  • Loading branch information
K0nnyaku committed Nov 29, 2024
1 parent 610c8d2 commit af42013
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/models/category.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::models::UserRecordId;
use serde::{Deserialize, Serialize};
use surrealdb::sql::Thing;

Expand All @@ -15,5 +16,5 @@ pub struct Category {
#[serde(crate = "rocket::serde")]
pub struct CreateCategory {
pub name: String,
pub group: String,
pub owner: UserRecordId,
}
16 changes: 5 additions & 11 deletions src/routes/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
category::{Category, CreateCategory},
error::Error,
response::{Empty, Response},
UserRecordId,
},
utils::{category, session},
Result,
Expand Down Expand Up @@ -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())))?;
Expand Down Expand Up @@ -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 = "<data>")]
pub async fn get_by_owner(
db: &State<Surreal<Client>>,
data: Json<GetByOwner>,
data: Json<UserRecordId>,
) -> Result<Vec<Category>> {
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())))?;

Expand Down Expand Up @@ -127,5 +121,5 @@ pub async fn get_by_name(db: &State<Surreal<Client>>, data: Json<GetByName>) ->

pub fn routes() -> Vec<rocket::Route> {
use rocket::routes;
routes![create, delete]
routes![create, delete, get_by_name, get_by_owner]
}
8 changes: 2 additions & 6 deletions src/utils/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Client>,
id: &str,
cat: CreateCategory,
) -> Result<Option<Category>> {
pub async fn create(db: &Surreal<Client>, cat: CreateCategory) -> Result<Option<Category>> {
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(),
})
Expand Down

0 comments on commit af42013

Please sign in to comment.