diff options
Diffstat (limited to 'server/src/actions/comment.rs')
-rw-r--r-- | server/src/actions/comment.rs | 110 |
1 files changed, 82 insertions, 28 deletions
diff --git a/server/src/actions/comment.rs b/server/src/actions/comment.rs index f6eee5f1..c3aa0107 100644 --- a/server/src/actions/comment.rs +++ b/server/src/actions/comment.rs @@ -1,9 +1,9 @@ extern crate diesel; -use schema::{comment, comment_like}; +use schema::{comment, comment_like, comment_saved}; use diesel::*; use diesel::result::Error; use serde::{Deserialize, Serialize}; -use {Crud, Likeable}; +use {Crud, Likeable, Saveable}; use actions::post::Post; // WITH RECURSIVE MyTree AS ( @@ -22,7 +22,8 @@ pub struct Comment { pub post_id: i32, pub parent_id: Option<i32>, pub content: String, - pub removed: Option<bool>, + pub removed: bool, + pub read: bool, pub published: chrono::NaiveDateTime, pub updated: Option<chrono::NaiveDateTime> } @@ -38,27 +39,6 @@ pub struct CommentForm { pub updated: Option<chrono::NaiveDateTime> } -#[derive(Identifiable, Queryable, Associations, PartialEq, Debug, Clone)] -#[belongs_to(Comment)] -#[table_name = "comment_like"] -pub struct CommentLike { - pub id: i32, - pub user_id: i32, - pub comment_id: i32, - pub post_id: i32, - pub score: i16, - pub published: chrono::NaiveDateTime, -} - -#[derive(Insertable, AsChangeset, Clone)] -#[table_name="comment_like"] -pub struct CommentLikeForm { - pub user_id: i32, - pub comment_id: i32, - pub post_id: i32, - pub score: i16 -} - impl Crud<CommentForm> for Comment { fn read(conn: &PgConnection, comment_id: i32) -> Result<Self, Error> { use schema::comment::dsl::*; @@ -87,6 +67,27 @@ impl Crud<CommentForm> for Comment { } } +#[derive(Identifiable, Queryable, Associations, PartialEq, Debug, Clone)] +#[belongs_to(Comment)] +#[table_name = "comment_like"] +pub struct CommentLike { + pub id: i32, + pub user_id: i32, + pub comment_id: i32, + pub post_id: i32, + pub score: i16, + pub published: chrono::NaiveDateTime, +} + +#[derive(Insertable, AsChangeset, Clone)] +#[table_name="comment_like"] +pub struct CommentLikeForm { + pub user_id: i32, + pub comment_id: i32, + pub post_id: i32, + pub score: i16 +} + impl Likeable <CommentLikeForm> for CommentLike { fn read(conn: &PgConnection, comment_id_from: i32) -> Result<Vec<Self>, Error> { use schema::comment_like::dsl::*; @@ -119,6 +120,39 @@ impl CommentLike { } } +#[derive(Identifiable, Queryable, Associations, PartialEq, Debug)] +#[belongs_to(Comment)] +#[table_name = "comment_saved"] +pub struct CommentSaved { + pub id: i32, + pub comment_id: i32, + pub user_id: i32, + pub published: chrono::NaiveDateTime, +} + +#[derive(Insertable, AsChangeset, Clone)] +#[table_name="comment_saved"] +pub struct CommentSavedForm { + pub comment_id: i32, + pub user_id: i32, +} + +impl Saveable <CommentSavedForm> for CommentSaved { + fn save(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<Self, Error> { + use schema::comment_saved::dsl::*; + insert_into(comment_saved) + .values(comment_saved_form) + .get_result::<Self>(conn) + } + fn unsave(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<usize, Error> { + use schema::comment_saved::dsl::*; + diesel::delete(comment_saved + .filter(comment_id.eq(comment_saved_form.comment_id)) + .filter(user_id.eq(comment_saved_form.user_id))) + .execute(conn) + } +} + #[cfg(test)] mod tests { use establish_connection; @@ -150,7 +184,7 @@ mod tests { description: None, category_id: 1, creator_id: inserted_user.id, - removed: None, + removed: false, updated: None }; @@ -162,8 +196,8 @@ mod tests { url: None, body: None, community_id: inserted_community.id, - removed: None, - locked: None, + removed: false, + locked: false, updated: None }; @@ -185,7 +219,8 @@ mod tests { content: "A test comment".into(), creator_id: inserted_user.id, post_id: inserted_post.id, - removed: Some(false), + removed: false, + read: false, parent_id: None, published: inserted_comment.published, updated: None @@ -202,6 +237,7 @@ mod tests { let inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap(); + // Comment Like let comment_like_form = CommentLikeForm { comment_id: inserted_comment.id, post_id: inserted_post.id, @@ -220,9 +256,25 @@ mod tests { score: 1 }; + // Comment Saved + let comment_saved_form = CommentSavedForm { + comment_id: inserted_comment.id, + user_id: inserted_user.id, + }; + + let inserted_comment_saved = CommentSaved::save(&conn, &comment_saved_form).unwrap(); + + let expected_comment_saved = CommentSaved { + id: inserted_comment_saved.id, + comment_id: inserted_comment.id, + user_id: inserted_user.id, + published: inserted_comment_saved.published, + }; + let read_comment = Comment::read(&conn, inserted_comment.id).unwrap(); let updated_comment = Comment::update(&conn, inserted_comment.id, &comment_form).unwrap(); let like_removed = CommentLike::remove(&conn, &comment_like_form).unwrap(); + let saved_removed = CommentSaved::unsave(&conn, &comment_saved_form).unwrap(); let num_deleted = Comment::delete(&conn, inserted_comment.id).unwrap(); Comment::delete(&conn, inserted_child_comment.id).unwrap(); Post::delete(&conn, inserted_post.id).unwrap(); @@ -233,8 +285,10 @@ mod tests { assert_eq!(expected_comment, inserted_comment); assert_eq!(expected_comment, updated_comment); assert_eq!(expected_comment_like, inserted_comment_like); + assert_eq!(expected_comment_saved, inserted_comment_saved); assert_eq!(expected_comment.id, inserted_child_comment.parent_id.unwrap()); assert_eq!(1, like_removed); + assert_eq!(1, saved_removed); assert_eq!(1, num_deleted); } |