2022-10-06 18:27:58 +00:00
|
|
|
use crate::{
|
|
|
|
newtypes::{DbUrl, SiteId},
|
|
|
|
source::{actor_language::SiteLanguage, site::*},
|
|
|
|
traits::Crud,
|
|
|
|
};
|
2020-05-16 14:04:08 +00:00
|
|
|
use diesel::{dsl::*, result::Error, *};
|
2022-02-07 19:23:12 +00:00
|
|
|
use url::Url;
|
2019-12-11 20:21:47 +00:00
|
|
|
|
2021-08-17 18:04:58 +00:00
|
|
|
impl Crud for Site {
|
|
|
|
type Form = SiteForm;
|
2022-10-06 18:27:58 +00:00
|
|
|
type IdType = SiteId;
|
|
|
|
fn read(conn: &mut PgConnection, _site_id: SiteId) -> Result<Self, Error> {
|
2021-10-16 13:33:38 +00:00
|
|
|
use crate::schema::site::dsl::*;
|
2019-12-11 20:21:47 +00:00
|
|
|
site.first::<Self>(conn)
|
|
|
|
}
|
|
|
|
|
2022-09-26 14:09:32 +00:00
|
|
|
fn create(conn: &mut PgConnection, new_site: &SiteForm) -> Result<Self, Error> {
|
2021-10-16 13:33:38 +00:00
|
|
|
use crate::schema::site::dsl::*;
|
2022-10-06 18:27:58 +00:00
|
|
|
let site_ = insert_into(site)
|
|
|
|
.values(new_site)
|
|
|
|
.get_result::<Self>(conn)?;
|
|
|
|
|
|
|
|
// initialize with all languages
|
|
|
|
SiteLanguage::update(conn, vec![], site_.id)?;
|
|
|
|
Ok(site_)
|
2019-12-11 20:21:47 +00:00
|
|
|
}
|
|
|
|
|
2022-10-06 18:27:58 +00:00
|
|
|
fn update(conn: &mut PgConnection, site_id: SiteId, new_site: &SiteForm) -> Result<Self, Error> {
|
2021-10-16 13:33:38 +00:00
|
|
|
use crate::schema::site::dsl::*;
|
2019-12-11 20:21:47 +00:00
|
|
|
diesel::update(site.find(site_id))
|
|
|
|
.set(new_site)
|
|
|
|
.get_result::<Self>(conn)
|
|
|
|
}
|
2022-10-06 18:27:58 +00:00
|
|
|
|
|
|
|
fn delete(conn: &mut PgConnection, site_id: SiteId) -> 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)
|
|
|
|
}
|
2019-12-11 20:21:47 +00:00
|
|
|
}
|
2020-08-05 16:03:46 +00:00
|
|
|
|
2021-10-16 13:33:38 +00:00
|
|
|
impl Site {
|
2022-10-06 18:27:58 +00:00
|
|
|
pub fn read_local(conn: &mut PgConnection) -> Result<Self, Error> {
|
2021-10-16 13:33:38 +00:00
|
|
|
use crate::schema::site::dsl::*;
|
2022-02-07 19:23:12 +00:00
|
|
|
site.order_by(id).first::<Self>(conn)
|
|
|
|
}
|
|
|
|
|
2022-09-26 14:09:32 +00:00
|
|
|
pub fn upsert(conn: &mut PgConnection, site_form: &SiteForm) -> Result<Site, Error> {
|
2022-02-07 19:23:12 +00:00
|
|
|
use crate::schema::site::dsl::*;
|
|
|
|
insert_into(site)
|
|
|
|
.values(site_form)
|
|
|
|
.on_conflict(actor_id)
|
|
|
|
.do_update()
|
|
|
|
.set(site_form)
|
|
|
|
.get_result::<Self>(conn)
|
|
|
|
}
|
|
|
|
|
2022-09-26 14:09:32 +00:00
|
|
|
pub fn read_from_apub_id(conn: &mut PgConnection, object_id: Url) -> Result<Option<Self>, Error> {
|
2022-02-07 19:23:12 +00:00
|
|
|
use crate::schema::site::dsl::*;
|
|
|
|
let object_id: DbUrl = object_id.into();
|
|
|
|
Ok(
|
|
|
|
site
|
|
|
|
.filter(actor_id.eq(object_id))
|
|
|
|
.first::<Site>(conn)
|
|
|
|
.ok()
|
|
|
|
.map(Into::into),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-09-26 14:09:32 +00:00
|
|
|
pub fn read_remote_sites(conn: &mut PgConnection) -> Result<Vec<Self>, Error> {
|
2022-02-07 19:23:12 +00:00
|
|
|
use crate::schema::site::dsl::*;
|
|
|
|
site.order_by(id).offset(1).get_results::<Self>(conn)
|
2020-12-20 01:10:47 +00:00
|
|
|
}
|
2020-08-05 16:03:46 +00:00
|
|
|
}
|