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::read_or_create(pool, "my_domain.tld".to_string())
101 let new_person = PersonInsertForm::builder()
102 .name("terrylake".into())
103 .public_key("pubkey".to_string())
104 .instance_id(inserted_instance.id)
107 let inserted_person = Person::create(pool, &new_person).await.unwrap();
109 let recipient_form = PersonInsertForm::builder()
110 .name("terrylakes recipient".into())
111 .public_key("pubkey".to_string())
112 .instance_id(inserted_instance.id)
115 let inserted_recipient = Person::create(pool, &recipient_form).await.unwrap();
117 let new_community = CommunityInsertForm::builder()
118 .name("test community lake".to_string())
119 .title("nada".to_owned())
120 .public_key("pubkey".to_string())
121 .instance_id(inserted_instance.id)
124 let inserted_community = Community::create(pool, &new_community).await.unwrap();
126 let new_post = PostInsertForm::builder()
127 .name("A test post".into())
128 .creator_id(inserted_person.id)
129 .community_id(inserted_community.id)
132 let inserted_post = Post::create(pool, &new_post).await.unwrap();
134 let comment_form = CommentInsertForm::builder()
135 .content("A test comment".into())
136 .creator_id(inserted_person.id)
137 .post_id(inserted_post.id)
140 let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
142 let comment_reply_form = CommentReplyInsertForm {
143 recipient_id: inserted_recipient.id,
144 comment_id: inserted_comment.id,
148 let inserted_reply = CommentReply::create(pool, &comment_reply_form)
152 let expected_reply = CommentReply {
153 id: inserted_reply.id,
154 recipient_id: inserted_reply.recipient_id,
155 comment_id: inserted_reply.comment_id,
157 published: inserted_reply.published,
160 let read_reply = CommentReply::read(pool, inserted_reply.id).await.unwrap();
162 let comment_reply_update_form = CommentReplyUpdateForm { read: Some(false) };
163 let updated_reply = CommentReply::update(pool, inserted_reply.id, &comment_reply_update_form)
167 Comment::delete(pool, inserted_comment.id).await.unwrap();
168 Post::delete(pool, inserted_post.id).await.unwrap();
169 Community::delete(pool, inserted_community.id)
172 Person::delete(pool, inserted_person.id).await.unwrap();
173 Person::delete(pool, inserted_recipient.id).await.unwrap();
174 Instance::delete(pool, inserted_instance.id).await.unwrap();
176 assert_eq!(expected_reply, read_reply);
177 assert_eq!(expected_reply, inserted_reply);
178 assert_eq!(expected_reply, updated_reply);