]> Untitled Git - lemmy.git/blob - crates/db_schema/src/impls/comment_reply.rs
8fed2ce47f03f24ccc2a7c61809938491880e737
[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: &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)
14   }
15
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))
23       .do_update()
24       .set(comment_reply_form)
25       .get_result::<Self>(conn)
26   }
27
28   fn update(
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)
37   }
38 }
39
40 impl CommentReply {
41   pub fn update_read(
42     conn: &mut 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: &mut 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(
67     conn: &mut PgConnection,
68     for_comment_id: CommentId,
69   ) -> Result<Self, Error> {
70     use crate::schema::comment_reply::dsl::*;
71     comment_reply
72       .filter(comment_id.eq(for_comment_id))
73       .first::<Self>(conn)
74   }
75 }
76
77 #[cfg(test)]
78 mod tests {
79   use crate::{
80     source::{
81       comment::*,
82       comment_reply::*,
83       community::{Community, CommunityForm},
84       person::*,
85       post::*,
86     },
87     traits::Crud,
88     utils::establish_unpooled_connection,
89   };
90   use serial_test::serial;
91
92   #[test]
93   #[serial]
94   fn test_crud() {
95     let conn = &mut establish_unpooled_connection();
96
97     let new_person = PersonForm {
98       name: "terrylake".into(),
99       public_key: Some("pubkey".to_string()),
100       ..PersonForm::default()
101     };
102
103     let inserted_person = Person::create(conn, &new_person).unwrap();
104
105     let recipient_form = PersonForm {
106       name: "terrylakes recipient".into(),
107       public_key: Some("pubkey".to_string()),
108       ..PersonForm::default()
109     };
110
111     let inserted_recipient = Person::create(conn, &recipient_form).unwrap();
112
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()
118     };
119
120     let inserted_community = Community::create(conn, &new_community).unwrap();
121
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()
127     };
128
129     let inserted_post = Post::create(conn, &new_post).unwrap();
130
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()
136     };
137
138     let inserted_comment = Comment::create(conn, &comment_form, None).unwrap();
139
140     let comment_reply_form = CommentReplyForm {
141       recipient_id: inserted_recipient.id,
142       comment_id: inserted_comment.id,
143       read: None,
144     };
145
146     let inserted_reply = CommentReply::create(conn, &comment_reply_form).unwrap();
147
148     let expected_reply = CommentReply {
149       id: inserted_reply.id,
150       recipient_id: inserted_reply.recipient_id,
151       comment_id: inserted_reply.comment_id,
152       read: false,
153       published: inserted_reply.published,
154     };
155
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();
163
164     assert_eq!(expected_reply, read_reply);
165     assert_eq!(expected_reply, inserted_reply);
166     assert_eq!(expected_reply, updated_reply);
167   }
168 }