mirror of https://github.com/LemmyNet/lemmy
Remove categories (fixes #1429)
parent
40ceec9737
commit
3141ad31de
@ -1,54 +0,0 @@
|
|||||||
use crate::Crud;
|
|
||||||
use diesel::{dsl::*, result::Error, *};
|
|
||||||
use lemmy_db_schema::{schema::category::dsl::*, source::category::*};
|
|
||||||
|
|
||||||
impl Crud<CategoryForm> for Category {
|
|
||||||
fn read(conn: &PgConnection, category_id: i32) -> Result<Self, Error> {
|
|
||||||
category.find(category_id).first::<Self>(conn)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn create(conn: &PgConnection, new_category: &CategoryForm) -> Result<Self, Error> {
|
|
||||||
insert_into(category)
|
|
||||||
.values(new_category)
|
|
||||||
.get_result::<Self>(conn)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn update(
|
|
||||||
conn: &PgConnection,
|
|
||||||
category_id: i32,
|
|
||||||
new_category: &CategoryForm,
|
|
||||||
) -> Result<Self, Error> {
|
|
||||||
diesel::update(category.find(category_id))
|
|
||||||
.set(new_category)
|
|
||||||
.get_result::<Self>(conn)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait Category_ {
|
|
||||||
fn list_all(conn: &PgConnection) -> Result<Vec<Category>, Error>;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Category_ for Category {
|
|
||||||
fn list_all(conn: &PgConnection) -> Result<Vec<Category>, Error> {
|
|
||||||
category.load::<Self>(conn)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use crate::{establish_unpooled_connection, source::category::Category_};
|
|
||||||
use lemmy_db_schema::source::category::Category;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_crud() {
|
|
||||||
let conn = establish_unpooled_connection();
|
|
||||||
|
|
||||||
let categories = Category::list_all(&conn).unwrap();
|
|
||||||
let expected_first_category = Category {
|
|
||||||
id: 1,
|
|
||||||
name: "Discussion".into(),
|
|
||||||
};
|
|
||||||
|
|
||||||
assert_eq!(expected_first_category, categories[0]);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,15 +0,0 @@
|
|||||||
use crate::schema::category;
|
|
||||||
use serde::Serialize;
|
|
||||||
|
|
||||||
#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Clone)]
|
|
||||||
#[table_name = "category"]
|
|
||||||
pub struct Category {
|
|
||||||
pub id: i32,
|
|
||||||
pub name: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Insertable, AsChangeset)]
|
|
||||||
#[table_name = "category"]
|
|
||||||
pub struct CategoryForm {
|
|
||||||
pub name: String,
|
|
||||||
}
|
|
@ -0,0 +1,34 @@
|
|||||||
|
create table category (
|
||||||
|
id serial primary key,
|
||||||
|
name varchar(100) not null unique
|
||||||
|
);
|
||||||
|
|
||||||
|
insert into category (name) values
|
||||||
|
('Discussion'),
|
||||||
|
('Humor/Memes'),
|
||||||
|
('Gaming'),
|
||||||
|
('Movies'),
|
||||||
|
('TV'),
|
||||||
|
('Music'),
|
||||||
|
('Literature'),
|
||||||
|
('Comics'),
|
||||||
|
('Photography'),
|
||||||
|
('Art'),
|
||||||
|
('Learning'),
|
||||||
|
('DIY'),
|
||||||
|
('Lifestyle'),
|
||||||
|
('News'),
|
||||||
|
('Politics'),
|
||||||
|
('Society'),
|
||||||
|
('Gender/Identity/Sexuality'),
|
||||||
|
('Race/Colonisation'),
|
||||||
|
('Religion'),
|
||||||
|
('Science/Technology'),
|
||||||
|
('Programming/Software'),
|
||||||
|
('Health/Sports/Fitness'),
|
||||||
|
('Porn'),
|
||||||
|
('Places'),
|
||||||
|
('Meta'),
|
||||||
|
('Other');
|
||||||
|
|
||||||
|
ALTER TABLE community ADD category_id int references category on update cascade on delete cascade not null;
|
@ -0,0 +1,2 @@
|
|||||||
|
ALTER TABLE community DROP COLUMN category_id;
|
||||||
|
DROP TABLE category;
|
Loading…
Reference in New Issue