]> Untitled Git - lemmy.git/blob - crates/db_schema/src/impls/comment_reply.rs
271e5e7184bc9d0eeeebdbebf3ebd6db0bb5b3ac
[lemmy.git] / crates / db_schema / src / impls / comment_reply.rs
1 use crate::{
2   newtypes::{CommentId, CommentReplyId, PersonId},
3   source::comment_reply::*,
4   traits::Crud,
5 };
6 use diesel::{dsl::*, result::Error, *};
7
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)
14   }
15
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))
23       .do_update()
24       .set(comment_reply_form)
25       .get_result::<Self>(conn)
26   }
27
28   fn update(
29     conn: &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)
37   }
38 }
39
40 impl CommentReply {
41   pub fn update_read(
42     conn: &PgConnection,
43     comment_reply_id: CommentReplyId,
44     new_read: bool,
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)
50   }
51
52   pub fn mark_all_as_read(
53     conn: &PgConnection,
54     for_recipient_id: PersonId,
55   ) -> Result<Vec<CommentReply>, Error> {
56     use crate::schema::comment_reply::dsl::*;
57     diesel::update(
58       comment_reply
59         .filter(recipient_id.eq(for_recipient_id))
60         .filter(read.eq(false)),
61     )
62     .set(read.eq(true))
63     .get_results::<Self>(conn)
64   }
65
66   pub fn read_by_comment(conn: &PgConnection, for_comment_id: CommentId) -> Result<Self, Error> {
67     use crate::schema::comment_reply::dsl::*;
68     comment_reply
69       .filter(comment_id.eq(for_comment_id))
70       .first::<Self>(conn)
71   }
72 }
73
74 #[cfg(test)]
75 mod tests {
76   use crate::{
77     source::{
78       comment::*,
79       comment_reply::*,
80       community::{Community, CommunityForm},
81       person::*,
82       post::*,
83     },
84     traits::Crud,
85     utils::establish_unpooled_connection,
86   };
87   use serial_test::serial;
88
89   #[test]
90   #[serial]
91   fn test_crud() {
92     let conn = establish_unpooled_connection();
93
94     let new_person = PersonForm {
95       name: "terrylake".into(),
96       public_key: Some("pubkey".to_string()),
97       ..PersonForm::default()
98     };
99
100     let inserted_person = Person::create(&conn, &new_person).unwrap();
101
102     let recipient_form = PersonForm {
103       name: "terrylakes recipient".into(),
104       public_key: Some("pubkey".to_string()),
105       ..PersonForm::default()
106     };
107
108     let inserted_recipient = Person::create(&conn, &recipient_form).unwrap();
109
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()
115     };
116
117     let inserted_community = Community::create(&conn, &new_community).unwrap();
118
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()
124     };
125
126     let inserted_post = Post::create(&conn, &new_post).unwrap();
127
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()
133     };
134
135     let inserted_comment = Comment::create(&conn, &comment_form, None).unwrap();
136
137     let comment_reply_form = CommentReplyForm {
138       recipient_id: inserted_recipient.id,
139       comment_id: inserted_comment.id,
140       read: None,
141     };
142
143     let inserted_reply = CommentReply::create(&conn, &comment_reply_form).unwrap();
144
145     let expected_reply = CommentReply {
146       id: inserted_reply.id,
147       recipient_id: inserted_reply.recipient_id,
148       comment_id: inserted_reply.comment_id,
149       read: false,
150       published: inserted_reply.published,
151     };
152
153     let read_reply = CommentReply::read(&conn, inserted_reply.id).unwrap();
154     let updated_reply =
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();
161
162     assert_eq!(expected_reply, read_reply);
163     assert_eq!(expected_reply, inserted_reply);
164     assert_eq!(expected_reply, updated_reply);
165   }
166 }