type InsertForm = CommentInsertForm;
type UpdateForm = CommentUpdateForm;
type IdType = CommentId;
- async fn read(pool: &mut DbPool<'_>, comment_id: CommentId) -> Result<Self, Error> {
- let conn = &mut get_conn(pool).await?;
- comment.find(comment_id).first::<Self>(conn).await
- }
-
- async fn delete(pool: &mut DbPool<'_>, comment_id: CommentId) -> Result<usize, Error> {
- let conn = &mut get_conn(pool).await?;
- diesel::delete(comment.find(comment_id)).execute(conn).await
- }
/// This is unimplemented, use [[Comment::create]]
async fn create(_pool: &mut DbPool<'_>, _comment_form: &Self::InsertForm) -> Result<Self, Error> {
type InsertForm = CommentReplyInsertForm;
type UpdateForm = CommentReplyUpdateForm;
type IdType = CommentReplyId;
- async fn read(pool: &mut DbPool<'_>, comment_reply_id: CommentReplyId) -> Result<Self, Error> {
- let conn = &mut get_conn(pool).await?;
- comment_reply
- .find(comment_reply_id)
- .first::<Self>(conn)
- .await
- }
async fn create(
pool: &mut DbPool<'_>,
type InsertForm = CommunityInsertForm;
type UpdateForm = CommunityUpdateForm;
type IdType = CommunityId;
- async fn read(pool: &mut DbPool<'_>, community_id: CommunityId) -> Result<Self, Error> {
- let conn = &mut get_conn(pool).await?;
- community::table
- .find(community_id)
- .first::<Self>(conn)
- .await
- }
-
- async fn delete(pool: &mut DbPool<'_>, community_id: CommunityId) -> Result<usize, Error> {
- let conn = &mut get_conn(pool).await?;
- diesel::delete(community::table.find(community_id))
- .execute(conn)
- .await
- }
async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error> {
let is_new_community = match &form.actor_id {
type InsertForm = LocalUserInsertForm;
type UpdateForm = LocalUserUpdateForm;
type IdType = LocalUserId;
- async fn read(pool: &mut DbPool<'_>, local_user_id: LocalUserId) -> Result<Self, Error> {
- let conn = &mut get_conn(pool).await?;
- local_user.find(local_user_id).first::<Self>(conn).await
- }
- async fn delete(pool: &mut DbPool<'_>, local_user_id: LocalUserId) -> Result<usize, Error> {
- let conn = &mut get_conn(pool).await?;
- diesel::delete(local_user.find(local_user_id))
- .execute(conn)
- .await
- }
+
async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
let mut form_with_encrypted_password = form.clone();
type InsertForm = ModRemovePostForm;
type UpdateForm = ModRemovePostForm;
type IdType = i32;
- async fn read(pool: &mut DbPool<'_>, from_id: i32) -> Result<Self, Error> {
- use crate::schema::mod_remove_post::dsl::mod_remove_post;
- let conn = &mut get_conn(pool).await?;
- mod_remove_post.find(from_id).first::<Self>(conn).await
- }
async fn create(pool: &mut DbPool<'_>, form: &ModRemovePostForm) -> Result<Self, Error> {
use crate::schema::mod_remove_post::dsl::mod_remove_post;
type InsertForm = ModLockPostForm;
type UpdateForm = ModLockPostForm;
type IdType = i32;
- async fn read(pool: &mut DbPool<'_>, from_id: i32) -> Result<Self, Error> {
- use crate::schema::mod_lock_post::dsl::mod_lock_post;
- let conn = &mut get_conn(pool).await?;
- mod_lock_post.find(from_id).first::<Self>(conn).await
- }
async fn create(pool: &mut DbPool<'_>, form: &ModLockPostForm) -> Result<Self, Error> {
use crate::schema::mod_lock_post::dsl::mod_lock_post;
type InsertForm = ModFeaturePostForm;
type UpdateForm = ModFeaturePostForm;
type IdType = i32;
- async fn read(pool: &mut DbPool<'_>, from_id: i32) -> Result<Self, Error> {
- use crate::schema::mod_feature_post::dsl::mod_feature_post;
- let conn = &mut get_conn(pool).await?;
- mod_feature_post.find(from_id).first::<Self>(conn).await
- }
async fn create(pool: &mut DbPool<'_>, form: &ModFeaturePostForm) -> Result<Self, Error> {
use crate::schema::mod_feature_post::dsl::mod_feature_post;
type InsertForm = ModRemoveCommentForm;
type UpdateForm = ModRemoveCommentForm;
type IdType = i32;
- async fn read(pool: &mut DbPool<'_>, from_id: i32) -> Result<Self, Error> {
- use crate::schema::mod_remove_comment::dsl::mod_remove_comment;
- let conn = &mut get_conn(pool).await?;
- mod_remove_comment.find(from_id).first::<Self>(conn).await
- }
async fn create(pool: &mut DbPool<'_>, form: &ModRemoveCommentForm) -> Result<Self, Error> {
use crate::schema::mod_remove_comment::dsl::mod_remove_comment;
type InsertForm = ModRemoveCommunityForm;
type UpdateForm = ModRemoveCommunityForm;
type IdType = i32;
- async fn read(pool: &mut DbPool<'_>, from_id: i32) -> Result<Self, Error> {
- use crate::schema::mod_remove_community::dsl::mod_remove_community;
- let conn = &mut get_conn(pool).await?;
- mod_remove_community.find(from_id).first::<Self>(conn).await
- }
async fn create(pool: &mut DbPool<'_>, form: &ModRemoveCommunityForm) -> Result<Self, Error> {
use crate::schema::mod_remove_community::dsl::mod_remove_community;
type InsertForm = ModBanFromCommunityForm;
type UpdateForm = ModBanFromCommunityForm;
type IdType = i32;
- async fn read(pool: &mut DbPool<'_>, from_id: i32) -> Result<Self, Error> {
- use crate::schema::mod_ban_from_community::dsl::mod_ban_from_community;
- let conn = &mut get_conn(pool).await?;
- mod_ban_from_community
- .find(from_id)
- .first::<Self>(conn)
- .await
- }
async fn create(pool: &mut DbPool<'_>, form: &ModBanFromCommunityForm) -> Result<Self, Error> {
use crate::schema::mod_ban_from_community::dsl::mod_ban_from_community;
type InsertForm = ModBanForm;
type UpdateForm = ModBanForm;
type IdType = i32;
- async fn read(pool: &mut DbPool<'_>, from_id: i32) -> Result<Self, Error> {
- use crate::schema::mod_ban::dsl::mod_ban;
- let conn = &mut get_conn(pool).await?;
- mod_ban.find(from_id).first::<Self>(conn).await
- }
async fn create(pool: &mut DbPool<'_>, form: &ModBanForm) -> Result<Self, Error> {
use crate::schema::mod_ban::dsl::mod_ban;
type UpdateForm = ModHideCommunityForm;
type IdType = i32;
- async fn read(pool: &mut DbPool<'_>, from_id: i32) -> Result<Self, Error> {
- use crate::schema::mod_hide_community::dsl::mod_hide_community;
- let conn = &mut get_conn(pool).await?;
- mod_hide_community.find(from_id).first::<Self>(conn).await
- }
-
async fn create(pool: &mut DbPool<'_>, form: &ModHideCommunityForm) -> Result<Self, Error> {
use crate::schema::mod_hide_community::dsl::mod_hide_community;
let conn = &mut get_conn(pool).await?;
type InsertForm = ModAddCommunityForm;
type UpdateForm = ModAddCommunityForm;
type IdType = i32;
- async fn read(pool: &mut DbPool<'_>, from_id: i32) -> Result<Self, Error> {
- use crate::schema::mod_add_community::dsl::mod_add_community;
- let conn = &mut get_conn(pool).await?;
- mod_add_community.find(from_id).first::<Self>(conn).await
- }
async fn create(pool: &mut DbPool<'_>, form: &ModAddCommunityForm) -> Result<Self, Error> {
use crate::schema::mod_add_community::dsl::mod_add_community;
type InsertForm = ModTransferCommunityForm;
type UpdateForm = ModTransferCommunityForm;
type IdType = i32;
- async fn read(pool: &mut DbPool<'_>, from_id: i32) -> Result<Self, Error> {
- use crate::schema::mod_transfer_community::dsl::mod_transfer_community;
- let conn = &mut get_conn(pool).await?;
- mod_transfer_community
- .find(from_id)
- .first::<Self>(conn)
- .await
- }
async fn create(pool: &mut DbPool<'_>, form: &ModTransferCommunityForm) -> Result<Self, Error> {
use crate::schema::mod_transfer_community::dsl::mod_transfer_community;
type InsertForm = ModAddForm;
type UpdateForm = ModAddForm;
type IdType = i32;
- async fn read(pool: &mut DbPool<'_>, from_id: i32) -> Result<Self, Error> {
- use crate::schema::mod_add::dsl::mod_add;
- let conn = &mut get_conn(pool).await?;
- mod_add.find(from_id).first::<Self>(conn).await
- }
async fn create(pool: &mut DbPool<'_>, form: &ModAddForm) -> Result<Self, Error> {
use crate::schema::mod_add::dsl::mod_add;
type InsertForm = AdminPurgePersonForm;
type UpdateForm = AdminPurgePersonForm;
type IdType = i32;
- async fn read(pool: &mut DbPool<'_>, from_id: i32) -> Result<Self, Error> {
- use crate::schema::admin_purge_person::dsl::admin_purge_person;
- let conn = &mut get_conn(pool).await?;
- admin_purge_person.find(from_id).first::<Self>(conn).await
- }
async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error> {
use crate::schema::admin_purge_person::dsl::admin_purge_person;
type InsertForm = AdminPurgeCommunityForm;
type UpdateForm = AdminPurgeCommunityForm;
type IdType = i32;
- async fn read(pool: &mut DbPool<'_>, from_id: i32) -> Result<Self, Error> {
- use crate::schema::admin_purge_community::dsl::admin_purge_community;
- let conn = &mut get_conn(pool).await?;
- admin_purge_community
- .find(from_id)
- .first::<Self>(conn)
- .await
- }
async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error> {
use crate::schema::admin_purge_community::dsl::admin_purge_community;
type InsertForm = AdminPurgePostForm;
type UpdateForm = AdminPurgePostForm;
type IdType = i32;
- async fn read(pool: &mut DbPool<'_>, from_id: i32) -> Result<Self, Error> {
- use crate::schema::admin_purge_post::dsl::admin_purge_post;
- let conn = &mut get_conn(pool).await?;
- admin_purge_post.find(from_id).first::<Self>(conn).await
- }
async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error> {
use crate::schema::admin_purge_post::dsl::admin_purge_post;
type InsertForm = AdminPurgeCommentForm;
type UpdateForm = AdminPurgeCommentForm;
type IdType = i32;
- async fn read(pool: &mut DbPool<'_>, from_id: i32) -> Result<Self, Error> {
- use crate::schema::admin_purge_comment::dsl::admin_purge_comment;
- let conn = &mut get_conn(pool).await?;
- admin_purge_comment.find(from_id).first::<Self>(conn).await
- }
async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error> {
use crate::schema::admin_purge_comment::dsl::admin_purge_comment;
type InsertForm = PasswordResetRequestForm;
type UpdateForm = PasswordResetRequestForm;
type IdType = i32;
- async fn read(pool: &mut DbPool<'_>, password_reset_request_id: i32) -> Result<Self, Error> {
- let conn = &mut get_conn(pool).await?;
- password_reset_request
- .find(password_reset_request_id)
- .first::<Self>(conn)
- .await
- }
+
async fn create(pool: &mut DbPool<'_>, form: &PasswordResetRequestForm) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
insert_into(password_reset_request)
.first::<Self>(conn)
.await
}
- async fn delete(pool: &mut DbPool<'_>, person_id: PersonId) -> Result<usize, Error> {
- let conn = &mut get_conn(pool).await?;
- diesel::delete(person::table.find(person_id))
- .execute(conn)
- .await
- }
+
async fn create(pool: &mut DbPool<'_>, form: &PersonInsertForm) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
insert_into(person::table)
type InsertForm = PersonMentionInsertForm;
type UpdateForm = PersonMentionUpdateForm;
type IdType = PersonMentionId;
- async fn read(pool: &mut DbPool<'_>, person_mention_id: PersonMentionId) -> Result<Self, Error> {
- let conn = &mut get_conn(pool).await?;
- person_mention
- .find(person_mention_id)
- .first::<Self>(conn)
- .await
- }
async fn create(
pool: &mut DbPool<'_>,
type InsertForm = PostInsertForm;
type UpdateForm = PostUpdateForm;
type IdType = PostId;
- async fn read(pool: &mut DbPool<'_>, post_id: PostId) -> Result<Self, Error> {
- let conn = &mut get_conn(pool).await?;
- post.find(post_id).first::<Self>(conn).await
- }
-
- async fn delete(pool: &mut DbPool<'_>, post_id: PostId) -> Result<usize, Error> {
- let conn = &mut get_conn(pool).await?;
- diesel::delete(post.find(post_id)).execute(conn).await
- }
async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
type InsertForm = PrivateMessageInsertForm;
type UpdateForm = PrivateMessageUpdateForm;
type IdType = PrivateMessageId;
- async fn read(
- pool: &mut DbPool<'_>,
- private_message_id: PrivateMessageId,
- ) -> Result<Self, Error> {
- let conn = &mut get_conn(pool).await?;
- private_message
- .find(private_message_id)
- .first::<Self>(conn)
- .await
- }
async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
.get_result::<Self>(conn)
.await
}
- async fn delete(pool: &mut DbPool<'_>, pm_id: Self::IdType) -> Result<usize, Error> {
- let conn = &mut get_conn(pool).await?;
- diesel::delete(private_message.find(pm_id))
- .execute(conn)
- .await
- }
}
impl PrivateMessage {
.await
}
- async fn read(pool: &mut DbPool<'_>, id_: Self::IdType) -> Result<Self, Error> {
- let conn = &mut get_conn(pool).await?;
- registration_application.find(id_).first::<Self>(conn).await
- }
-
async fn update(
pool: &mut DbPool<'_>,
id_: Self::IdType,
.get_result::<Self>(conn)
.await
}
-
- async fn delete(pool: &mut DbPool<'_>, id_: Self::IdType) -> Result<usize, Error> {
- let conn = &mut get_conn(pool).await?;
- diesel::delete(registration_application.find(id_))
- .execute(conn)
- .await
- }
}
impl RegistrationApplication {
.get_result::<Self>(conn)
.await
}
-
- async fn delete(pool: &mut DbPool<'_>, site_id: SiteId) -> Result<usize, Error> {
- let conn = &mut get_conn(pool).await?;
- diesel::delete(site.find(site_id)).execute(conn).await
- }
}
impl Site {
use crate::{
newtypes::{CommunityId, DbUrl, PersonId},
- utils::DbPool,
+ utils::{get_conn, DbPool},
};
-use diesel::result::Error;
+use diesel::{
+ associations::HasTable,
+ dsl,
+ query_builder::{DeleteStatement, IntoUpdateTarget},
+ query_dsl::methods::{FindDsl, LimitDsl},
+ result::Error,
+ Table,
+};
+use diesel_async::{
+ methods::{ExecuteDsl, LoadQuery},
+ AsyncPgConnection,
+ RunQueryDsl,
+};
+
+/// Returned by `diesel::delete`
+pub type Delete<T> = DeleteStatement<<T as HasTable>::Table, <T as IntoUpdateTarget>::WhereClause>;
+
+/// Returned by `Self::table().find(id)`
+pub type Find<T> = dsl::Find<<T as HasTable>::Table, <T as Crud>::IdType>;
+
+pub type PrimaryKey<T> = <<T as HasTable>::Table as Table>::PrimaryKey;
+// Trying to create default implementations for `create` and `update` results in a lifetime mess and weird compile errors.
+// https://github.com/rust-lang/rust/issues/102211
#[async_trait]
-pub trait Crud {
+pub trait Crud: HasTable + Sized
+where
+ Self::Table: FindDsl<Self::IdType>,
+ Find<Self>: LimitDsl + IntoUpdateTarget + Send,
+ Delete<Find<Self>>: ExecuteDsl<AsyncPgConnection> + Send + 'static,
+
+ // Used by `RunQueryDsl::first`
+ dsl::Limit<Find<Self>>: LoadQuery<'static, AsyncPgConnection, Self> + Send + 'static,
+{
type InsertForm;
type UpdateForm;
- type IdType;
- async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error>
- where
- Self: Sized;
- async fn read(pool: &mut DbPool<'_>, id: Self::IdType) -> Result<Self, Error>
- where
- Self: Sized;
+ type IdType: Send;
+
+ async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error>;
+
+ async fn read(pool: &mut DbPool<'_>, id: Self::IdType) -> Result<Self, Error> {
+ let query: Find<Self> = Self::table().find(id);
+ let conn = &mut *get_conn(pool).await?;
+ query.first::<Self>(conn).await
+ }
+
/// 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: &mut DbPool<'_>,
id: Self::IdType,
form: &Self::UpdateForm,
- ) -> Result<Self, Error>
- where
- Self: Sized;
- async fn delete(_pool: &mut DbPool<'_>, _id: Self::IdType) -> Result<usize, Error>
- where
- Self: Sized,
- Self::IdType: Send,
- {
- async { Err(Error::NotFound) }.await
+ ) -> Result<Self, Error>;
+
+ async fn delete(pool: &mut DbPool<'_>, id: Self::IdType) -> Result<usize, Error> {
+ let query: Delete<Find<Self>> = diesel::delete(Self::table().find(id));
+ let conn = &mut *get_conn(pool).await?;
+ query.execute(conn).await
}
}