2 use crate::schema::{post, post_like, post_read, post_saved};
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>,
22 #[derive(Insertable, AsChangeset, Clone)]
23 #[table_name = "post"]
26 pub url: Option<String>,
27 pub body: Option<String>,
29 pub community_id: i32,
30 pub removed: Option<bool>,
31 pub locked: Option<bool>,
32 pub updated: Option<chrono::NaiveDateTime>,
33 pub deleted: Option<bool>,
35 pub stickied: Option<bool>,
38 impl Crud<PostForm> for Post {
39 fn read(conn: &PgConnection, post_id: i32) -> Result<Self, Error> {
40 use crate::schema::post::dsl::*;
41 post.find(post_id).first::<Self>(conn)
44 fn delete(conn: &PgConnection, post_id: i32) -> Result<usize, Error> {
45 use crate::schema::post::dsl::*;
46 diesel::delete(post.find(post_id)).execute(conn)
49 fn create(conn: &PgConnection, new_post: &PostForm) -> Result<Self, Error> {
50 use crate::schema::post::dsl::*;
51 insert_into(post).values(new_post).get_result::<Self>(conn)
54 fn update(conn: &PgConnection, post_id: i32, new_post: &PostForm) -> Result<Self, Error> {
55 use crate::schema::post::dsl::*;
56 diesel::update(post.find(post_id))
58 .get_result::<Self>(conn)
62 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
64 #[table_name = "post_like"]
70 pub published: chrono::NaiveDateTime,
73 #[derive(Insertable, AsChangeset, Clone)]
74 #[table_name = "post_like"]
75 pub struct PostLikeForm {
81 impl Likeable<PostLikeForm> for PostLike {
82 fn read(conn: &PgConnection, post_id_from: i32) -> Result<Vec<Self>, Error> {
83 use crate::schema::post_like::dsl::*;
85 .filter(post_id.eq(post_id_from))
88 fn like(conn: &PgConnection, post_like_form: &PostLikeForm) -> Result<Self, Error> {
89 use crate::schema::post_like::dsl::*;
90 insert_into(post_like)
91 .values(post_like_form)
92 .get_result::<Self>(conn)
94 fn remove(conn: &PgConnection, post_like_form: &PostLikeForm) -> Result<usize, Error> {
95 use crate::schema::post_like::dsl::*;
98 .filter(post_id.eq(post_like_form.post_id))
99 .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::*;
133 .filter(post_id.eq(post_saved_form.post_id))
134 .filter(user_id.eq(post_saved_form.user_id)),
140 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
142 #[table_name = "post_read"]
143 pub struct PostRead {
147 pub published: chrono::NaiveDateTime,
150 #[derive(Insertable, AsChangeset, Clone)]
151 #[table_name = "post_read"]
152 pub struct PostReadForm {
157 impl Readable<PostReadForm> for PostRead {
158 fn mark_as_read(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<Self, Error> {
159 use crate::schema::post_read::dsl::*;
160 insert_into(post_read)
161 .values(post_read_form)
162 .get_result::<Self>(conn)
164 fn mark_as_unread(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<usize, Error> {
165 use crate::schema::post_read::dsl::*;
168 .filter(post_id.eq(post_read_form.post_id))
169 .filter(user_id.eq(post_read_form.user_id)),
177 use super::super::community::*;
178 use super::super::user::*;
182 let conn = establish_connection();
184 let new_user = UserForm {
186 fedi_name: "rrf".into(),
187 preferred_username: None,
188 password_encrypted: "nope".into(),
194 theme: "darkly".into(),
197 let inserted_user = User_::create(&conn, &new_user).unwrap();
199 let new_community = CommunityForm {
200 name: "test community_3".to_string(),
201 title: "nada".to_owned(),
204 creator_id: inserted_user.id,
211 let inserted_community = Community::create(&conn, &new_community).unwrap();
213 let new_post = PostForm {
214 name: "A test post".into(),
217 creator_id: inserted_user.id,
218 community_id: inserted_community.id,
227 let inserted_post = Post::create(&conn, &new_post).unwrap();
229 let expected_post = Post {
230 id: inserted_post.id,
231 name: "A test post".into(),
234 creator_id: inserted_user.id,
235 community_id: inserted_community.id,
236 published: inserted_post.published,
246 let post_like_form = PostLikeForm {
247 post_id: inserted_post.id,
248 user_id: inserted_user.id,
252 let inserted_post_like = PostLike::like(&conn, &post_like_form).unwrap();
254 let expected_post_like = PostLike {
255 id: inserted_post_like.id,
256 post_id: inserted_post.id,
257 user_id: inserted_user.id,
258 published: inserted_post_like.published,
263 let post_saved_form = PostSavedForm {
264 post_id: inserted_post.id,
265 user_id: inserted_user.id,
268 let inserted_post_saved = PostSaved::save(&conn, &post_saved_form).unwrap();
270 let expected_post_saved = PostSaved {
271 id: inserted_post_saved.id,
272 post_id: inserted_post.id,
273 user_id: inserted_user.id,
274 published: inserted_post_saved.published,
278 let post_read_form = PostReadForm {
279 post_id: inserted_post.id,
280 user_id: inserted_user.id,
283 let inserted_post_read = PostRead::mark_as_read(&conn, &post_read_form).unwrap();
285 let expected_post_read = PostRead {
286 id: inserted_post_read.id,
287 post_id: inserted_post.id,
288 user_id: inserted_user.id,
289 published: inserted_post_read.published,
292 let read_post = Post::read(&conn, inserted_post.id).unwrap();
293 let updated_post = Post::update(&conn, inserted_post.id, &new_post).unwrap();
294 let like_removed = PostLike::remove(&conn, &post_like_form).unwrap();
295 let saved_removed = PostSaved::unsave(&conn, &post_saved_form).unwrap();
296 let read_removed = PostRead::mark_as_unread(&conn, &post_read_form).unwrap();
297 let num_deleted = Post::delete(&conn, inserted_post.id).unwrap();
298 Community::delete(&conn, inserted_community.id).unwrap();
299 User_::delete(&conn, inserted_user.id).unwrap();
301 assert_eq!(expected_post, read_post);
302 assert_eq!(expected_post, inserted_post);
303 assert_eq!(expected_post, updated_post);
304 assert_eq!(expected_post_like, inserted_post_like);
305 assert_eq!(expected_post_saved, inserted_post_saved);
306 assert_eq!(expected_post_read, inserted_post_read);
307 assert_eq!(1, like_removed);
308 assert_eq!(1, saved_removed);
309 assert_eq!(1, read_removed);
310 assert_eq!(1, num_deleted);