lemmy/crates/db_schema/src/impls/registration_application.rs
dullbananas 91834d0d21
Default imprementations for read and delete in Crud trait (#3707)
* h

* Start doing stuff

* Default impl for Crud::read

* Simplify Crud::read lifetimes

* fmt

* Stuff

* Stuff

* Successfully make default read implementation work

* Restore Person::read

* Clean up default Crud::read and rename 'query2

* Replace filter with find

* Attempt default Crud::create

* Change Crud to Crud<'a> (won't compile)

* Revert "Change Crud to Crud<'a> (won't compile)"

This reverts commit 7ed20f5f71.

* Default Crud::delete

* Remove Crud::delete definitions that match default

* Remove commented Site::read

* Insert trait

* Revert "Insert trait"

This reverts commit 9d780c2403.

* Use non-borrowed forms

* Revert "Use non-borrowed forms"

This reverts commit d2dd442563.

* Revert "Revert "Change Crud to Crud<'a> (won't compile)""

This reverts commit 25a27165a8.

* Fix lifetime for everything except Crud::delete

* Fix Crud::delete

* Add comment about futures

* Attempt Crud::create

* Attempt separate CrudBounds

* Revert "Attempt separate CrudBounds"

This reverts commit 1b4ca321c3.

* Try to fix Crud::create

* Move lifetime parameters to associated types

* Revert "Move lifetime parameters to associated types"

This reverts commit af1bc858ce.

* Revert "Try to fix Crud::create"

This reverts commit eec238496c.

* Revert "Revert "Attempt separate CrudBounds""

This reverts commit 1ec33ce502.

* Revert "Attempt separate CrudBounds"

This reverts commit 1b4ca321c3.

* Revert "Attempt Crud::create"

This reverts commit 47e8071b68.

* Revert "Add comment about futures"

This reverts commit b266b14653.

* Revert "Fix Crud::delete"

This reverts commit 3abcce2eec.

* Revert "Fix lifetime for everything except Crud::delete"

This reverts commit c1ad7a161b.

* Revert "Revert "Revert "Change Crud to Crud<'a> (won't compile)"""

This reverts commit 3129cd0fc3.

* Clean up

* Update site.rs
2023-08-01 10:34:10 -04:00

53 lines
1.4 KiB
Rust

use crate::{
newtypes::LocalUserId,
schema::registration_application::dsl::{local_user_id, registration_application},
source::registration_application::{
RegistrationApplication,
RegistrationApplicationInsertForm,
RegistrationApplicationUpdateForm,
},
traits::Crud,
utils::{get_conn, DbPool},
};
use diesel::{insert_into, result::Error, ExpressionMethods, QueryDsl};
use diesel_async::RunQueryDsl;
#[async_trait]
impl Crud for RegistrationApplication {
type InsertForm = RegistrationApplicationInsertForm;
type UpdateForm = RegistrationApplicationUpdateForm;
type IdType = i32;
async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
insert_into(registration_application)
.values(form)
.get_result::<Self>(conn)
.await
}
async fn update(
pool: &mut DbPool<'_>,
id_: Self::IdType,
form: &Self::UpdateForm,
) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
diesel::update(registration_application.find(id_))
.set(form)
.get_result::<Self>(conn)
.await
}
}
impl RegistrationApplication {
pub async fn find_by_local_user_id(
pool: &mut DbPool<'_>,
local_user_id_: LocalUserId,
) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
registration_application
.filter(local_user_id.eq(local_user_id_))
.first::<Self>(conn)
.await
}
}