From c58a6f1fae64530b04261c7e82ac2433768e62b0 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Sat, 19 Feb 2022 21:41:30 +0530 Subject: [PATCH] feat: REST endpont return comment ID after creation --- src/api/v1/gists.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/api/v1/gists.rs b/src/api/v1/gists.rs index 1ebb0e6..9db8434 100644 --- a/src/api/v1/gists.rs +++ b/src/api/v1/gists.rs @@ -122,6 +122,11 @@ pub struct PostCommentRequest { pub comment: String, } +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct PostCommentResp { + pub id: i64, +} + #[my_codegen::post( path = "crate::V1_API_ROUTES.gist.post_comment", wrap = "super::get_auth_middleware()" @@ -149,8 +154,10 @@ async fn post_comment( comment: &payload.comment, }; - db.new_comment(&msg).await?; // TODO get comment ID - Ok(HttpResponse::Ok().finish()) + let resp = PostCommentResp { + id: db.new_comment(&msg).await?, + }; + Ok(HttpResponse::Ok().json(resp)) } #[cfg(test)] @@ -370,6 +377,11 @@ mod tests { gist: gist_id.clone(), }; + /* +++++++++++++++++++++++++++++++ + * COMMENTS + * +++++++++++++++++++++++++++++++ + */ + let mut comment = PostCommentRequest { comment: "".into() }; println!("empty comment"); // empty comment @@ -398,6 +410,8 @@ mod tests { ) .await; + let mut comment_ids = Vec::with_capacity(2); + println!("comment OK"); create_comment.gist = gist_id; let post_comment_path = V1_API_ROUTES.gist.get_post_comment_route(&create_comment); @@ -409,6 +423,8 @@ mod tests { ) .await; assert_eq!(resp.status(), StatusCode::OK); + let comment_resp: PostCommentResp = test::read_body_json(resp).await; + comment_ids.push(comment_resp); println!("comment OK"); create_comment.gist = unlisted; @@ -421,6 +437,8 @@ mod tests { ) .await; assert_eq!(resp.status(), StatusCode::OK); + let comment_resp: PostCommentResp = test::read_body_json(resp).await; + comment_ids.push(comment_resp); println!("comment OK"); create_comment.gist = private.clone(); @@ -433,6 +451,7 @@ mod tests { ) .await; assert_eq!(resp.status(), StatusCode::OK); + let _comment_resp: PostCommentResp = test::read_body_json(resp).await; // commenting on private gist println!("private gist, not OK");