2 use diesel::{dsl::*, result::Error, *};
3 use lemmy_db_schema::{schema::category::dsl::*, source::category::*};
5 impl Crud<CategoryForm> for Category {
6 fn read(conn: &PgConnection, category_id: i32) -> Result<Self, Error> {
7 category.find(category_id).first::<Self>(conn)
10 fn create(conn: &PgConnection, new_category: &CategoryForm) -> Result<Self, Error> {
13 .get_result::<Self>(conn)
19 new_category: &CategoryForm,
20 ) -> Result<Self, Error> {
21 diesel::update(category.find(category_id))
23 .get_result::<Self>(conn)
28 fn list_all(conn: &PgConnection) -> Result<Vec<Category>, Error>;
31 impl Category_ for Category {
32 fn list_all(conn: &PgConnection) -> Result<Vec<Category>, Error> {
33 category.load::<Self>(conn)
39 use crate::{establish_unpooled_connection, source::category::Category_};
40 use lemmy_db_schema::source::category::Category;
44 let conn = establish_unpooled_connection();
46 let categories = Category::list_all(&conn).unwrap();
47 let expected_first_category = Category {
49 name: "Discussion".into(),
52 assert_eq!(expected_first_category, categories[0]);