diff options
author | Dessalines <tyhou13@gmx.com> | 2020-07-11 19:12:56 -0400 |
---|---|---|
committer | Dessalines <tyhou13@gmx.com> | 2020-07-11 19:12:56 -0400 |
commit | 60288b2d060ba930fe6cae22c4824d88fe7a00c9 (patch) | |
tree | 8fed01325853240c69f3688ee621be29bedfd382 /server/lemmy_db/src/site.rs | |
parent | 1710844a1bc6a4f46eceaa12f2fb428cb794c694 (diff) | |
parent | 1b9f2fa5f7f7831f59b24cb36a5607a769a0d92e (diff) |
Merge branch 'master' into jmarthernandez-remove-karma-from-search
Diffstat (limited to 'server/lemmy_db/src/site.rs')
-rw-r--r-- | server/lemmy_db/src/site.rs | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/server/lemmy_db/src/site.rs b/server/lemmy_db/src/site.rs new file mode 100644 index 00000000..066ae0b1 --- /dev/null +++ b/server/lemmy_db/src/site.rs @@ -0,0 +1,53 @@ +use crate::{schema::site, Crud}; +use diesel::{dsl::*, result::Error, *}; +use serde::{Deserialize, Serialize}; + +#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)] +#[table_name = "site"] +pub struct Site { + pub id: i32, + pub name: String, + pub description: Option<String>, + pub creator_id: i32, + pub published: chrono::NaiveDateTime, + pub updated: Option<chrono::NaiveDateTime>, + pub enable_downvotes: bool, + pub open_registration: bool, + pub enable_nsfw: bool, +} + +#[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)] +#[table_name = "site"] +pub struct SiteForm { + pub name: String, + pub description: Option<String>, + pub creator_id: i32, + pub updated: Option<chrono::NaiveDateTime>, + pub enable_downvotes: bool, + pub open_registration: bool, + pub enable_nsfw: bool, +} + +impl Crud<SiteForm> for Site { + fn read(conn: &PgConnection, _site_id: i32) -> Result<Self, Error> { + use crate::schema::site::dsl::*; + site.first::<Self>(conn) + } + + fn delete(conn: &PgConnection, site_id: i32) -> Result<usize, Error> { + use crate::schema::site::dsl::*; + diesel::delete(site.find(site_id)).execute(conn) + } + + fn create(conn: &PgConnection, new_site: &SiteForm) -> Result<Self, Error> { + use crate::schema::site::dsl::*; + insert_into(site).values(new_site).get_result::<Self>(conn) + } + + fn update(conn: &PgConnection, site_id: i32, new_site: &SiteForm) -> Result<Self, Error> { + use crate::schema::site::dsl::*; + diesel::update(site.find(site_id)) + .set(new_site) + .get_result::<Self>(conn) + } +} |