]> Untitled Git - lemmy.git/blob - server/src/db/site.rs
Merge remote-tracking branch 'upstream/master'
[lemmy.git] / server / src / db / site.rs
1 use super::*;
2 use crate::schema::site;
3
4 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
5 #[table_name = "site"]
6 pub struct Site {
7   pub id: i32,
8   pub name: String,
9   pub description: Option<String>,
10   pub creator_id: i32,
11   pub published: chrono::NaiveDateTime,
12   pub updated: Option<chrono::NaiveDateTime>,
13   pub enable_downvotes: bool,
14   pub open_registration: bool,
15   pub enable_nsfw: bool,
16 }
17
18 #[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
19 #[table_name = "site"]
20 pub struct SiteForm {
21   pub name: String,
22   pub description: Option<String>,
23   pub creator_id: i32,
24   pub updated: Option<chrono::NaiveDateTime>,
25   pub enable_downvotes: bool,
26   pub open_registration: bool,
27   pub enable_nsfw: bool,
28 }
29
30 impl Crud<SiteForm> for Site {
31   fn read(conn: &PgConnection, _site_id: i32) -> Result<Self, Error> {
32     use crate::schema::site::dsl::*;
33     site.first::<Self>(conn)
34   }
35
36   fn delete(conn: &PgConnection, site_id: i32) -> Result<usize, Error> {
37     use crate::schema::site::dsl::*;
38     diesel::delete(site.find(site_id)).execute(conn)
39   }
40
41   fn create(conn: &PgConnection, new_site: &SiteForm) -> Result<Self, Error> {
42     use crate::schema::site::dsl::*;
43     insert_into(site).values(new_site).get_result::<Self>(conn)
44   }
45
46   fn update(conn: &PgConnection, site_id: i32, new_site: &SiteForm) -> Result<Self, Error> {
47     use crate::schema::site::dsl::*;
48     diesel::update(site.find(site_id))
49       .set(new_site)
50       .get_result::<Self>(conn)
51   }
52 }