use crate::{ newtypes::{DbUrl, SiteId}, source::{actor_language::SiteLanguage, site::*}, traits::Crud, }; use diesel::{dsl::*, result::Error, *}; use url::Url; impl Crud for Site { type Form = SiteForm; type IdType = SiteId; fn read(conn: &mut PgConnection, _site_id: SiteId) -> Result { use crate::schema::site::dsl::*; site.first::(conn) } fn create(conn: &mut PgConnection, new_site: &SiteForm) -> Result { use crate::schema::site::dsl::*; let site_ = insert_into(site) .values(new_site) .get_result::(conn)?; // initialize with all languages SiteLanguage::update(conn, vec![], site_.id)?; Ok(site_) } fn update(conn: &mut PgConnection, site_id: SiteId, new_site: &SiteForm) -> Result { use crate::schema::site::dsl::*; diesel::update(site.find(site_id)) .set(new_site) .get_result::(conn) } fn delete(conn: &mut PgConnection, site_id: SiteId) -> Result { use crate::schema::site::dsl::*; diesel::delete(site.find(site_id)).execute(conn) } } impl Site { pub fn read_local(conn: &mut PgConnection) -> Result { use crate::schema::site::dsl::*; site.order_by(id).first::(conn) } pub fn upsert(conn: &mut PgConnection, site_form: &SiteForm) -> Result { use crate::schema::site::dsl::*; insert_into(site) .values(site_form) .on_conflict(actor_id) .do_update() .set(site_form) .get_result::(conn) } pub fn read_from_apub_id(conn: &mut PgConnection, object_id: Url) -> Result, Error> { use crate::schema::site::dsl::*; let object_id: DbUrl = object_id.into(); Ok( site .filter(actor_id.eq(object_id)) .first::(conn) .ok() .map(Into::into), ) } pub fn read_remote_sites(conn: &mut PgConnection) -> Result, Error> { use crate::schema::site::dsl::*; site.order_by(id).offset(1).get_results::(conn) } }