feat: REST endpont return comment ID after creation

master
Aravinth Manivannan 2022-02-19 21:41:30 +05:30
parent c252f06b16
commit c58a6f1fae
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
1 changed files with 21 additions and 2 deletions

View File

@ -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");