2022-11-09 10:05:00 +00:00
|
|
|
use crate::{
|
|
|
|
newtypes::LocalUserId,
|
2022-11-19 04:33:54 +00:00
|
|
|
schema::registration_application::dsl::{local_user_id, registration_application},
|
|
|
|
source::registration_application::{
|
|
|
|
RegistrationApplication,
|
|
|
|
RegistrationApplicationInsertForm,
|
|
|
|
RegistrationApplicationUpdateForm,
|
|
|
|
},
|
2022-11-09 10:05:00 +00:00
|
|
|
traits::Crud,
|
|
|
|
utils::{get_conn, DbPool},
|
|
|
|
};
|
|
|
|
use diesel::{insert_into, result::Error, ExpressionMethods, QueryDsl};
|
|
|
|
use diesel_async::RunQueryDsl;
|
2021-12-15 19:49:59 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
#[async_trait]
|
2021-12-15 19:49:59 +00:00
|
|
|
impl Crud for RegistrationApplication {
|
2022-10-27 09:24:07 +00:00
|
|
|
type InsertForm = RegistrationApplicationInsertForm;
|
|
|
|
type UpdateForm = RegistrationApplicationUpdateForm;
|
2021-12-15 19:49:59 +00:00
|
|
|
type IdType = i32;
|
2022-11-09 10:05:00 +00:00
|
|
|
|
|
|
|
async fn create(pool: &DbPool, form: &Self::InsertForm) -> Result<Self, Error> {
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
2021-12-15 19:49:59 +00:00
|
|
|
insert_into(registration_application)
|
|
|
|
.values(form)
|
|
|
|
.get_result::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2021-12-15 19:49:59 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn read(pool: &DbPool, id_: Self::IdType) -> Result<Self, Error> {
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
|
|
|
registration_application.find(id_).first::<Self>(conn).await
|
2021-12-15 19:49:59 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn update(
|
|
|
|
pool: &DbPool,
|
2022-10-27 09:24:07 +00:00
|
|
|
id_: Self::IdType,
|
|
|
|
form: &Self::UpdateForm,
|
|
|
|
) -> Result<Self, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2021-12-15 19:49:59 +00:00
|
|
|
diesel::update(registration_application.find(id_))
|
|
|
|
.set(form)
|
|
|
|
.get_result::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2021-12-15 19:49:59 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn delete(pool: &DbPool, id_: Self::IdType) -> Result<usize, Error> {
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
|
|
|
diesel::delete(registration_application.find(id_))
|
|
|
|
.execute(conn)
|
|
|
|
.await
|
2021-12-15 19:49:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RegistrationApplication {
|
2022-11-09 10:05:00 +00:00
|
|
|
pub async fn find_by_local_user_id(
|
|
|
|
pool: &DbPool,
|
2021-12-15 19:49:59 +00:00
|
|
|
local_user_id_: LocalUserId,
|
|
|
|
) -> Result<Self, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2021-12-15 19:49:59 +00:00
|
|
|
registration_application
|
|
|
|
.filter(local_user_id.eq(local_user_id_))
|
|
|
|
.first::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2021-12-15 19:49:59 +00:00
|
|
|
}
|
|
|
|
}
|