]> Untitled Git - lemmy.git/blob - crates/db_schema/src/impls/person_mention.rs
Diesel 2.0.0 upgrade (#2452)
[lemmy.git] / crates / db_schema / src / impls / person_mention.rs
1 use crate::{
2   newtypes::{CommentId, PersonId, PersonMentionId},
3   source::person_mention::*,
4   traits::Crud,
5 };
6 use diesel::{dsl::*, result::Error, *};
7
8 impl Crud for PersonMention {
9   type Form = PersonMentionForm;
10   type IdType = PersonMentionId;
11   fn read(conn: &mut PgConnection, person_mention_id: PersonMentionId) -> Result<Self, Error> {
12     use crate::schema::person_mention::dsl::*;
13     person_mention.find(person_mention_id).first::<Self>(conn)
14   }
15
16   fn create(
17     conn: &mut PgConnection,
18     person_mention_form: &PersonMentionForm,
19   ) -> Result<Self, Error> {
20     use crate::schema::person_mention::dsl::*;
21     // since the return here isnt utilized, we dont need to do an update
22     // but get_result doesnt return the existing row here
23     insert_into(person_mention)
24       .values(person_mention_form)
25       .on_conflict((recipient_id, comment_id))
26       .do_update()
27       .set(person_mention_form)
28       .get_result::<Self>(conn)
29   }
30
31   fn update(
32     conn: &mut PgConnection,
33     person_mention_id: PersonMentionId,
34     person_mention_form: &PersonMentionForm,
35   ) -> Result<Self, Error> {
36     use crate::schema::person_mention::dsl::*;
37     diesel::update(person_mention.find(person_mention_id))
38       .set(person_mention_form)
39       .get_result::<Self>(conn)
40   }
41 }
42
43 impl PersonMention {
44   pub fn update_read(
45     conn: &mut PgConnection,
46     person_mention_id: PersonMentionId,
47     new_read: bool,
48   ) -> Result<PersonMention, Error> {
49     use crate::schema::person_mention::dsl::*;
50     diesel::update(person_mention.find(person_mention_id))
51       .set(read.eq(new_read))
52       .get_result::<Self>(conn)
53   }
54
55   pub fn mark_all_as_read(
56     conn: &mut PgConnection,
57     for_recipient_id: PersonId,
58   ) -> Result<Vec<PersonMention>, Error> {
59     use crate::schema::person_mention::dsl::*;
60     diesel::update(
61       person_mention
62         .filter(recipient_id.eq(for_recipient_id))
63         .filter(read.eq(false)),
64     )
65     .set(read.eq(true))
66     .get_results::<Self>(conn)
67   }
68   pub fn read_by_comment_and_person(
69     conn: &mut PgConnection,
70     for_comment_id: CommentId,
71     for_recipient_id: PersonId,
72   ) -> Result<Self, Error> {
73     use crate::schema::person_mention::dsl::*;
74     person_mention
75       .filter(comment_id.eq(for_comment_id))
76       .filter(recipient_id.eq(for_recipient_id))
77       .first::<Self>(conn)
78   }
79 }
80
81 #[cfg(test)]
82 mod tests {
83   use crate::{
84     source::{
85       comment::*,
86       community::{Community, CommunityForm},
87       person::*,
88       person_mention::*,
89       post::*,
90     },
91     traits::Crud,
92     utils::establish_unpooled_connection,
93   };
94   use serial_test::serial;
95
96   #[test]
97   #[serial]
98   fn test_crud() {
99     let conn = &mut establish_unpooled_connection();
100
101     let new_person = PersonForm {
102       name: "terrylake".into(),
103       public_key: Some("pubkey".to_string()),
104       ..PersonForm::default()
105     };
106
107     let inserted_person = Person::create(conn, &new_person).unwrap();
108
109     let recipient_form = PersonForm {
110       name: "terrylakes recipient".into(),
111       public_key: Some("pubkey".to_string()),
112       ..PersonForm::default()
113     };
114
115     let inserted_recipient = Person::create(conn, &recipient_form).unwrap();
116
117     let new_community = CommunityForm {
118       name: "test community lake".to_string(),
119       title: "nada".to_owned(),
120       public_key: Some("pubkey".to_string()),
121       ..CommunityForm::default()
122     };
123
124     let inserted_community = Community::create(conn, &new_community).unwrap();
125
126     let new_post = PostForm {
127       name: "A test post".into(),
128       creator_id: inserted_person.id,
129       community_id: inserted_community.id,
130       ..PostForm::default()
131     };
132
133     let inserted_post = Post::create(conn, &new_post).unwrap();
134
135     let comment_form = CommentForm {
136       content: "A test comment".into(),
137       creator_id: inserted_person.id,
138       post_id: inserted_post.id,
139       ..CommentForm::default()
140     };
141
142     let inserted_comment = Comment::create(conn, &comment_form, None).unwrap();
143
144     let person_mention_form = PersonMentionForm {
145       recipient_id: inserted_recipient.id,
146       comment_id: inserted_comment.id,
147       read: None,
148     };
149
150     let inserted_mention = PersonMention::create(conn, &person_mention_form).unwrap();
151
152     let expected_mention = PersonMention {
153       id: inserted_mention.id,
154       recipient_id: inserted_mention.recipient_id,
155       comment_id: inserted_mention.comment_id,
156       read: false,
157       published: inserted_mention.published,
158     };
159
160     let read_mention = PersonMention::read(conn, inserted_mention.id).unwrap();
161     let updated_mention =
162       PersonMention::update(conn, inserted_mention.id, &person_mention_form).unwrap();
163     Comment::delete(conn, inserted_comment.id).unwrap();
164     Post::delete(conn, inserted_post.id).unwrap();
165     Community::delete(conn, inserted_community.id).unwrap();
166     Person::delete(conn, inserted_person.id).unwrap();
167     Person::delete(conn, inserted_recipient.id).unwrap();
168
169     assert_eq!(expected_mention, read_mention);
170     assert_eq!(expected_mention, inserted_mention);
171     assert_eq!(expected_mention, updated_mention);
172   }
173 }