1 use crate::schema::{comment, comment_like, comment_saved};
5 // WITH RECURSIVE MyTree AS (
6 // SELECT * FROM comment WHERE parent_id IS NULL
8 // SELECT m.* FROM comment AS m JOIN MyTree AS t ON m.parent_id = t.id
10 // SELECT * FROM MyTree;
12 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
14 #[table_name="comment"]
19 pub parent_id: Option<i32>,
23 pub published: chrono::NaiveDateTime,
24 pub updated: Option<chrono::NaiveDateTime>,
28 #[derive(Insertable, AsChangeset, Clone)]
29 #[table_name="comment"]
30 pub struct CommentForm {
33 pub parent_id: Option<i32>,
35 pub removed: Option<bool>,
36 pub read: Option<bool>,
37 pub updated: Option<chrono::NaiveDateTime>,
38 pub deleted: Option<bool>,
41 impl Crud<CommentForm> for Comment {
42 fn read(conn: &PgConnection, comment_id: i32) -> Result<Self, Error> {
43 use crate::schema::comment::dsl::*;
44 comment.find(comment_id)
48 fn delete(conn: &PgConnection, comment_id: i32) -> Result<usize, Error> {
49 use crate::schema::comment::dsl::*;
50 diesel::delete(comment.find(comment_id))
54 fn create(conn: &PgConnection, comment_form: &CommentForm) -> Result<Self, Error> {
55 use crate::schema::comment::dsl::*;
58 .get_result::<Self>(conn)
61 fn update(conn: &PgConnection, comment_id: i32, comment_form: &CommentForm) -> Result<Self, Error> {
62 use crate::schema::comment::dsl::*;
63 diesel::update(comment.find(comment_id))
65 .get_result::<Self>(conn)
69 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug, Clone)]
70 #[belongs_to(Comment)]
71 #[table_name = "comment_like"]
72 pub struct CommentLike {
78 pub published: chrono::NaiveDateTime,
81 #[derive(Insertable, AsChangeset, Clone)]
82 #[table_name="comment_like"]
83 pub struct CommentLikeForm {
90 impl Likeable <CommentLikeForm> for CommentLike {
91 fn read(conn: &PgConnection, comment_id_from: i32) -> Result<Vec<Self>, Error> {
92 use crate::schema::comment_like::dsl::*;
94 .filter(comment_id.eq(comment_id_from))
98 fn like(conn: &PgConnection, comment_like_form: &CommentLikeForm) -> Result<Self, Error> {
99 use crate::schema::comment_like::dsl::*;
100 insert_into(comment_like)
101 .values(comment_like_form)
102 .get_result::<Self>(conn)
104 fn remove(conn: &PgConnection, comment_like_form: &CommentLikeForm) -> Result<usize, Error> {
105 use crate::schema::comment_like::dsl::*;
108 .filter(comment_id.eq(comment_like_form.comment_id))
109 .filter(user_id.eq(comment_like_form.user_id)))
115 pub fn from_post(conn: &PgConnection, post_id_from: i32) -> Result<Vec<Self>, Error> {
116 use crate::schema::comment_like::dsl::*;
118 .filter(post_id.eq(post_id_from))
123 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
124 #[belongs_to(Comment)]
125 #[table_name = "comment_saved"]
126 pub struct CommentSaved {
130 pub published: chrono::NaiveDateTime,
133 #[derive(Insertable, AsChangeset, Clone)]
134 #[table_name="comment_saved"]
135 pub struct CommentSavedForm {
140 impl Saveable <CommentSavedForm> for CommentSaved {
141 fn save(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<Self, Error> {
142 use crate::schema::comment_saved::dsl::*;
143 insert_into(comment_saved)
144 .values(comment_saved_form)
145 .get_result::<Self>(conn)
147 fn unsave(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<usize, Error> {
148 use crate::schema::comment_saved::dsl::*;
149 diesel::delete(comment_saved
150 .filter(comment_id.eq(comment_saved_form.comment_id))
151 .filter(user_id.eq(comment_saved_form.user_id)))
159 use super::super::post::*;
160 use super::super::community::*;
161 use super::super::user::*;
164 let conn = establish_connection();
166 let new_user = UserForm {
167 name: "terry".into(),
168 fedi_name: "rrf".into(),
169 preferred_username: None,
170 password_encrypted: "nope".into(),
178 let inserted_user = User_::create(&conn, &new_user).unwrap();
180 let new_community = CommunityForm {
181 name: "test community".to_string(),
182 title: "nada".to_owned(),
185 creator_id: inserted_user.id,
192 let inserted_community = Community::create(&conn, &new_community).unwrap();
194 let new_post = PostForm {
195 name: "A test post".into(),
196 creator_id: inserted_user.id,
199 community_id: inserted_community.id,
207 let inserted_post = Post::create(&conn, &new_post).unwrap();
209 let comment_form = CommentForm {
210 content: "A test comment".into(),
211 creator_id: inserted_user.id,
212 post_id: inserted_post.id,
220 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
222 let expected_comment = Comment {
223 id: inserted_comment.id,
224 content: "A test comment".into(),
225 creator_id: inserted_user.id,
226 post_id: inserted_post.id,
231 published: inserted_comment.published,
235 let child_comment_form = CommentForm {
236 content: "A child comment".into(),
237 creator_id: inserted_user.id,
238 post_id: inserted_post.id,
239 parent_id: Some(inserted_comment.id),
246 let inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
249 let comment_like_form = CommentLikeForm {
250 comment_id: inserted_comment.id,
251 post_id: inserted_post.id,
252 user_id: inserted_user.id,
256 let inserted_comment_like = CommentLike::like(&conn, &comment_like_form).unwrap();
258 let expected_comment_like = CommentLike {
259 id: inserted_comment_like.id,
260 comment_id: inserted_comment.id,
261 post_id: inserted_post.id,
262 user_id: inserted_user.id,
263 published: inserted_comment_like.published,
268 let comment_saved_form = CommentSavedForm {
269 comment_id: inserted_comment.id,
270 user_id: inserted_user.id,
273 let inserted_comment_saved = CommentSaved::save(&conn, &comment_saved_form).unwrap();
275 let expected_comment_saved = CommentSaved {
276 id: inserted_comment_saved.id,
277 comment_id: inserted_comment.id,
278 user_id: inserted_user.id,
279 published: inserted_comment_saved.published,
282 let read_comment = Comment::read(&conn, inserted_comment.id).unwrap();
283 let updated_comment = Comment::update(&conn, inserted_comment.id, &comment_form).unwrap();
284 let like_removed = CommentLike::remove(&conn, &comment_like_form).unwrap();
285 let saved_removed = CommentSaved::unsave(&conn, &comment_saved_form).unwrap();
286 let num_deleted = Comment::delete(&conn, inserted_comment.id).unwrap();
287 Comment::delete(&conn, inserted_child_comment.id).unwrap();
288 Post::delete(&conn, inserted_post.id).unwrap();
289 Community::delete(&conn, inserted_community.id).unwrap();
290 User_::delete(&conn, inserted_user.id).unwrap();
292 assert_eq!(expected_comment, read_comment);
293 assert_eq!(expected_comment, inserted_comment);
294 assert_eq!(expected_comment, updated_comment);
295 assert_eq!(expected_comment_like, inserted_comment_like);
296 assert_eq!(expected_comment_saved, inserted_comment_saved);
297 assert_eq!(expected_comment.id, inserted_child_comment.parent_id.unwrap());
298 assert_eq!(1, like_removed);
299 assert_eq!(1, saved_removed);
300 assert_eq!(1, num_deleted);