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