This commit is contained in:
dull b 2023-07-11 18:00:09 +00:00
parent 73492af4b0
commit 239686be0b

View File

@ -1,28 +1,29 @@
use crate::{ use crate::{
newtypes::{CommunityId, DbUrl, PersonId}, newtypes::{CommunityId, DbUrl, PersonId},
utils::DbPool, utils::{DbPool,get_conn},
}; };
use diesel::result::Error; use diesel::{result::Error,Identifiable, associations::HasTable,query_dsl::methods::FindDsl};
#[async_trait] #[async_trait]
pub trait Crud { pub trait Crud where Self: Identifiable, Self::Table: FindDsl<Self::Id> {
type InsertForm; type InsertForm;
type UpdateForm; type UpdateForm;
type IdType;
async fn create(pool: &DbPool, form: &Self::InsertForm) -> Result<Self, Error> async fn create(pool: &DbPool, form: &Self::InsertForm) -> Result<Self, Error>
where where
Self: Sized; Self: Sized {
async fn read(pool: &DbPool, id: Self::IdType) -> Result<Self, Error>
};
async fn read(pool: &DbPool, id: Self::Id) -> Result<Self, Error>
where where
Self: Sized; Self: Sized;
/// when you want to null out a column, you have to send Some(None)), since sending None means you just don't want to update that column. /// when you want to null out a column, you have to send Some(None)), since sending None means you just don't want to update that column.
async fn update(pool: &DbPool, id: Self::IdType, form: &Self::UpdateForm) -> Result<Self, Error> async fn update(pool: &DbPool, id: Self::Id, form: &Self::UpdateForm) -> Result<Self, Error>
where where
Self: Sized; Self: Sized;
async fn delete(_pool: &DbPool, _id: Self::IdType) -> Result<usize, Error> async fn delete(_pool: &DbPool, _id: Self::Id) -> Result<usize, Error>
where where
Self: Sized, Self: Sized,
Self::IdType: Send, Self::Id: Send,
{ {
async { Err(Error::NotFound) }.await async { Err(Error::NotFound) }.await
} }
@ -67,7 +68,7 @@ pub trait Likeable {
async fn remove( async fn remove(
pool: &DbPool, pool: &DbPool,
person_id: PersonId, person_id: PersonId,
item_id: Self::IdType, item_id: Self::Id,
) -> Result<usize, Error> ) -> Result<usize, Error>
where where
Self: Sized; Self: Sized;
@ -126,14 +127,14 @@ pub trait Reportable {
Self: Sized; Self: Sized;
async fn resolve( async fn resolve(
pool: &DbPool, pool: &DbPool,
report_id: Self::IdType, report_id: Self::Id,
resolver_id: PersonId, resolver_id: PersonId,
) -> Result<usize, Error> ) -> Result<usize, Error>
where where
Self: Sized; Self: Sized;
async fn unresolve( async fn unresolve(
pool: &DbPool, pool: &DbPool,
report_id: Self::IdType, report_id: Self::Id,
resolver_id: PersonId, resolver_id: PersonId,
) -> Result<usize, Error> ) -> Result<usize, Error>
where where