Skip to content

Commit

Permalink
feat: delete category
Browse files Browse the repository at this point in the history
  • Loading branch information
K0nnyaku committed Nov 29, 2024
1 parent 3a6c43b commit 0d84e9f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
6 changes: 0 additions & 6 deletions src/models/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,3 @@ pub struct Category {
pub updated_at: chrono::NaiveDateTime,
}


#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(crate = "rocket::serde")]
pub struct CreateOrganization {
pub name: String,
}
42 changes: 37 additions & 5 deletions src/routes/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use surrealdb::{engine::remote::ws::Client, Surreal};

#[derive(Serialize, Deserialize)]
#[serde(crate = "rocket::serde")]
pub struct CatData {
pub struct CreateCategory {
pub id: &'r str,
pub token: &'r str,

Expand All @@ -27,7 +27,7 @@ pub struct CreateCatResponse {
#[post("/category", data = "<category>")]
pub async fn create(
db: &State<Surreal<Client>>,
category: Json<CatData<'_>>,
category: Json<CreateCategory<'_>>,
) -> Result<CreateCatResponse> {
if !session::verify(db, category.id, category.token).await {
return Err(Error::Unauthorized(Json(
Expand All @@ -38,14 +38,46 @@ pub async fn create(
let cat = category::create(db, category.id, 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(),
)))?;
.ok_or(Error::ServerError(Json("Failed to create category".into())))?;

Ok(Json(Response {
success: true,
message: "Category created successfully".into(),
data: Some(CreateCatResponse {
id: cat.id.unwrap().id.to_string(),
id: cat.id.unwrap().id.to_string(),
}),
}))
}

#[post("/category/<id>", data = "<category>")]
pub async fn delete(
db: &State<Surreal<Client>>,
id: &str,
category: Json<CreateCategory<'_>>,
) -> Result<Empty> {
if !session::verify(db, category.id, category.token).await {
return Err(Error::Unauthorized(Json(
"Failed to grant permission".into(),
)));
}

category::delete(db, id)
.await
.map_err(|e| Error::ServerError(Json(e.to_string().into())))?;

remove_dir_all(Path::new("content/").join(id))
.await?
.map_err(|e| Error::ServerError(Json(e.to_string().into())))?;

Ok(Response {
success: true,
message: "Category deleted successfully".into(),
data: None,
}
.into())
}

pub fn routes() -> Vec<rocket::Route> {
use rocket::routes;
routes![create, delete]
}
4 changes: 4 additions & 0 deletions src/utils/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ pub async fn create(
})
.await?)
}

pub async fn delete(db: &Surreal<Client>, id: &str) -> Result<Option<Category>> {
Ok(db.delete("category", id).await?)
}

0 comments on commit 0d84e9f

Please sign in to comment.