2 newtypes::{CommentId, CommentReplyId, PersonId},
3 schema::comment_reply::dsl::{comment_id, comment_reply, read, recipient_id},
4 source::comment_reply::{CommentReply, CommentReplyInsertForm, CommentReplyUpdateForm},
6 utils::{get_conn, DbPool},
8 use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl};
9 use diesel_async::RunQueryDsl;
12 impl Crud for CommentReply {
13 type InsertForm = CommentReplyInsertForm;
14 type UpdateForm = CommentReplyUpdateForm;
15 type IdType = CommentReplyId;
18 pool: &mut DbPool<'_>,
19 comment_reply_form: &Self::InsertForm,
20 ) -> Result<Self, Error> {
21 let conn = &mut get_conn(pool).await?;
23 // since the return here isnt utilized, we dont need to do an update
24 // but get_result doesnt return the existing row here
25 insert_into(comment_reply)
26 .values(comment_reply_form)
27 .on_conflict((recipient_id, comment_id))
29 .set(comment_reply_form)
30 .get_result::<Self>(conn)
35 pool: &mut DbPool<'_>,
36 comment_reply_id: CommentReplyId,
37 comment_reply_form: &Self::UpdateForm,
38 ) -> Result<Self, Error> {
39 let conn = &mut get_conn(pool).await?;
40 diesel::update(comment_reply.find(comment_reply_id))
41 .set(comment_reply_form)
42 .get_result::<Self>(conn)
48 pub async fn mark_all_as_read(
49 pool: &mut DbPool<'_>,
50 for_recipient_id: PersonId,
51 ) -> Result<Vec<CommentReply>, Error> {
52 let conn = &mut get_conn(pool).await?;
55 .filter(recipient_id.eq(for_recipient_id))
56 .filter(read.eq(false)),
59 .get_results::<Self>(conn)
63 pub async fn read_by_comment(
64 pool: &mut DbPool<'_>,
65 for_comment_id: CommentId,
66 ) -> Result<Self, Error> {
67 let conn = &mut get_conn(pool).await?;
69 .filter(comment_id.eq(for_comment_id))
77 #![allow(clippy::unwrap_used)]
78 #![allow(clippy::indexing_slicing)]
82 comment::{Comment, CommentInsertForm},
83 comment_reply::{CommentReply, CommentReplyInsertForm, CommentReplyUpdateForm},
84 community::{Community, CommunityInsertForm},
86 person::{Person, PersonInsertForm},
87 post::{Post, PostInsertForm},
90 utils::build_db_pool_for_tests,
92 use serial_test::serial;
96 async fn test_crud() {
97 let pool = &build_db_pool_for_tests().await;
98 let pool = &mut pool.into();
100 let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
104 let new_person = PersonInsertForm::builder()
105 .name("terrylake".into())
106 .public_key("pubkey".to_string())
107 .instance_id(inserted_instance.id)
110 let inserted_person = Person::create(pool, &new_person).await.unwrap();
112 let recipient_form = PersonInsertForm::builder()
113 .name("terrylakes recipient".into())
114 .public_key("pubkey".to_string())
115 .instance_id(inserted_instance.id)
118 let inserted_recipient = Person::create(pool, &recipient_form).await.unwrap();
120 let new_community = CommunityInsertForm::builder()
121 .name("test community lake".to_string())
122 .title("nada".to_owned())
123 .public_key("pubkey".to_string())
124 .instance_id(inserted_instance.id)
127 let inserted_community = Community::create(pool, &new_community).await.unwrap();
129 let new_post = PostInsertForm::builder()
130 .name("A test post".into())
131 .creator_id(inserted_person.id)
132 .community_id(inserted_community.id)
135 let inserted_post = Post::create(pool, &new_post).await.unwrap();
137 let comment_form = CommentInsertForm::builder()
138 .content("A test comment".into())
139 .creator_id(inserted_person.id)
140 .post_id(inserted_post.id)
143 let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
145 let comment_reply_form = CommentReplyInsertForm {
146 recipient_id: inserted_recipient.id,
147 comment_id: inserted_comment.id,
151 let inserted_reply = CommentReply::create(pool, &comment_reply_form)
155 let expected_reply = CommentReply {
156 id: inserted_reply.id,
157 recipient_id: inserted_reply.recipient_id,
158 comment_id: inserted_reply.comment_id,
160 published: inserted_reply.published,
163 let read_reply = CommentReply::read(pool, inserted_reply.id).await.unwrap();
165 let comment_reply_update_form = CommentReplyUpdateForm { read: Some(false) };
166 let updated_reply = CommentReply::update(pool, inserted_reply.id, &comment_reply_update_form)
170 Comment::delete(pool, inserted_comment.id).await.unwrap();
171 Post::delete(pool, inserted_post.id).await.unwrap();
172 Community::delete(pool, inserted_community.id)
175 Person::delete(pool, inserted_person.id).await.unwrap();
176 Person::delete(pool, inserted_recipient.id).await.unwrap();
177 Instance::delete(pool, inserted_instance.id).await.unwrap();
179 assert_eq!(expected_reply, read_reply);
180 assert_eq!(expected_reply, inserted_reply);
181 assert_eq!(expected_reply, updated_reply);