Skip to content

Commit

Permalink
update model for category
Browse files Browse the repository at this point in the history
  • Loading branch information
huangcheng committed Nov 29, 2023
1 parent 690def5 commit 95b3ff6
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 4 deletions.
3 changes: 2 additions & 1 deletion migrations/20231126121150_category.up.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
CREATE TABLE category
(
id INT AUTO_INCREMENT NOT NULL PRIMARY KEY,
name VARCHAR(50) NOT NULL,
name VARCHAR(255) NOT NULL,
icon VARCHAR(255) NOT NULL,
description VARCHAR(255) NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
Expand Down
8 changes: 5 additions & 3 deletions src/handlers/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub async fn get_categories(
.get::<i64, &str>("count");

let categories = sqlx::query_as::<_, Category>(
r#"SELECT id, name, description, created_at, updated_at FROM category LIMIT ? OFFSET ?"#,
r#"SELECT id, name, description, icon, created_at, updated_at FROM category LIMIT ? OFFSET ?"#,
)
.bind(size)
.bind(page * size)
Expand All @@ -41,7 +41,7 @@ pub async fn update_category<'r>(
db: &mut Connection<Db>,
) -> Result<Category, ServiceError> {
let record = query_as::<_, Category>(
r#"SELECT id, name, description, created_at, updated_at FROM category WHERE id = ?"#,
r#"SELECT id, name, description, icon, created_at, updated_at FROM category WHERE id = ?"#,
)
.bind(id)
.fetch_one(&mut ***db)
Expand All @@ -65,13 +65,15 @@ pub async fn update_category<'r>(
id: record.id,
name,
description,
icon: record.icon,
created_at: record.created_at,
updated_at: record.updated_at,
};

query(r#"UPDATE category SET name = ?, description = ? WHERE id = ?"#)
query(r#"UPDATE category SET name = ?, description = ?, icon = ? WHERE id = ?"#)
.bind(&record.name)
.bind(&record.description)
.bind(&record.icon)
.bind(record.id)
.execute(&mut ***db)
.await?;
Expand Down
1 change: 1 addition & 0 deletions src/models/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub struct Category {
pub id: i64,
pub name: String,
pub description: String,
pub icon: String,
pub created_at: NaiveDateTime,
pub updated_at: NaiveDateTime,
}
2 changes: 2 additions & 0 deletions src/request/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ use serde::Deserialize;
pub struct UpdateCategory<'r> {
pub name: Option<&'r str>,
pub description: Option<&'r str>,
pub icon: Option<&'r str>,
}

#[derive(Debug, Deserialize)]
pub struct CreateCategory<'r> {
pub name: &'r str,
pub description: &'r str,
pub icon: &'r str,
}
2 changes: 2 additions & 0 deletions src/response/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub struct Category {
pub id: i64,
pub name: String,
pub description: String,
pub icon: String,
}

impl From<category::Category> for Category {
Expand All @@ -15,6 +16,7 @@ impl From<category::Category> for Category {
id: category.id,
name: category.name,
description: category.description,
icon: category.icon,
}
}
}

0 comments on commit 95b3ff6

Please sign in to comment.