Skip to content

Commit

Permalink
add the handler to delete a category
Browse files Browse the repository at this point in the history
  • Loading branch information
huangcheng committed Nov 27, 2023
1 parent 7cf9d7a commit 6ffea8e
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 8 deletions.
2 changes: 1 addition & 1 deletion migrations/20231126121150_category.up.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CREATE TABLE category (
id VARCHAR(36) NOT NULL DEFAULT (uuid()) PRIMARY KEY,
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
description VARCHAR(255) NOT NULL
);
1 change: 1 addition & 0 deletions migrations/20231126151613_user.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DELETE FROM user WHERE username = "admin";
3 changes: 3 additions & 0 deletions migrations/20231126151613_user.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- Defualt user with password "admin"
INSERT INTO user (username, nickname, email, password, avatar)
VALUES ("admin", "Admin", "admin@startpage.ws", "$2a$12$uo6na36UFZn8/x95mQoRzucy0jHLmKEFJU8CV9m8YR4AwVPk03tGK", "https://avatars.githubusercontent.com/u/2804393?v=4");
2 changes: 1 addition & 1 deletion src/bin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ async fn main() -> Result<(), rocket::Error> {
.mount("/api/user", routes![user::me])
.mount("/api/auth", routes![auth::login, auth::logout])
.mount("/api/categories", routes![category::all])
.mount("/api/category", routes![category::update, category::add])
.mount("/api/category", routes![category::update, category::add, category::delete])
.launch()
.await?;

Expand Down
9 changes: 9 additions & 0 deletions src/handlers/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,12 @@ pub async fn add_category(

Ok(())
}

pub async fn delete_category(id: &str, state: &State<AppState>) -> Result<(), ServiceError> {
query(r#"DELETE FROM category WHERE id = ?"#)
.bind(id)
.execute(&state.pool)
.await?;

Ok(())
}
2 changes: 1 addition & 1 deletion src/models/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use sqlx::FromRow;

#[derive(Debug, Serialize, Deserialize, FromRow)]
pub struct Category {
pub id: String,
pub id: i64,
pub name: String,
pub description: String,
}
23 changes: 18 additions & 5 deletions src/routes/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use std::ops::Deref;
use rocket::http::Status;
use rocket::serde::json::Json;
use rocket::State;
use rocket::{get, post, put};
use rocket::{get, post, put, delete};

use crate::handlers::category::{add_category, get_all_categories, update_category};
use crate::handlers::category::{add_category, get_all_categories, update_category, delete_category};
use crate::middlewares::JwtMiddleware;
use crate::models::category::Category;
use crate::request::category::{CreateCategory, UpdateCategory};
Expand All @@ -24,12 +24,12 @@ pub async fn update<'r>(
category: Json<UpdateCategory<'r>>,
state: &State<AppState>,
_jwt: JwtMiddleware,
) -> Result<Json<Category>, Status> {
let category = update_category(id, category.deref(), state)
) -> Result<(), Status> {
update_category(id, category.deref(), state)
.await
.map_err(|e| e.status())?;

Ok(Json(category))
Ok(())
}

#[post("/", format = "json", data = "<category>")]
Expand All @@ -44,3 +44,16 @@ pub async fn add<'r>(

Ok(())
}

#[delete("/<id>")]
pub async fn delete<'r>(
id: &'r str,
state: &State<AppState>,
_jwt: JwtMiddleware,
) -> Result<(), Status> {
delete_category(id, state)
.await
.map_err(|e| e.status())?;

Ok(())
}

0 comments on commit 6ffea8e

Please sign in to comment.