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;
16 async fn read(pool: &mut DbPool<'_>, comment_reply_id: CommentReplyId) -> Result<Self, Error> {
17 let conn = &mut get_conn(pool).await?;
19 .find(comment_reply_id)
25 pool: &mut DbPool<'_>,
26 comment_reply_form: &Self::InsertForm,
27 ) -> Result<Self, Error> {
28 let conn = &mut get_conn(pool).await?;
30 // since the return here isnt utilized, we dont need to do an update
31 // but get_result doesnt return the existing row here
32 insert_into(comment_reply)
33 .values(comment_reply_form)
34 .on_conflict((recipient_id, comment_id))
36 .set(comment_reply_form)
37 .get_result::<Self>(conn)
42 pool: &mut DbPool<'_>,
43 comment_reply_id: CommentReplyId,
44 comment_reply_form: &Self::UpdateForm,
45 ) -> Result<Self, Error> {
46 let conn = &mut get_conn(pool).await?;
47 diesel::update(comment_reply.find(comment_reply_id))
48 .set(comment_reply_form)
49 .get_result::<Self>(conn)
55 pub async fn mark_all_as_read(
56 pool: &mut DbPool<'_>,
57 for_recipient_id: PersonId,
58 ) -> Result<Vec<CommentReply>, Error> {
59 let conn = &mut get_conn(pool).await?;
62 .filter(recipient_id.eq(for_recipient_id))
63 .filter(read.eq(false)),
66 .get_results::<Self>(conn)
70 pub async fn read_by_comment(
71 pool: &mut DbPool<'_>,
72 for_comment_id: CommentId,
73 ) -> Result<Self, Error> {
74 let conn = &mut get_conn(pool).await?;
76 .filter(comment_id.eq(for_comment_id))
84 #![allow(clippy::unwrap_used)]
85 #![allow(clippy::indexing_slicing)]
89 comment::{Comment, CommentInsertForm},
90 comment_reply::{CommentReply, CommentReplyInsertForm, CommentReplyUpdateForm},
91 community::{Community, CommunityInsertForm},
93 person::{Person, PersonInsertForm},
94 post::{Post, PostInsertForm},
97 utils::build_db_pool_for_tests,
99 use serial_test::serial;
103 async fn test_crud() {
104 let pool = &build_db_pool_for_tests().await;
105 let pool = &mut pool.into();
107 let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
111 let new_person = PersonInsertForm::builder()
112 .name("terrylake".into())
113 .public_key("pubkey".to_string())
114 .instance_id(inserted_instance.id)
117 let inserted_person = Person::create(pool, &new_person).await.unwrap();
119 let recipient_form = PersonInsertForm::builder()
120 .name("terrylakes recipient".into())
121 .public_key("pubkey".to_string())
122 .instance_id(inserted_instance.id)
125 let inserted_recipient = Person::create(pool, &recipient_form).await.unwrap();
127 let new_community = CommunityInsertForm::builder()
128 .name("test community lake".to_string())
129 .title("nada".to_owned())
130 .public_key("pubkey".to_string())
131 .instance_id(inserted_instance.id)
134 let inserted_community = Community::create(pool, &new_community).await.unwrap();
136 let new_post = PostInsertForm::builder()
137 .name("A test post".into())
138 .creator_id(inserted_person.id)
139 .community_id(inserted_community.id)
142 let inserted_post = Post::create(pool, &new_post).await.unwrap();
144 let comment_form = CommentInsertForm::builder()
145 .content("A test comment".into())
146 .creator_id(inserted_person.id)
147 .post_id(inserted_post.id)
150 let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
152 let comment_reply_form = CommentReplyInsertForm {
153 recipient_id: inserted_recipient.id,
154 comment_id: inserted_comment.id,
158 let inserted_reply = CommentReply::create(pool, &comment_reply_form)
162 let expected_reply = CommentReply {
163 id: inserted_reply.id,
164 recipient_id: inserted_reply.recipient_id,
165 comment_id: inserted_reply.comment_id,
167 published: inserted_reply.published,
170 let read_reply = CommentReply::read(pool, inserted_reply.id).await.unwrap();
172 let comment_reply_update_form = CommentReplyUpdateForm { read: Some(false) };
173 let updated_reply = CommentReply::update(pool, inserted_reply.id, &comment_reply_update_form)
177 Comment::delete(pool, inserted_comment.id).await.unwrap();
178 Post::delete(pool, inserted_post.id).await.unwrap();
179 Community::delete(pool, inserted_community.id)
182 Person::delete(pool, inserted_person.id).await.unwrap();
183 Person::delete(pool, inserted_recipient.id).await.unwrap();
184 Instance::delete(pool, inserted_instance.id).await.unwrap();
186 assert_eq!(expected_reply, read_reply);
187 assert_eq!(expected_reply, inserted_reply);
188 assert_eq!(expected_reply, updated_reply);