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))
86 comment::{Comment, CommentInsertForm},
87 comment_reply::{CommentReply, CommentReplyInsertForm, CommentReplyUpdateForm},
88 community::{Community, CommunityInsertForm},
90 person::{Person, PersonInsertForm},
91 post::{Post, PostInsertForm},
94 utils::build_db_pool_for_tests,
96 use serial_test::serial;
100 async fn test_crud() {
101 let pool = &build_db_pool_for_tests().await;
102 let pool = &mut pool.into();
104 let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
108 let new_person = PersonInsertForm::builder()
109 .name("terrylake".into())
110 .public_key("pubkey".to_string())
111 .instance_id(inserted_instance.id)
114 let inserted_person = Person::create(pool, &new_person).await.unwrap();
116 let recipient_form = PersonInsertForm::builder()
117 .name("terrylakes recipient".into())
118 .public_key("pubkey".to_string())
119 .instance_id(inserted_instance.id)
122 let inserted_recipient = Person::create(pool, &recipient_form).await.unwrap();
124 let new_community = CommunityInsertForm::builder()
125 .name("test community lake".to_string())
126 .title("nada".to_owned())
127 .public_key("pubkey".to_string())
128 .instance_id(inserted_instance.id)
131 let inserted_community = Community::create(pool, &new_community).await.unwrap();
133 let new_post = PostInsertForm::builder()
134 .name("A test post".into())
135 .creator_id(inserted_person.id)
136 .community_id(inserted_community.id)
139 let inserted_post = Post::create(pool, &new_post).await.unwrap();
141 let comment_form = CommentInsertForm::builder()
142 .content("A test comment".into())
143 .creator_id(inserted_person.id)
144 .post_id(inserted_post.id)
147 let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
149 let comment_reply_form = CommentReplyInsertForm {
150 recipient_id: inserted_recipient.id,
151 comment_id: inserted_comment.id,
155 let inserted_reply = CommentReply::create(pool, &comment_reply_form)
159 let expected_reply = CommentReply {
160 id: inserted_reply.id,
161 recipient_id: inserted_reply.recipient_id,
162 comment_id: inserted_reply.comment_id,
164 published: inserted_reply.published,
167 let read_reply = CommentReply::read(pool, inserted_reply.id).await.unwrap();
169 let comment_reply_update_form = CommentReplyUpdateForm { read: Some(false) };
170 let updated_reply = CommentReply::update(pool, inserted_reply.id, &comment_reply_update_form)
174 Comment::delete(pool, inserted_comment.id).await.unwrap();
175 Post::delete(pool, inserted_post.id).await.unwrap();
176 Community::delete(pool, inserted_community.id)
179 Person::delete(pool, inserted_person.id).await.unwrap();
180 Person::delete(pool, inserted_recipient.id).await.unwrap();
181 Instance::delete(pool, inserted_instance.id).await.unwrap();
183 assert_eq!(expected_reply, read_reply);
184 assert_eq!(expected_reply, inserted_reply);
185 assert_eq!(expected_reply, updated_reply);