2 newtypes::{CommentId, CommentReplyId, PersonId},
3 source::comment_reply::*,
6 use diesel::{dsl::*, result::Error, *};
8 impl Crud for CommentReply {
9 type Form = CommentReplyForm;
10 type IdType = CommentReplyId;
11 fn read(conn: &mut PgConnection, comment_reply_id: CommentReplyId) -> Result<Self, Error> {
12 use crate::schema::comment_reply::dsl::*;
13 comment_reply.find(comment_reply_id).first::<Self>(conn)
16 fn create(conn: &mut PgConnection, comment_reply_form: &CommentReplyForm) -> Result<Self, Error> {
17 use crate::schema::comment_reply::dsl::*;
18 // since the return here isnt utilized, we dont need to do an update
19 // but get_result doesnt return the existing row here
20 insert_into(comment_reply)
21 .values(comment_reply_form)
22 .on_conflict((recipient_id, comment_id))
24 .set(comment_reply_form)
25 .get_result::<Self>(conn)
29 conn: &mut PgConnection,
30 comment_reply_id: CommentReplyId,
31 comment_reply_form: &CommentReplyForm,
32 ) -> Result<Self, Error> {
33 use crate::schema::comment_reply::dsl::*;
34 diesel::update(comment_reply.find(comment_reply_id))
35 .set(comment_reply_form)
36 .get_result::<Self>(conn)
42 conn: &mut PgConnection,
43 comment_reply_id: CommentReplyId,
45 ) -> Result<CommentReply, Error> {
46 use crate::schema::comment_reply::dsl::*;
47 diesel::update(comment_reply.find(comment_reply_id))
48 .set(read.eq(new_read))
49 .get_result::<Self>(conn)
52 pub fn mark_all_as_read(
53 conn: &mut PgConnection,
54 for_recipient_id: PersonId,
55 ) -> Result<Vec<CommentReply>, Error> {
56 use crate::schema::comment_reply::dsl::*;
59 .filter(recipient_id.eq(for_recipient_id))
60 .filter(read.eq(false)),
63 .get_results::<Self>(conn)
66 pub fn read_by_comment(
67 conn: &mut PgConnection,
68 for_comment_id: CommentId,
69 ) -> Result<Self, Error> {
70 use crate::schema::comment_reply::dsl::*;
72 .filter(comment_id.eq(for_comment_id))
83 community::{Community, CommunityForm},
88 utils::establish_unpooled_connection,
90 use serial_test::serial;
95 let conn = &mut establish_unpooled_connection();
97 let new_person = PersonForm {
98 name: "terrylake".into(),
99 public_key: Some("pubkey".to_string()),
100 ..PersonForm::default()
103 let inserted_person = Person::create(conn, &new_person).unwrap();
105 let recipient_form = PersonForm {
106 name: "terrylakes recipient".into(),
107 public_key: Some("pubkey".to_string()),
108 ..PersonForm::default()
111 let inserted_recipient = Person::create(conn, &recipient_form).unwrap();
113 let new_community = CommunityForm {
114 name: "test community lake".to_string(),
115 title: "nada".to_owned(),
116 public_key: Some("pubkey".to_string()),
117 ..CommunityForm::default()
120 let inserted_community = Community::create(conn, &new_community).unwrap();
122 let new_post = PostForm {
123 name: "A test post".into(),
124 creator_id: inserted_person.id,
125 community_id: inserted_community.id,
126 ..PostForm::default()
129 let inserted_post = Post::create(conn, &new_post).unwrap();
131 let comment_form = CommentForm {
132 content: "A test comment".into(),
133 creator_id: inserted_person.id,
134 post_id: inserted_post.id,
135 ..CommentForm::default()
138 let inserted_comment = Comment::create(conn, &comment_form, None).unwrap();
140 let comment_reply_form = CommentReplyForm {
141 recipient_id: inserted_recipient.id,
142 comment_id: inserted_comment.id,
146 let inserted_reply = CommentReply::create(conn, &comment_reply_form).unwrap();
148 let expected_reply = CommentReply {
149 id: inserted_reply.id,
150 recipient_id: inserted_reply.recipient_id,
151 comment_id: inserted_reply.comment_id,
153 published: inserted_reply.published,
156 let read_reply = CommentReply::read(conn, inserted_reply.id).unwrap();
157 let updated_reply = CommentReply::update(conn, inserted_reply.id, &comment_reply_form).unwrap();
158 Comment::delete(conn, inserted_comment.id).unwrap();
159 Post::delete(conn, inserted_post.id).unwrap();
160 Community::delete(conn, inserted_community.id).unwrap();
161 Person::delete(conn, inserted_person.id).unwrap();
162 Person::delete(conn, inserted_recipient.id).unwrap();
164 assert_eq!(expected_reply, read_reply);
165 assert_eq!(expected_reply, inserted_reply);
166 assert_eq!(expected_reply, updated_reply);