2 use diesel::{dsl::*, result::Error, *};
3 use lemmy_db_schema::{source::person_mention::*, PersonId, PersonMentionId};
5 impl Crud<PersonMentionForm, PersonMentionId> for PersonMention {
6 fn read(conn: &PgConnection, person_mention_id: PersonMentionId) -> Result<Self, Error> {
7 use lemmy_db_schema::schema::person_mention::dsl::*;
8 person_mention.find(person_mention_id).first::<Self>(conn)
11 fn create(conn: &PgConnection, person_mention_form: &PersonMentionForm) -> Result<Self, Error> {
12 use lemmy_db_schema::schema::person_mention::dsl::*;
13 // since the return here isnt utilized, we dont need to do an update
14 // but get_result doesnt return the existing row here
15 insert_into(person_mention)
16 .values(person_mention_form)
17 .on_conflict((recipient_id, comment_id))
19 .set(person_mention_form)
20 .get_result::<Self>(conn)
25 person_mention_id: PersonMentionId,
26 person_mention_form: &PersonMentionForm,
27 ) -> Result<Self, Error> {
28 use lemmy_db_schema::schema::person_mention::dsl::*;
29 diesel::update(person_mention.find(person_mention_id))
30 .set(person_mention_form)
31 .get_result::<Self>(conn)
35 pub trait PersonMention_ {
38 person_mention_id: PersonMentionId,
40 ) -> Result<PersonMention, Error>;
43 for_recipient_id: PersonId,
44 ) -> Result<Vec<PersonMention>, Error>;
47 impl PersonMention_ for PersonMention {
50 person_mention_id: PersonMentionId,
52 ) -> Result<PersonMention, Error> {
53 use lemmy_db_schema::schema::person_mention::dsl::*;
54 diesel::update(person_mention.find(person_mention_id))
55 .set(read.eq(new_read))
56 .get_result::<Self>(conn)
61 for_recipient_id: PersonId,
62 ) -> Result<Vec<PersonMention>, Error> {
63 use lemmy_db_schema::schema::person_mention::dsl::*;
66 .filter(recipient_id.eq(for_recipient_id))
67 .filter(read.eq(false)),
70 .get_results::<Self>(conn)
76 use crate::{establish_unpooled_connection, Crud};
77 use lemmy_db_schema::source::{
79 community::{Community, CommunityForm},
84 use serial_test::serial;
89 let conn = establish_unpooled_connection();
91 let new_person = PersonForm {
92 name: "terrylake".into(),
93 ..PersonForm::default()
96 let inserted_person = Person::create(&conn, &new_person).unwrap();
98 let recipient_form = PersonForm {
99 name: "terrylakes recipient".into(),
100 ..PersonForm::default()
103 let inserted_recipient = Person::create(&conn, &recipient_form).unwrap();
105 let new_community = CommunityForm {
106 name: "test community lake".to_string(),
107 title: "nada".to_owned(),
108 ..CommunityForm::default()
111 let inserted_community = Community::create(&conn, &new_community).unwrap();
113 let new_post = PostForm {
114 name: "A test post".into(),
115 creator_id: inserted_person.id,
116 community_id: inserted_community.id,
117 ..PostForm::default()
120 let inserted_post = Post::create(&conn, &new_post).unwrap();
122 let comment_form = CommentForm {
123 content: "A test comment".into(),
124 creator_id: inserted_person.id,
125 post_id: inserted_post.id,
126 ..CommentForm::default()
129 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
131 let person_mention_form = PersonMentionForm {
132 recipient_id: inserted_recipient.id,
133 comment_id: inserted_comment.id,
137 let inserted_mention = PersonMention::create(&conn, &person_mention_form).unwrap();
139 let expected_mention = PersonMention {
140 id: inserted_mention.id,
141 recipient_id: inserted_mention.recipient_id,
142 comment_id: inserted_mention.comment_id,
144 published: inserted_mention.published,
147 let read_mention = PersonMention::read(&conn, inserted_mention.id).unwrap();
148 let updated_mention =
149 PersonMention::update(&conn, inserted_mention.id, &person_mention_form).unwrap();
150 Comment::delete(&conn, inserted_comment.id).unwrap();
151 Post::delete(&conn, inserted_post.id).unwrap();
152 Community::delete(&conn, inserted_community.id).unwrap();
153 Person::delete(&conn, inserted_person.id).unwrap();
154 Person::delete(&conn, inserted_recipient.id).unwrap();
156 assert_eq!(expected_mention, read_mention);
157 assert_eq!(expected_mention, inserted_mention);
158 assert_eq!(expected_mention, updated_mention);