2022-11-09 10:05:00 +00:00
|
|
|
use crate::{
|
2022-11-19 04:33:54 +00:00
|
|
|
schema::local_site::dsl::local_site,
|
2023-01-05 01:42:30 +00:00
|
|
|
source::local_site::{
|
|
|
|
LocalSite,
|
|
|
|
LocalSiteInsertForm,
|
|
|
|
LocalSiteUpdateForm,
|
|
|
|
RegistrationMode,
|
|
|
|
RegistrationModeType,
|
|
|
|
},
|
2022-11-09 10:05:00 +00:00
|
|
|
utils::{get_conn, DbPool},
|
|
|
|
};
|
2023-01-05 01:42:30 +00:00
|
|
|
use diesel::{
|
|
|
|
deserialize,
|
|
|
|
deserialize::FromSql,
|
|
|
|
dsl::insert_into,
|
|
|
|
pg::{Pg, PgValue},
|
|
|
|
result::Error,
|
|
|
|
serialize,
|
|
|
|
serialize::{IsNull, Output, ToSql},
|
|
|
|
};
|
2022-11-09 10:05:00 +00:00
|
|
|
use diesel_async::RunQueryDsl;
|
2023-01-05 01:42:30 +00:00
|
|
|
use std::io::Write;
|
2022-10-27 09:24:07 +00:00
|
|
|
|
|
|
|
impl LocalSite {
|
2022-11-09 10:05:00 +00:00
|
|
|
pub async fn create(pool: &DbPool, form: &LocalSiteInsertForm) -> Result<Self, Error> {
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
2022-10-27 09:24:07 +00:00
|
|
|
insert_into(local_site)
|
|
|
|
.values(form)
|
|
|
|
.get_result::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2022-10-27 09:24:07 +00:00
|
|
|
}
|
2022-11-09 10:05:00 +00:00
|
|
|
pub async fn read(pool: &DbPool) -> Result<Self, Error> {
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
|
|
|
local_site.first::<Self>(conn).await
|
2022-10-27 09:24:07 +00:00
|
|
|
}
|
2022-11-09 10:05:00 +00:00
|
|
|
pub async fn update(pool: &DbPool, form: &LocalSiteUpdateForm) -> Result<Self, Error> {
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
2022-10-27 09:24:07 +00:00
|
|
|
diesel::update(local_site)
|
|
|
|
.set(form)
|
|
|
|
.get_result::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2022-10-27 09:24:07 +00:00
|
|
|
}
|
2022-11-09 10:05:00 +00:00
|
|
|
pub async fn delete(pool: &DbPool) -> Result<usize, Error> {
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
|
|
|
diesel::delete(local_site).execute(conn).await
|
2022-10-27 09:24:07 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-05 01:42:30 +00:00
|
|
|
|
|
|
|
impl ToSql<RegistrationModeType, Pg> for RegistrationMode {
|
|
|
|
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
|
|
|
|
match *self {
|
|
|
|
RegistrationMode::Closed => out.write_all(b"closed")?,
|
|
|
|
RegistrationMode::RequireApplication => out.write_all(b"require_application")?,
|
|
|
|
RegistrationMode::Open => out.write_all(b"open")?,
|
|
|
|
}
|
|
|
|
Ok(IsNull::No)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromSql<RegistrationModeType, Pg> for RegistrationMode {
|
|
|
|
fn from_sql(bytes: PgValue<'_>) -> deserialize::Result<Self> {
|
|
|
|
match bytes.as_bytes() {
|
|
|
|
b"closed" => Ok(RegistrationMode::Closed),
|
|
|
|
b"require_application" => Ok(RegistrationMode::RequireApplication),
|
|
|
|
b"open" => Ok(RegistrationMode::Open),
|
|
|
|
_ => Err("Unrecognized enum variant".into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|