Skip to content

Commit

Permalink
fix(backend): make create_comment endpoint sensible (#414)
Browse files Browse the repository at this point in the history
* feat(backend): remove user id from new comment input

* fix(backend): use put instead of post for create_comment
  • Loading branch information
zax-xyz authored Mar 27, 2023
1 parent 00c11d7 commit 485a948
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
22 changes: 16 additions & 6 deletions backend/server/src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,33 @@ use crate::database::{
};
use crate::error::JsonErr;
use rocket::{
form::Form,
get,
http::Status,
post,
serde::{json::Json, Serialize},
put,
serde::{json::Json, Deserialize, Serialize},
};

#[derive(Deserialize)]
pub struct NewCommentInput {
pub application_id: i32,
pub description: String,
}

#[derive(Serialize)]
pub enum CommentError {
Unauthorized,
CouldNotInsert,
CommentNotFound,
}

#[post("/", data = "<new_comment>")]
#[put("/", data = "<new_comment_input>")]
pub async fn create_comment(
new_comment: Form<NewComment>,
new_comment_input: Json<NewCommentInput>,
user: User,
db: Database,
) -> Result<Json<Comment>, JsonErr<CommentError>> {
// need to be director to comment
let app_id = new_comment.application_id; // stack copy of i32
let app_id = new_comment_input.application_id; // stack copy of i32
db.run(move |conn| {
OrganisationUser::application_admin_level(app_id, user.id, &conn)
.is_at_least_director()
Expand All @@ -34,6 +39,11 @@ pub async fn create_comment(
.await
.or_else(|_| Err(JsonErr(CommentError::Unauthorized, Status::Forbidden)))?;

let new_comment = NewComment {
application_id: new_comment_input.application_id,
commenter_user_id: user.id,
description: new_comment_input.description.to_string(),
};
let comment = db
.run(move |conn| NewComment::insert(&new_comment, conn))
.await
Expand Down
2 changes: 1 addition & 1 deletion backend/server/src/database/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,7 @@ pub struct Comment {
pub updated_at: NaiveDateTime,
}

#[derive(Insertable, FromForm)]
#[derive(Insertable)]
#[table_name = "comments"]
pub struct NewComment {
pub application_id: i32,
Expand Down

0 comments on commit 485a948

Please sign in to comment.