2020-12-18 17:27:25 +00:00
|
|
|
use crate::Crud;
|
2020-05-16 14:04:08 +00:00
|
|
|
use diesel::{dsl::*, result::Error, *};
|
2020-12-21 13:38:34 +00:00
|
|
|
use lemmy_db_schema::{naive_now, source::site::*};
|
2019-12-11 20:21:47 +00:00
|
|
|
|
|
|
|
impl Crud<SiteForm> for Site {
|
|
|
|
fn read(conn: &PgConnection, _site_id: i32) -> Result<Self, Error> {
|
2020-12-18 16:17:21 +00:00
|
|
|
use lemmy_db_schema::schema::site::dsl::*;
|
2019-12-11 20:21:47 +00:00
|
|
|
site.first::<Self>(conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create(conn: &PgConnection, new_site: &SiteForm) -> Result<Self, Error> {
|
2020-12-18 16:17:21 +00:00
|
|
|
use lemmy_db_schema::schema::site::dsl::*;
|
2019-12-11 20:21:47 +00:00
|
|
|
insert_into(site).values(new_site).get_result::<Self>(conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update(conn: &PgConnection, site_id: i32, new_site: &SiteForm) -> Result<Self, Error> {
|
2020-12-18 16:17:21 +00:00
|
|
|
use lemmy_db_schema::schema::site::dsl::*;
|
2019-12-11 20:21:47 +00:00
|
|
|
diesel::update(site.find(site_id))
|
|
|
|
.set(new_site)
|
|
|
|
.get_result::<Self>(conn)
|
|
|
|
}
|
2020-12-20 05:09:20 +00:00
|
|
|
fn delete(conn: &PgConnection, site_id: i32) -> Result<usize, Error> {
|
2020-12-21 14:28:20 +00:00
|
|
|
use lemmy_db_schema::schema::site::dsl::*;
|
2020-12-20 05:09:20 +00:00
|
|
|
diesel::delete(site.find(site_id)).execute(conn)
|
|
|
|
}
|
2019-12-11 20:21:47 +00:00
|
|
|
}
|
2020-08-05 16:03:46 +00:00
|
|
|
|
2020-12-21 13:38:34 +00:00
|
|
|
pub trait Site_ {
|
|
|
|
fn transfer(conn: &PgConnection, new_creator_id: i32) -> Result<Site, Error>;
|
2020-12-21 14:28:20 +00:00
|
|
|
fn read_simple(conn: &PgConnection) -> Result<Site, Error>;
|
2020-12-21 13:38:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Site_ for Site {
|
|
|
|
fn transfer(conn: &PgConnection, new_creator_id: i32) -> Result<Site, Error> {
|
2020-12-18 16:17:21 +00:00
|
|
|
use lemmy_db_schema::schema::site::dsl::*;
|
2020-08-05 16:03:46 +00:00
|
|
|
diesel::update(site.find(1))
|
|
|
|
.set((creator_id.eq(new_creator_id), updated.eq(naive_now())))
|
|
|
|
.get_result::<Self>(conn)
|
|
|
|
}
|
2020-12-20 01:10:47 +00:00
|
|
|
|
2020-12-21 14:28:20 +00:00
|
|
|
fn read_simple(conn: &PgConnection) -> Result<Self, Error> {
|
|
|
|
use lemmy_db_schema::schema::site::dsl::*;
|
2020-12-20 01:10:47 +00:00
|
|
|
site.first::<Self>(conn)
|
|
|
|
}
|
2020-08-05 16:03:46 +00:00
|
|
|
}
|