1 use schema::{category};
2 use 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)
24 fn delete(conn: &PgConnection, category_id: i32) -> Result<usize, Error> {
25 diesel::delete(category.find(category_id))
29 fn create(conn: &PgConnection, new_category: &CategoryForm) -> Result<Self, Error> {
32 .get_result::<Self>(conn)
35 fn update(conn: &PgConnection, category_id: i32, new_category: &CategoryForm) -> Result<Self, Error> {
36 diesel::update(category.find(category_id))
38 .get_result::<Self>(conn)
43 pub fn list_all(conn: &PgConnection) -> Result<Vec<Self>, Error> {
44 category.load::<Self>(conn)
53 let conn = establish_connection();
55 let categories = Category::list_all(&conn).unwrap();
56 let expected_first_category = Category {
58 name: "Discussion".into()
61 assert_eq!(expected_first_category, categories[0]);