Simplify db_queries traits by removing generics

This commit is contained in:
Felix Ableitner 2021-08-09 00:01:17 +02:00
parent dacf1e6229
commit 0670dc4565
14 changed files with 131 additions and 61 deletions

View file

@ -25,17 +25,19 @@ pub mod source;
pub type DbPool = diesel::r2d2::Pool<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;
pub trait Crud<Form, IdType> {
fn create(conn: &PgConnection, form: &Form) -> Result<Self, Error>
pub trait Crud {
type Form;
type IdType;
fn create(conn: &PgConnection, form: &Self::Form) -> Result<Self, Error>
where
Self: Sized;
fn read(conn: &PgConnection, id: IdType) -> Result<Self, Error>
fn read(conn: &PgConnection, id: Self::IdType) -> Result<Self, Error>
where
Self: Sized;
fn update(conn: &PgConnection, id: IdType, form: &Form) -> Result<Self, Error>
fn update(conn: &PgConnection, id: Self::IdType, form: &Self::Form) -> Result<Self, Error>
where
Self: Sized;
fn delete(_conn: &PgConnection, _id: IdType) -> Result<usize, Error>
fn delete(_conn: &PgConnection, _id: Self::IdType) -> Result<usize, Error>
where
Self: Sized,
{
@ -43,8 +45,9 @@ pub trait Crud<Form, IdType> {
}
}
pub trait Followable<Form> {
fn follow(conn: &PgConnection, form: &Form) -> Result<Self, Error>
pub trait Followable {
type Form;
fn follow(conn: &PgConnection, form: &Self::Form) -> Result<Self, Error>
where
Self: Sized;
fn follow_accepted(
@ -54,59 +57,70 @@ pub trait Followable<Form> {
) -> Result<Self, Error>
where
Self: Sized;
fn unfollow(conn: &PgConnection, form: &Form) -> Result<usize, Error>
fn unfollow(conn: &PgConnection, form: &Self::Form) -> Result<usize, Error>
where
Self: Sized;
fn has_local_followers(conn: &PgConnection, community_id: CommunityId) -> Result<bool, Error>;
}
pub trait Joinable<Form> {
fn join(conn: &PgConnection, form: &Form) -> Result<Self, Error>
pub trait Joinable {
type Form;
fn join(conn: &PgConnection, form: &Self::Form) -> Result<Self, Error>
where
Self: Sized;
fn leave(conn: &PgConnection, form: &Form) -> Result<usize, Error>
fn leave(conn: &PgConnection, form: &Self::Form) -> Result<usize, Error>
where
Self: Sized;
}
pub trait Likeable<Form, IdType> {
fn like(conn: &PgConnection, form: &Form) -> Result<Self, Error>
pub trait Likeable {
type Form;
type IdType;
fn like(conn: &PgConnection, form: &Self::Form) -> Result<Self, Error>
where
Self: Sized;
fn remove(conn: &PgConnection, person_id: PersonId, item_id: IdType) -> Result<usize, Error>
fn remove(
conn: &PgConnection,
person_id: PersonId,
item_id: Self::IdType,
) -> Result<usize, Error>
where
Self: Sized;
}
pub trait Bannable<Form> {
fn ban(conn: &PgConnection, form: &Form) -> Result<Self, Error>
pub trait Bannable {
type Form;
fn ban(conn: &PgConnection, form: &Self::Form) -> Result<Self, Error>
where
Self: Sized;
fn unban(conn: &PgConnection, form: &Form) -> Result<usize, Error>
fn unban(conn: &PgConnection, form: &Self::Form) -> Result<usize, Error>
where
Self: Sized;
}
pub trait Saveable<Form> {
fn save(conn: &PgConnection, form: &Form) -> Result<Self, Error>
pub trait Saveable {
type Form;
fn save(conn: &PgConnection, form: &Self::Form) -> Result<Self, Error>
where
Self: Sized;
fn unsave(conn: &PgConnection, form: &Form) -> Result<usize, Error>
fn unsave(conn: &PgConnection, form: &Self::Form) -> Result<usize, Error>
where
Self: Sized;
}
pub trait Readable<Form> {
fn mark_as_read(conn: &PgConnection, form: &Form) -> Result<Self, Error>
pub trait Readable {
type Form;
fn mark_as_read(conn: &PgConnection, form: &Self::Form) -> Result<Self, Error>
where
Self: Sized;
fn mark_as_unread(conn: &PgConnection, form: &Form) -> Result<usize, Error>
fn mark_as_unread(conn: &PgConnection, form: &Self::Form) -> Result<usize, Error>
where
Self: Sized;
}
pub trait Reportable<Form> {
fn report(conn: &PgConnection, form: &Form) -> Result<Self, Error>
pub trait Reportable {
type Form;
fn report(conn: &PgConnection, form: &Self::Form) -> Result<Self, Error>
where
Self: Sized;
fn resolve(conn: &PgConnection, report_id: i32, resolver_id: PersonId) -> Result<usize, Error>
@ -121,11 +135,12 @@ pub trait DeleteableOrRemoveable {
fn blank_out_deleted_or_removed_info(self) -> Self;
}
pub trait ApubObject<Form> {
pub trait ApubObject {
type Form;
fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Self, Error>
where
Self: Sized;
fn upsert(conn: &PgConnection, user_form: &Form) -> Result<Self, Error>
fn upsert(conn: &PgConnection, user_form: &Self::Form) -> Result<Self, Error>
where
Self: Sized;
}

View file

@ -9,7 +9,9 @@ use std::{
io::{Error as IoError, ErrorKind},
};
impl Crud<ActivityForm, i32> for Activity {
impl Crud for Activity {
type Form = ActivityForm;
type IdType = i32;
fn read(conn: &PgConnection, activity_id: i32) -> Result<Self, Error> {
use lemmy_db_schema::schema::activity::dsl::*;
activity.find(activity_id).first::<Self>(conn)

View file

@ -135,7 +135,9 @@ impl Comment_ for Comment {
}
}
impl Crud<CommentForm, CommentId> for Comment {
impl Crud for Comment {
type Form = CommentForm;
type IdType = CommentId;
fn read(conn: &PgConnection, comment_id: CommentId) -> Result<Self, Error> {
use lemmy_db_schema::schema::comment::dsl::*;
comment.find(comment_id).first::<Self>(conn)
@ -165,7 +167,8 @@ impl Crud<CommentForm, CommentId> for Comment {
}
}
impl ApubObject<CommentForm> for Comment {
impl ApubObject for Comment {
type Form = CommentForm;
fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Self, Error> {
use lemmy_db_schema::schema::comment::dsl::*;
comment.filter(ap_id.eq(object_id)).first::<Self>(conn)
@ -182,7 +185,9 @@ impl ApubObject<CommentForm> for Comment {
}
}
impl Likeable<CommentLikeForm, CommentId> for CommentLike {
impl Likeable for CommentLike {
type Form = CommentLikeForm;
type IdType = CommentId;
fn like(conn: &PgConnection, comment_like_form: &CommentLikeForm) -> Result<Self, Error> {
use lemmy_db_schema::schema::comment_like::dsl::*;
insert_into(comment_like)
@ -207,7 +212,8 @@ impl Likeable<CommentLikeForm, CommentId> for CommentLike {
}
}
impl Saveable<CommentSavedForm> for CommentSaved {
impl Saveable for CommentSaved {
type Form = CommentSavedForm;
fn save(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<Self, Error> {
use lemmy_db_schema::schema::comment_saved::dsl::*;
insert_into(comment_saved)

View file

@ -6,7 +6,8 @@ use lemmy_db_schema::{
PersonId,
};
impl Reportable<CommentReportForm> for CommentReport {
impl Reportable for CommentReport {
type Form = CommentReportForm;
/// creates a comment report and returns it
///
/// * `conn` - the postgres connection

View file

@ -60,7 +60,9 @@ mod safe_type {
}
}
impl Crud<CommunityForm, CommunityId> for Community {
impl Crud for Community {
type Form = CommunityForm;
type IdType = CommunityId;
fn read(conn: &PgConnection, community_id: CommunityId) -> Result<Self, Error> {
use lemmy_db_schema::schema::community::dsl::*;
community.find(community_id).first::<Self>(conn)
@ -90,7 +92,8 @@ impl Crud<CommunityForm, CommunityId> for Community {
}
}
impl ApubObject<CommunityForm> for Community {
impl ApubObject for Community {
type Form = CommunityForm;
fn read_from_apub_id(conn: &PgConnection, for_actor_id: &DbUrl) -> Result<Self, Error> {
use lemmy_db_schema::schema::community::dsl::*;
community
@ -175,7 +178,8 @@ impl Community_ for Community {
}
}
impl Joinable<CommunityModeratorForm> for CommunityModerator {
impl Joinable for CommunityModerator {
type Form = CommunityModeratorForm;
fn join(
conn: &PgConnection,
community_moderator_form: &CommunityModeratorForm,
@ -252,7 +256,8 @@ impl CommunityModerator_ for CommunityModerator {
}
}
impl Bannable<CommunityPersonBanForm> for CommunityPersonBan {
impl Bannable for CommunityPersonBan {
type Form = CommunityPersonBanForm;
fn ban(
conn: &PgConnection,
community_person_ban_form: &CommunityPersonBanForm,
@ -277,7 +282,8 @@ impl Bannable<CommunityPersonBanForm> for CommunityPersonBan {
}
}
impl Followable<CommunityFollowerForm> for CommunityFollower {
impl Followable for CommunityFollower {
type Form = CommunityFollowerForm;
fn follow(
conn: &PgConnection,
community_follower_form: &CommunityFollowerForm,

View file

@ -91,7 +91,9 @@ impl LocalUser_ for LocalUser {
}
}
impl Crud<LocalUserForm, LocalUserId> for LocalUser {
impl Crud for LocalUser {
type Form = LocalUserForm;
type IdType = LocalUserId;
fn read(conn: &PgConnection, local_user_id: LocalUserId) -> Result<Self, Error> {
local_user.find(local_user_id).first::<Self>(conn)
}

View file

@ -2,7 +2,9 @@ use crate::Crud;
use diesel::{dsl::*, result::Error, *};
use lemmy_db_schema::source::moderator::*;
impl Crud<ModRemovePostForm, i32> for ModRemovePost {
impl Crud for ModRemovePost {
type Form = ModRemovePostForm;
type IdType = i32;
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use lemmy_db_schema::schema::mod_remove_post::dsl::*;
mod_remove_post.find(from_id).first::<Self>(conn)
@ -23,7 +25,9 @@ impl Crud<ModRemovePostForm, i32> for ModRemovePost {
}
}
impl Crud<ModLockPostForm, i32> for ModLockPost {
impl Crud for ModLockPost {
type Form = ModLockPostForm;
type IdType = i32;
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use lemmy_db_schema::schema::mod_lock_post::dsl::*;
mod_lock_post.find(from_id).first::<Self>(conn)
@ -44,7 +48,9 @@ impl Crud<ModLockPostForm, i32> for ModLockPost {
}
}
impl Crud<ModStickyPostForm, i32> for ModStickyPost {
impl Crud for ModStickyPost {
type Form = ModStickyPostForm;
type IdType = i32;
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use lemmy_db_schema::schema::mod_sticky_post::dsl::*;
mod_sticky_post.find(from_id).first::<Self>(conn)
@ -65,7 +71,9 @@ impl Crud<ModStickyPostForm, i32> for ModStickyPost {
}
}
impl Crud<ModRemoveCommentForm, i32> for ModRemoveComment {
impl Crud for ModRemoveComment {
type Form = ModRemoveCommentForm;
type IdType = i32;
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use lemmy_db_schema::schema::mod_remove_comment::dsl::*;
mod_remove_comment.find(from_id).first::<Self>(conn)
@ -86,7 +94,9 @@ impl Crud<ModRemoveCommentForm, i32> for ModRemoveComment {
}
}
impl Crud<ModRemoveCommunityForm, i32> for ModRemoveCommunity {
impl Crud for ModRemoveCommunity {
type Form = ModRemoveCommunityForm;
type IdType = i32;
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use lemmy_db_schema::schema::mod_remove_community::dsl::*;
mod_remove_community.find(from_id).first::<Self>(conn)
@ -111,7 +121,9 @@ impl Crud<ModRemoveCommunityForm, i32> for ModRemoveCommunity {
}
}
impl Crud<ModBanFromCommunityForm, i32> for ModBanFromCommunity {
impl Crud for ModBanFromCommunity {
type Form = ModBanFromCommunityForm;
type IdType = i32;
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use lemmy_db_schema::schema::mod_ban_from_community::dsl::*;
mod_ban_from_community.find(from_id).first::<Self>(conn)
@ -136,7 +148,9 @@ impl Crud<ModBanFromCommunityForm, i32> for ModBanFromCommunity {
}
}
impl Crud<ModBanForm, i32> for ModBan {
impl Crud for ModBan {
type Form = ModBanForm;
type IdType = i32;
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use lemmy_db_schema::schema::mod_ban::dsl::*;
mod_ban.find(from_id).first::<Self>(conn)
@ -155,7 +169,9 @@ impl Crud<ModBanForm, i32> for ModBan {
}
}
impl Crud<ModAddCommunityForm, i32> for ModAddCommunity {
impl Crud for ModAddCommunity {
type Form = ModAddCommunityForm;
type IdType = i32;
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use lemmy_db_schema::schema::mod_add_community::dsl::*;
mod_add_community.find(from_id).first::<Self>(conn)
@ -176,7 +192,9 @@ impl Crud<ModAddCommunityForm, i32> for ModAddCommunity {
}
}
impl Crud<ModAddForm, i32> for ModAdd {
impl Crud for ModAdd {
type Form = ModAddForm;
type IdType = i32;
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use lemmy_db_schema::schema::mod_add::dsl::*;
mod_add.find(from_id).first::<Self>(conn)

View file

@ -7,7 +7,9 @@ use lemmy_db_schema::{
};
use sha2::{Digest, Sha256};
impl Crud<PasswordResetRequestForm, i32> for PasswordResetRequest {
impl Crud for PasswordResetRequest {
type Form = PasswordResetRequestForm;
type IdType = i32;
fn read(conn: &PgConnection, password_reset_request_id: i32) -> Result<Self, Error> {
password_reset_request
.find(password_reset_request_id)

View file

@ -158,7 +158,9 @@ mod safe_type_alias_2 {
}
}
impl Crud<PersonForm, PersonId> for Person {
impl Crud for Person {
type Form = PersonForm;
type IdType = PersonId;
fn read(conn: &PgConnection, person_id: PersonId) -> Result<Self, Error> {
person
.filter(deleted.eq(false))
@ -178,7 +180,8 @@ impl Crud<PersonForm, PersonId> for Person {
}
}
impl ApubObject<PersonForm> for Person {
impl ApubObject for Person {
type Form = PersonForm;
fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Self, Error> {
use lemmy_db_schema::schema::person::dsl::*;
person

View file

@ -2,7 +2,9 @@ use crate::Crud;
use diesel::{dsl::*, result::Error, *};
use lemmy_db_schema::{source::person_mention::*, PersonId, PersonMentionId};
impl Crud<PersonMentionForm, PersonMentionId> for PersonMention {
impl Crud for PersonMention {
type Form = PersonMentionForm;
type IdType = PersonMentionId;
fn read(conn: &PgConnection, person_mention_id: PersonMentionId) -> Result<Self, Error> {
use lemmy_db_schema::schema::person_mention::dsl::*;
person_mention.find(person_mention_id).first::<Self>(conn)

View file

@ -18,7 +18,9 @@ use lemmy_db_schema::{
PostId,
};
impl Crud<PostForm, PostId> for Post {
impl Crud for Post {
type Form = PostForm;
type IdType = PostId;
fn read(conn: &PgConnection, post_id: PostId) -> Result<Self, Error> {
use lemmy_db_schema::schema::post::dsl::*;
post.find(post_id).first::<Self>(conn)
@ -179,7 +181,8 @@ impl Post_ for Post {
}
}
impl ApubObject<PostForm> for Post {
impl ApubObject for Post {
type Form = PostForm;
fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Self, Error> {
use lemmy_db_schema::schema::post::dsl::*;
post.filter(ap_id.eq(object_id)).first::<Self>(conn)
@ -196,7 +199,9 @@ impl ApubObject<PostForm> for Post {
}
}
impl Likeable<PostLikeForm, PostId> for PostLike {
impl Likeable for PostLike {
type Form = PostLikeForm;
type IdType = PostId;
fn like(conn: &PgConnection, post_like_form: &PostLikeForm) -> Result<Self, Error> {
use lemmy_db_schema::schema::post_like::dsl::*;
insert_into(post_like)
@ -217,7 +222,8 @@ impl Likeable<PostLikeForm, PostId> for PostLike {
}
}
impl Saveable<PostSavedForm> for PostSaved {
impl Saveable for PostSaved {
type Form = PostSavedForm;
fn save(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<Self, Error> {
use lemmy_db_schema::schema::post_saved::dsl::*;
insert_into(post_saved)
@ -238,7 +244,8 @@ impl Saveable<PostSavedForm> for PostSaved {
}
}
impl Readable<PostReadForm> for PostRead {
impl Readable for PostRead {
type Form = PostReadForm;
fn mark_as_read(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<Self, Error> {
use lemmy_db_schema::schema::post_read::dsl::*;
insert_into(post_read)

View file

@ -2,7 +2,8 @@ use crate::Reportable;
use diesel::{dsl::*, result::Error, *};
use lemmy_db_schema::{naive_now, source::post_report::*, PersonId};
impl Reportable<PostReportForm> for PostReport {
impl Reportable for PostReport {
type Form = PostReportForm;
/// creates a post report and returns it
///
/// * `conn` - the postgres connection

View file

@ -2,7 +2,9 @@ use crate::{ApubObject, Crud, DeleteableOrRemoveable};
use diesel::{dsl::*, result::Error, *};
use lemmy_db_schema::{naive_now, source::private_message::*, DbUrl, PersonId, PrivateMessageId};
impl Crud<PrivateMessageForm, PrivateMessageId> for PrivateMessage {
impl Crud for PrivateMessage {
type Form = PrivateMessageForm;
type IdType = PrivateMessageId;
fn read(conn: &PgConnection, private_message_id: PrivateMessageId) -> Result<Self, Error> {
use lemmy_db_schema::schema::private_message::dsl::*;
private_message.find(private_message_id).first::<Self>(conn)
@ -27,7 +29,8 @@ impl Crud<PrivateMessageForm, PrivateMessageId> for PrivateMessage {
}
}
impl ApubObject<PrivateMessageForm> for PrivateMessage {
impl ApubObject for PrivateMessage {
type Form = PrivateMessageForm;
fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Self, Error>
where
Self: Sized,

View file

@ -2,7 +2,9 @@ use crate::Crud;
use diesel::{dsl::*, result::Error, *};
use lemmy_db_schema::{naive_now, source::site::*, PersonId};
impl Crud<SiteForm, i32> for Site {
impl Crud for Site {
type Form = SiteForm;
type IdType = i32;
fn read(conn: &PgConnection, _site_id: i32) -> Result<Self, Error> {
use lemmy_db_schema::schema::site::dsl::*;
site.first::<Self>(conn)