1 use crate::schema::{post, post_like, post_saved, post_read};
4 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
9 pub url: Option<String>,
10 pub body: Option<String>,
12 pub community_id: i32,
15 pub published: chrono::NaiveDateTime,
16 pub updated: Option<chrono::NaiveDateTime>,
21 #[derive(Insertable, AsChangeset, Clone)]
25 pub url: Option<String>,
26 pub body: Option<String>,
28 pub community_id: i32,
29 pub removed: Option<bool>,
30 pub locked: Option<bool>,
31 pub updated: Option<chrono::NaiveDateTime>,
32 pub deleted: Option<bool>,
36 impl Crud<PostForm> for Post {
37 fn read(conn: &PgConnection, post_id: i32) -> Result<Self, Error> {
38 use crate::schema::post::dsl::*;
43 fn delete(conn: &PgConnection, post_id: i32) -> Result<usize, Error> {
44 use crate::schema::post::dsl::*;
45 diesel::delete(post.find(post_id))
49 fn create(conn: &PgConnection, new_post: &PostForm) -> Result<Self, Error> {
50 use crate::schema::post::dsl::*;
53 .get_result::<Self>(conn)
56 fn update(conn: &PgConnection, post_id: i32, new_post: &PostForm) -> Result<Self, Error> {
57 use crate::schema::post::dsl::*;
58 diesel::update(post.find(post_id))
60 .get_result::<Self>(conn)
64 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
66 #[table_name = "post_like"]
72 pub published: chrono::NaiveDateTime,
75 #[derive(Insertable, AsChangeset, Clone)]
76 #[table_name="post_like"]
77 pub struct PostLikeForm {
83 impl Likeable <PostLikeForm> for PostLike {
84 fn read(conn: &PgConnection, post_id_from: i32) -> Result<Vec<Self>, Error> {
85 use crate::schema::post_like::dsl::*;
87 .filter(post_id.eq(post_id_from))
90 fn like(conn: &PgConnection, post_like_form: &PostLikeForm) -> Result<Self, Error> {
91 use crate::schema::post_like::dsl::*;
92 insert_into(post_like)
93 .values(post_like_form)
94 .get_result::<Self>(conn)
96 fn remove(conn: &PgConnection, post_like_form: &PostLikeForm) -> Result<usize, Error> {
97 use crate::schema::post_like::dsl::*;
98 diesel::delete(post_like
99 .filter(post_id.eq(post_like_form.post_id))
100 .filter(user_id.eq(post_like_form.user_id)))
105 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
107 #[table_name = "post_saved"]
108 pub struct PostSaved {
112 pub published: chrono::NaiveDateTime,
115 #[derive(Insertable, AsChangeset, Clone)]
116 #[table_name="post_saved"]
117 pub struct PostSavedForm {
122 impl Saveable <PostSavedForm> for PostSaved {
123 fn save(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<Self, Error> {
124 use crate::schema::post_saved::dsl::*;
125 insert_into(post_saved)
126 .values(post_saved_form)
127 .get_result::<Self>(conn)
129 fn unsave(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<usize, Error> {
130 use crate::schema::post_saved::dsl::*;
131 diesel::delete(post_saved
132 .filter(post_id.eq(post_saved_form.post_id))
133 .filter(user_id.eq(post_saved_form.user_id)))
138 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
140 #[table_name = "post_read"]
141 pub struct PostRead {
145 pub published: chrono::NaiveDateTime,
148 #[derive(Insertable, AsChangeset, Clone)]
149 #[table_name="post_read"]
150 pub struct PostReadForm {
155 impl Readable <PostReadForm> for PostRead {
156 fn mark_as_read(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<Self, Error> {
157 use crate::schema::post_read::dsl::*;
158 insert_into(post_read)
159 .values(post_read_form)
160 .get_result::<Self>(conn)
162 fn mark_as_unread(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<usize, Error> {
163 use crate::schema::post_read::dsl::*;
164 diesel::delete(post_read
165 .filter(post_id.eq(post_read_form.post_id))
166 .filter(user_id.eq(post_read_form.user_id)))
174 use super::super::community::*;
175 use super::super::user::*;
178 let conn = establish_connection();
180 let new_user = UserForm {
182 fedi_name: "rrf".into(),
183 preferred_username: None,
184 password_encrypted: "nope".into(),
192 let inserted_user = User_::create(&conn, &new_user).unwrap();
194 let new_community = CommunityForm {
195 name: "test community_3".to_string(),
196 title: "nada".to_owned(),
199 creator_id: inserted_user.id,
206 let inserted_community = Community::create(&conn, &new_community).unwrap();
208 let new_post = PostForm {
209 name: "A test post".into(),
212 creator_id: inserted_user.id,
213 community_id: inserted_community.id,
221 let inserted_post = Post::create(&conn, &new_post).unwrap();
223 let expected_post = Post {
224 id: inserted_post.id,
225 name: "A test post".into(),
228 creator_id: inserted_user.id,
229 community_id: inserted_community.id,
230 published: inserted_post.published,
239 let post_like_form = PostLikeForm {
240 post_id: inserted_post.id,
241 user_id: inserted_user.id,
245 let inserted_post_like = PostLike::like(&conn, &post_like_form).unwrap();
247 let expected_post_like = PostLike {
248 id: inserted_post_like.id,
249 post_id: inserted_post.id,
250 user_id: inserted_user.id,
251 published: inserted_post_like.published,
256 let post_saved_form = PostSavedForm {
257 post_id: inserted_post.id,
258 user_id: inserted_user.id,
261 let inserted_post_saved = PostSaved::save(&conn, &post_saved_form).unwrap();
263 let expected_post_saved = PostSaved {
264 id: inserted_post_saved.id,
265 post_id: inserted_post.id,
266 user_id: inserted_user.id,
267 published: inserted_post_saved.published,
271 let post_read_form = PostReadForm {
272 post_id: inserted_post.id,
273 user_id: inserted_user.id,
276 let inserted_post_read = PostRead::mark_as_read(&conn, &post_read_form).unwrap();
278 let expected_post_read = PostRead {
279 id: inserted_post_read.id,
280 post_id: inserted_post.id,
281 user_id: inserted_user.id,
282 published: inserted_post_read.published,
285 let read_post = Post::read(&conn, inserted_post.id).unwrap();
286 let updated_post = Post::update(&conn, inserted_post.id, &new_post).unwrap();
287 let like_removed = PostLike::remove(&conn, &post_like_form).unwrap();
288 let saved_removed = PostSaved::unsave(&conn, &post_saved_form).unwrap();
289 let read_removed = PostRead::mark_as_unread(&conn, &post_read_form).unwrap();
290 let num_deleted = Post::delete(&conn, inserted_post.id).unwrap();
291 Community::delete(&conn, inserted_community.id).unwrap();
292 User_::delete(&conn, inserted_user.id).unwrap();
294 assert_eq!(expected_post, read_post);
295 assert_eq!(expected_post, inserted_post);
296 assert_eq!(expected_post, updated_post);
297 assert_eq!(expected_post_like, inserted_post_like);
298 assert_eq!(expected_post_saved, inserted_post_saved);
299 assert_eq!(expected_post_read, inserted_post_read);
300 assert_eq!(1, like_removed);
301 assert_eq!(1, saved_removed);
302 assert_eq!(1, read_removed);
303 assert_eq!(1, num_deleted);