diff options
author | Dessalines <tyhou13@gmx.com> | 2020-02-07 23:05:15 -0500 |
---|---|---|
committer | Dessalines <tyhou13@gmx.com> | 2020-02-07 23:05:15 -0500 |
commit | ecd10482a6815fc3b51b2da7cd5f494c01c385e6 (patch) | |
tree | 16d25467ba4224bfd807122806d1f77ddfaee6e8 /server/src/api/comment.rs | |
parent | 779a72581c4c1dee846296cd0f3ea761914aedc6 (diff) |
Add new comments views to main and community pages. Fixes #480
Diffstat (limited to 'server/src/api/comment.rs')
-rw-r--r-- | server/src/api/comment.rs | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/server/src/api/comment.rs b/server/src/api/comment.rs index 775085e9..5c614966 100644 --- a/server/src/api/comment.rs +++ b/server/src/api/comment.rs @@ -2,6 +2,7 @@ use super::*; use crate::send_email; use crate::settings::Settings; use diesel::PgConnection; +use std::str::FromStr; #[derive(Serialize, Deserialize)] pub struct CreateComment { @@ -47,6 +48,21 @@ pub struct CreateCommentLike { auth: String, } +#[derive(Serialize, Deserialize)] +pub struct GetComments { + type_: String, + sort: String, + page: Option<i64>, + limit: Option<i64>, + pub community_id: Option<i32>, + auth: Option<String>, +} + +#[derive(Serialize, Deserialize)] +pub struct GetCommentsResponse { + comments: Vec<CommentView>, +} + impl Perform<CommentResponse> for Oper<CreateComment> { fn perform(&self, conn: &PgConnection) -> Result<CommentResponse, Error> { let data: &CreateComment = &self.data; @@ -456,3 +472,40 @@ impl Perform<CommentResponse> for Oper<CreateCommentLike> { }) } } + +impl Perform<GetCommentsResponse> for Oper<GetComments> { + fn perform(&self, conn: &PgConnection) -> Result<GetCommentsResponse, Error> { + let data: &GetComments = &self.data; + + let user_claims: Option<Claims> = match &data.auth { + Some(auth) => match Claims::decode(&auth) { + Ok(claims) => Some(claims.claims), + Err(_e) => None, + }, + None => None, + }; + + let user_id = match &user_claims { + Some(claims) => Some(claims.id), + None => None, + }; + + let type_ = ListingType::from_str(&data.type_)?; + let sort = SortType::from_str(&data.sort)?; + + let comments = match CommentQueryBuilder::create(&conn) + .listing_type(type_) + .sort(&sort) + .for_community_id(data.community_id) + .my_user_id(user_id) + .page(data.page) + .limit(data.limit) + .list() + { + Ok(comments) => comments, + Err(_e) => return Err(APIError::err("couldnt_get_comments").into()), + }; + + Ok(GetCommentsResponse { comments }) + } +} |