From 6ffea8e39cb36852e19544112877a896a07fa49a Mon Sep 17 00:00:00 2001 From: HUANG Cheng Date: Mon, 27 Nov 2023 11:23:46 +0800 Subject: [PATCH] add the handler to delete a category --- migrations/20231126121150_category.up.sql | 2 +- migrations/20231126151613_user.down.sql | 1 + migrations/20231126151613_user.up.sql | 3 +++ src/bin/server.rs | 2 +- src/handlers/category.rs | 9 +++++++++ src/models/category.rs | 2 +- src/routes/category.rs | 23 ++++++++++++++++++----- 7 files changed, 34 insertions(+), 8 deletions(-) create mode 100644 migrations/20231126151613_user.down.sql create mode 100644 migrations/20231126151613_user.up.sql diff --git a/migrations/20231126121150_category.up.sql b/migrations/20231126121150_category.up.sql index b9f72a8..e626018 100644 --- a/migrations/20231126121150_category.up.sql +++ b/migrations/20231126121150_category.up.sql @@ -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 ); diff --git a/migrations/20231126151613_user.down.sql b/migrations/20231126151613_user.down.sql new file mode 100644 index 0000000..63a9582 --- /dev/null +++ b/migrations/20231126151613_user.down.sql @@ -0,0 +1 @@ +DELETE FROM user WHERE username = "admin"; diff --git a/migrations/20231126151613_user.up.sql b/migrations/20231126151613_user.up.sql new file mode 100644 index 0000000..30c277b --- /dev/null +++ b/migrations/20231126151613_user.up.sql @@ -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"); diff --git a/src/bin/server.rs b/src/bin/server.rs index d1b6fca..97d6274 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -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?; diff --git a/src/handlers/category.rs b/src/handlers/category.rs index 94b6eed..0f07472 100644 --- a/src/handlers/category.rs +++ b/src/handlers/category.rs @@ -67,3 +67,12 @@ pub async fn add_category( Ok(()) } + +pub async fn delete_category(id: &str, state: &State) -> Result<(), ServiceError> { + query(r#"DELETE FROM category WHERE id = ?"#) + .bind(id) + .execute(&state.pool) + .await?; + + Ok(()) +} diff --git a/src/models/category.rs b/src/models/category.rs index f0ef800..20bafa9 100644 --- a/src/models/category.rs +++ b/src/models/category.rs @@ -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, } diff --git a/src/routes/category.rs b/src/routes/category.rs index 90eda8e..ca9c72c 100644 --- a/src/routes/category.rs +++ b/src/routes/category.rs @@ -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}; @@ -24,12 +24,12 @@ pub async fn update<'r>( category: Json>, state: &State, _jwt: JwtMiddleware, -) -> Result, 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 = "")] @@ -44,3 +44,16 @@ pub async fn add<'r>( Ok(()) } + +#[delete("/")] +pub async fn delete<'r>( + id: &'r str, + state: &State, + _jwt: JwtMiddleware, +) -> Result<(), Status> { + delete_category(id, state) + .await + .map_err(|e| e.status())?; + + Ok(()) +}