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: &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: &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)
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)
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(
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(conn: &PgConnection, for_comment_id: CommentId) -> Result<Self, Error> {
67 use crate::schema::comment_reply::dsl::*;
69 .filter(comment_id.eq(for_comment_id))
80 community::{Community, CommunityForm},
85 utils::establish_unpooled_connection,
87 use serial_test::serial;
92 let conn = establish_unpooled_connection();
94 let new_person = PersonForm {
95 name: "terrylake".into(),
96 public_key: Some("pubkey".to_string()),
97 ..PersonForm::default()
100 let inserted_person = Person::create(&conn, &new_person).unwrap();
102 let recipient_form = PersonForm {
103 name: "terrylakes recipient".into(),
104 public_key: Some("pubkey".to_string()),
105 ..PersonForm::default()
108 let inserted_recipient = Person::create(&conn, &recipient_form).unwrap();
110 let new_community = CommunityForm {
111 name: "test community lake".to_string(),
112 title: "nada".to_owned(),
113 public_key: Some("pubkey".to_string()),
114 ..CommunityForm::default()
117 let inserted_community = Community::create(&conn, &new_community).unwrap();
119 let new_post = PostForm {
120 name: "A test post".into(),
121 creator_id: inserted_person.id,
122 community_id: inserted_community.id,
123 ..PostForm::default()
126 let inserted_post = Post::create(&conn, &new_post).unwrap();
128 let comment_form = CommentForm {
129 content: "A test comment".into(),
130 creator_id: inserted_person.id,
131 post_id: inserted_post.id,
132 ..CommentForm::default()
135 let inserted_comment = Comment::create(&conn, &comment_form, None).unwrap();
137 let comment_reply_form = CommentReplyForm {
138 recipient_id: inserted_recipient.id,
139 comment_id: inserted_comment.id,
143 let inserted_reply = CommentReply::create(&conn, &comment_reply_form).unwrap();
145 let expected_reply = CommentReply {
146 id: inserted_reply.id,
147 recipient_id: inserted_reply.recipient_id,
148 comment_id: inserted_reply.comment_id,
150 published: inserted_reply.published,
153 let read_reply = CommentReply::read(&conn, inserted_reply.id).unwrap();
155 CommentReply::update(&conn, inserted_reply.id, &comment_reply_form).unwrap();
156 Comment::delete(&conn, inserted_comment.id).unwrap();
157 Post::delete(&conn, inserted_post.id).unwrap();
158 Community::delete(&conn, inserted_community.id).unwrap();
159 Person::delete(&conn, inserted_person.id).unwrap();
160 Person::delete(&conn, inserted_recipient.id).unwrap();
162 assert_eq!(expected_reply, read_reply);
163 assert_eq!(expected_reply, inserted_reply);
164 assert_eq!(expected_reply, updated_reply);