2 use diesel::{dsl::*, result::Error, *};
3 use lemmy_db_schema::{source::person_mention::*, PersonId, PersonMentionId};
5 impl Crud for PersonMention {
6 type Form = PersonMentionForm;
7 type IdType = PersonMentionId;
8 fn read(conn: &PgConnection, person_mention_id: PersonMentionId) -> Result<Self, Error> {
9 use lemmy_db_schema::schema::person_mention::dsl::*;
10 person_mention.find(person_mention_id).first::<Self>(conn)
13 fn create(conn: &PgConnection, person_mention_form: &PersonMentionForm) -> Result<Self, Error> {
14 use lemmy_db_schema::schema::person_mention::dsl::*;
15 // since the return here isnt utilized, we dont need to do an update
16 // but get_result doesnt return the existing row here
17 insert_into(person_mention)
18 .values(person_mention_form)
19 .on_conflict((recipient_id, comment_id))
21 .set(person_mention_form)
22 .get_result::<Self>(conn)
27 person_mention_id: PersonMentionId,
28 person_mention_form: &PersonMentionForm,
29 ) -> Result<Self, Error> {
30 use lemmy_db_schema::schema::person_mention::dsl::*;
31 diesel::update(person_mention.find(person_mention_id))
32 .set(person_mention_form)
33 .get_result::<Self>(conn)
37 pub trait PersonMention_ {
40 person_mention_id: PersonMentionId,
42 ) -> Result<PersonMention, Error>;
45 for_recipient_id: PersonId,
46 ) -> Result<Vec<PersonMention>, Error>;
49 impl PersonMention_ for PersonMention {
52 person_mention_id: PersonMentionId,
54 ) -> Result<PersonMention, Error> {
55 use lemmy_db_schema::schema::person_mention::dsl::*;
56 diesel::update(person_mention.find(person_mention_id))
57 .set(read.eq(new_read))
58 .get_result::<Self>(conn)
63 for_recipient_id: PersonId,
64 ) -> Result<Vec<PersonMention>, Error> {
65 use lemmy_db_schema::schema::person_mention::dsl::*;
68 .filter(recipient_id.eq(for_recipient_id))
69 .filter(read.eq(false)),
72 .get_results::<Self>(conn)
78 use crate::{establish_unpooled_connection, Crud};
79 use lemmy_db_schema::source::{
81 community::{Community, CommunityForm},
86 use serial_test::serial;
91 let conn = establish_unpooled_connection();
93 let new_person = PersonForm {
94 name: "terrylake".into(),
95 ..PersonForm::default()
98 let inserted_person = Person::create(&conn, &new_person).unwrap();
100 let recipient_form = PersonForm {
101 name: "terrylakes recipient".into(),
102 ..PersonForm::default()
105 let inserted_recipient = Person::create(&conn, &recipient_form).unwrap();
107 let new_community = CommunityForm {
108 name: "test community lake".to_string(),
109 title: "nada".to_owned(),
110 ..CommunityForm::default()
113 let inserted_community = Community::create(&conn, &new_community).unwrap();
115 let new_post = PostForm {
116 name: "A test post".into(),
117 creator_id: inserted_person.id,
118 community_id: inserted_community.id,
119 ..PostForm::default()
122 let inserted_post = Post::create(&conn, &new_post).unwrap();
124 let comment_form = CommentForm {
125 content: "A test comment".into(),
126 creator_id: inserted_person.id,
127 post_id: inserted_post.id,
128 ..CommentForm::default()
131 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
133 let person_mention_form = PersonMentionForm {
134 recipient_id: inserted_recipient.id,
135 comment_id: inserted_comment.id,
139 let inserted_mention = PersonMention::create(&conn, &person_mention_form).unwrap();
141 let expected_mention = PersonMention {
142 id: inserted_mention.id,
143 recipient_id: inserted_mention.recipient_id,
144 comment_id: inserted_mention.comment_id,
146 published: inserted_mention.published,
149 let read_mention = PersonMention::read(&conn, inserted_mention.id).unwrap();
150 let updated_mention =
151 PersonMention::update(&conn, inserted_mention.id, &person_mention_form).unwrap();
152 Comment::delete(&conn, inserted_comment.id).unwrap();
153 Post::delete(&conn, inserted_post.id).unwrap();
154 Community::delete(&conn, inserted_community.id).unwrap();
155 Person::delete(&conn, inserted_person.id).unwrap();
156 Person::delete(&conn, inserted_recipient.id).unwrap();
158 assert_eq!(expected_mention, read_mention);
159 assert_eq!(expected_mention, inserted_mention);
160 assert_eq!(expected_mention, updated_mention);