2020-08-05 16:03:46 +00:00
|
|
|
use crate::{naive_now, schema::site, Crud};
|
2020-05-16 14:04:08 +00:00
|
|
|
use diesel::{dsl::*, result::Error, *};
|
2020-12-02 19:32:47 +00:00
|
|
|
use serde::Serialize;
|
2019-12-11 20:21:47 +00:00
|
|
|
|
2020-12-02 19:32:47 +00:00
|
|
|
#[derive(Queryable, Identifiable, PartialEq, Debug, Clone, Serialize)]
|
2019-12-11 20:21:47 +00:00
|
|
|
#[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,
|
2020-08-05 16:03:46 +00:00
|
|
|
pub icon: Option<String>,
|
|
|
|
pub banner: Option<String>,
|
2019-12-11 20:21:47 +00:00
|
|
|
}
|
|
|
|
|
2020-09-03 19:45:12 +00:00
|
|
|
#[derive(Insertable, AsChangeset)]
|
2019-12-11 20:21:47 +00:00
|
|
|
#[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,
|
2020-08-05 16:03:46 +00:00
|
|
|
// when you want to null out a column, you have to send Some(None)), since sending None means you just don't want to update that column.
|
|
|
|
pub icon: Option<Option<String>>,
|
|
|
|
pub banner: Option<Option<String>>,
|
2019-12-11 20:21:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 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)
|
|
|
|
}
|
|
|
|
}
|
2020-08-05 16:03:46 +00:00
|
|
|
|
|
|
|
impl Site {
|
|
|
|
pub fn transfer(conn: &PgConnection, new_creator_id: i32) -> Result<Self, Error> {
|
|
|
|
use crate::schema::site::dsl::*;
|
|
|
|
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
|
|
|
|
|
|
|
pub fn read_simple(conn: &PgConnection) -> Result<Self, Error> {
|
|
|
|
use crate::schema::site::dsl::*;
|
|
|
|
site.first::<Self>(conn)
|
|
|
|
}
|
2020-08-05 16:03:46 +00:00
|
|
|
}
|