lemmy/crates/db_schema/src/impls/site.rs

35 lines
1 KiB
Rust
Raw Normal View History

use crate::{source::site::*, traits::Crud};
2020-05-16 14:04:08 +00:00
use diesel::{dsl::*, result::Error, *};
impl Crud for Site {
type Form = SiteForm;
type IdType = i32;
fn read(conn: &PgConnection, _site_id: i32) -> Result<Self, Error> {
2021-10-16 13:33:38 +00:00
use crate::schema::site::dsl::*;
site.first::<Self>(conn)
}
fn create(conn: &PgConnection, new_site: &SiteForm) -> Result<Self, Error> {
2021-10-16 13:33:38 +00:00
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> {
2021-10-16 13:33:38 +00:00
use crate::schema::site::dsl::*;
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> {
2021-10-16 13:33:38 +00:00
use crate::schema::site::dsl::*;
2020-12-20 05:09:20 +00:00
diesel::delete(site.find(site_id)).execute(conn)
}
}
2021-10-16 13:33:38 +00:00
impl Site {
pub fn read_simple(conn: &PgConnection) -> Result<Self, Error> {
use crate::schema::site::dsl::*;
site.first::<Self>(conn)
}
}