1 use 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>,
20 #[derive(Insertable, AsChangeset, Clone)]
24 pub url: Option<String>,
25 pub body: Option<String>,
27 pub community_id: i32,
28 pub removed: Option<bool>,
29 pub locked: Option<bool>,
30 pub updated: Option<chrono::NaiveDateTime>,
31 pub deleted: Option<bool>,
34 impl Crud<PostForm> for Post {
35 fn read(conn: &PgConnection, post_id: i32) -> Result<Self, Error> {
36 use schema::post::dsl::*;
41 fn delete(conn: &PgConnection, post_id: i32) -> Result<usize, Error> {
42 use schema::post::dsl::*;
43 diesel::delete(post.find(post_id))
47 fn create(conn: &PgConnection, new_post: &PostForm) -> Result<Self, Error> {
48 use schema::post::dsl::*;
51 .get_result::<Self>(conn)
54 fn update(conn: &PgConnection, post_id: i32, new_post: &PostForm) -> Result<Self, Error> {
55 use 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 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 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 schema::post_like::dsl::*;
96 diesel::delete(post_like
97 .filter(post_id.eq(post_like_form.post_id))
98 .filter(user_id.eq(post_like_form.user_id)))
103 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
105 #[table_name = "post_saved"]
106 pub struct PostSaved {
110 pub published: chrono::NaiveDateTime,
113 #[derive(Insertable, AsChangeset, Clone)]
114 #[table_name="post_saved"]
115 pub struct PostSavedForm {
120 impl Saveable <PostSavedForm> for PostSaved {
121 fn save(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<Self, Error> {
122 use schema::post_saved::dsl::*;
123 insert_into(post_saved)
124 .values(post_saved_form)
125 .get_result::<Self>(conn)
127 fn unsave(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<usize, Error> {
128 use schema::post_saved::dsl::*;
129 diesel::delete(post_saved
130 .filter(post_id.eq(post_saved_form.post_id))
131 .filter(user_id.eq(post_saved_form.user_id)))
136 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
138 #[table_name = "post_read"]
139 pub struct PostRead {
143 pub published: chrono::NaiveDateTime,
146 #[derive(Insertable, AsChangeset, Clone)]
147 #[table_name="post_read"]
148 pub struct PostReadForm {
153 impl Readable <PostReadForm> for PostRead {
154 fn mark_as_read(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<Self, Error> {
155 use schema::post_read::dsl::*;
156 insert_into(post_read)
157 .values(post_read_form)
158 .get_result::<Self>(conn)
160 fn mark_as_unread(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<usize, Error> {
161 use schema::post_read::dsl::*;
162 diesel::delete(post_read
163 .filter(post_id.eq(post_read_form.post_id))
164 .filter(user_id.eq(post_read_form.user_id)))
172 use super::super::community::*;
173 use super::super::user::*;
176 let conn = establish_connection();
178 let new_user = UserForm {
180 fedi_name: "rrf".into(),
181 preferred_username: None,
182 password_encrypted: "nope".into(),
189 let inserted_user = User_::create(&conn, &new_user).unwrap();
191 let new_community = CommunityForm {
192 name: "test community_3".to_string(),
193 title: "nada".to_owned(),
196 creator_id: inserted_user.id,
202 let inserted_community = Community::create(&conn, &new_community).unwrap();
204 let new_post = PostForm {
205 name: "A test post".into(),
208 creator_id: inserted_user.id,
209 community_id: inserted_community.id,
216 let inserted_post = Post::create(&conn, &new_post).unwrap();
218 let expected_post = Post {
219 id: inserted_post.id,
220 name: "A test post".into(),
223 creator_id: inserted_user.id,
224 community_id: inserted_community.id,
225 published: inserted_post.published,
233 let post_like_form = PostLikeForm {
234 post_id: inserted_post.id,
235 user_id: inserted_user.id,
239 let inserted_post_like = PostLike::like(&conn, &post_like_form).unwrap();
241 let expected_post_like = PostLike {
242 id: inserted_post_like.id,
243 post_id: inserted_post.id,
244 user_id: inserted_user.id,
245 published: inserted_post_like.published,
250 let post_saved_form = PostSavedForm {
251 post_id: inserted_post.id,
252 user_id: inserted_user.id,
255 let inserted_post_saved = PostSaved::save(&conn, &post_saved_form).unwrap();
257 let expected_post_saved = PostSaved {
258 id: inserted_post_saved.id,
259 post_id: inserted_post.id,
260 user_id: inserted_user.id,
261 published: inserted_post_saved.published,
265 let post_read_form = PostReadForm {
266 post_id: inserted_post.id,
267 user_id: inserted_user.id,
270 let inserted_post_read = PostRead::mark_as_read(&conn, &post_read_form).unwrap();
272 let expected_post_read = PostRead {
273 id: inserted_post_read.id,
274 post_id: inserted_post.id,
275 user_id: inserted_user.id,
276 published: inserted_post_read.published,
279 let read_post = Post::read(&conn, inserted_post.id).unwrap();
280 let updated_post = Post::update(&conn, inserted_post.id, &new_post).unwrap();
281 let like_removed = PostLike::remove(&conn, &post_like_form).unwrap();
282 let saved_removed = PostSaved::unsave(&conn, &post_saved_form).unwrap();
283 let read_removed = PostRead::mark_as_unread(&conn, &post_read_form).unwrap();
284 let num_deleted = Post::delete(&conn, inserted_post.id).unwrap();
285 Community::delete(&conn, inserted_community.id).unwrap();
286 User_::delete(&conn, inserted_user.id).unwrap();
288 assert_eq!(expected_post, read_post);
289 assert_eq!(expected_post, inserted_post);
290 assert_eq!(expected_post, updated_post);
291 assert_eq!(expected_post_like, inserted_post_like);
292 assert_eq!(expected_post_saved, inserted_post_saved);
293 assert_eq!(expected_post_read, inserted_post_read);
294 assert_eq!(1, like_removed);
295 assert_eq!(1, saved_removed);
296 assert_eq!(1, read_removed);
297 assert_eq!(1, num_deleted);