2 use crate::schema::category;
3 use crate::schema::category::dsl::*;
5 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
6 #[table_name = "category"]
12 #[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
13 #[table_name = "category"]
14 pub struct CategoryForm {
18 impl Crud<CategoryForm> for Category {
19 fn read(conn: &PgConnection, category_id: i32) -> Result<Self, Error> {
20 category.find(category_id).first::<Self>(conn)
23 fn delete(conn: &PgConnection, category_id: i32) -> Result<usize, Error> {
24 diesel::delete(category.find(category_id)).execute(conn)
27 fn create(conn: &PgConnection, new_category: &CategoryForm) -> Result<Self, Error> {
30 .get_result::<Self>(conn)
36 new_category: &CategoryForm,
37 ) -> Result<Self, Error> {
38 diesel::update(category.find(category_id))
40 .get_result::<Self>(conn)
45 pub fn list_all(conn: &PgConnection) -> Result<Vec<Self>, Error> {
46 category.load::<Self>(conn)
55 let conn = establish_connection();
57 let categories = Category::list_all(&conn).unwrap();
58 let expected_first_category = Category {
60 name: "Discussion".into(),
63 assert_eq!(expected_first_category, categories[0]);