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: &DbPool, comment_reply_id: CommentReplyId) -> Result<Self, Error> {
17 let conn = &mut get_conn(pool).await?;
19 .find(comment_reply_id)
24 async fn create(pool: &DbPool, comment_reply_form: &Self::InsertForm) -> Result<Self, Error> {
25 let conn = &mut get_conn(pool).await?;
27 // since the return here isnt utilized, we dont need to do an update
28 // but get_result doesnt return the existing row here
29 insert_into(comment_reply)
30 .values(comment_reply_form)
31 .on_conflict((recipient_id, comment_id))
33 .set(comment_reply_form)
34 .get_result::<Self>(conn)
40 comment_reply_id: CommentReplyId,
41 comment_reply_form: &Self::UpdateForm,
42 ) -> Result<Self, Error> {
43 let conn = &mut get_conn(pool).await?;
44 diesel::update(comment_reply.find(comment_reply_id))
45 .set(comment_reply_form)
46 .get_result::<Self>(conn)
52 pub async fn mark_all_as_read(
54 for_recipient_id: PersonId,
55 ) -> Result<Vec<CommentReply>, Error> {
56 let conn = &mut get_conn(pool).await?;
59 .filter(recipient_id.eq(for_recipient_id))
60 .filter(read.eq(false)),
63 .get_results::<Self>(conn)
67 pub async fn read_by_comment(pool: &DbPool, for_comment_id: CommentId) -> Result<Self, Error> {
68 let conn = &mut get_conn(pool).await?;
70 .filter(comment_id.eq(for_comment_id))
80 comment::{Comment, CommentInsertForm},
81 comment_reply::{CommentReply, CommentReplyInsertForm, CommentReplyUpdateForm},
82 community::{Community, CommunityInsertForm},
84 person::{Person, PersonInsertForm},
85 post::{Post, PostInsertForm},
88 utils::build_db_pool_for_tests,
90 use serial_test::serial;
94 async fn test_crud() {
95 let pool = &build_db_pool_for_tests().await;
97 let inserted_instance = Instance::create(pool, "my_domain.tld").await.unwrap();
99 let new_person = PersonInsertForm::builder()
100 .name("terrylake".into())
101 .public_key("pubkey".to_string())
102 .instance_id(inserted_instance.id)
105 let inserted_person = Person::create(pool, &new_person).await.unwrap();
107 let recipient_form = PersonInsertForm::builder()
108 .name("terrylakes recipient".into())
109 .public_key("pubkey".to_string())
110 .instance_id(inserted_instance.id)
113 let inserted_recipient = Person::create(pool, &recipient_form).await.unwrap();
115 let new_community = CommunityInsertForm::builder()
116 .name("test community lake".to_string())
117 .title("nada".to_owned())
118 .public_key("pubkey".to_string())
119 .instance_id(inserted_instance.id)
122 let inserted_community = Community::create(pool, &new_community).await.unwrap();
124 let new_post = PostInsertForm::builder()
125 .name("A test post".into())
126 .creator_id(inserted_person.id)
127 .community_id(inserted_community.id)
130 let inserted_post = Post::create(pool, &new_post).await.unwrap();
132 let comment_form = CommentInsertForm::builder()
133 .content("A test comment".into())
134 .creator_id(inserted_person.id)
135 .post_id(inserted_post.id)
138 let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
140 let comment_reply_form = CommentReplyInsertForm {
141 recipient_id: inserted_recipient.id,
142 comment_id: inserted_comment.id,
146 let inserted_reply = CommentReply::create(pool, &comment_reply_form)
150 let expected_reply = CommentReply {
151 id: inserted_reply.id,
152 recipient_id: inserted_reply.recipient_id,
153 comment_id: inserted_reply.comment_id,
155 published: inserted_reply.published,
158 let read_reply = CommentReply::read(pool, inserted_reply.id).await.unwrap();
160 let comment_reply_update_form = CommentReplyUpdateForm { read: Some(false) };
161 let updated_reply = CommentReply::update(pool, inserted_reply.id, &comment_reply_update_form)
165 Comment::delete(pool, inserted_comment.id).await.unwrap();
166 Post::delete(pool, inserted_post.id).await.unwrap();
167 Community::delete(pool, inserted_community.id)
170 Person::delete(pool, inserted_person.id).await.unwrap();
171 Person::delete(pool, inserted_recipient.id).await.unwrap();
172 Instance::delete(pool, inserted_instance.id).await.unwrap();
174 assert_eq!(expected_reply, read_reply);
175 assert_eq!(expected_reply, inserted_reply);
176 assert_eq!(expected_reply, updated_reply);