Skip to content

Commit

Permalink
fix: messages endopoint path
Browse files Browse the repository at this point in the history
  • Loading branch information
rito528 committed Nov 2, 2024
1 parent aff32da commit 6987cc5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
16 changes: 8 additions & 8 deletions server/entrypoint/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ use presentation::{
form_handler::{
create_form_handler, create_label_for_answers, create_label_for_forms,
create_question_handler, delete_form_comment_handler, delete_form_handler,
delete_label_for_answers, delete_label_for_forms, edit_label_for_answers,
edit_label_for_forms, form_list_handler, get_all_answers, get_answer_by_form_id_handler,
get_answer_handler, get_form_handler, get_labels_for_answers, get_labels_for_forms,
get_messages_handler, get_questions_handler, post_answer_handler, post_form_comment,
post_message_handler, public_form_list_handler, put_question_handler,
replace_answer_labels, replace_form_labels, update_answer_handler, update_form_handler,
update_message_handler,
delete_label_for_answers, delete_label_for_forms, delete_message_handler,
edit_label_for_answers, edit_label_for_forms, form_list_handler, get_all_answers,
get_answer_by_form_id_handler, get_answer_handler, get_form_handler,
get_labels_for_answers, get_labels_for_forms, get_messages_handler, get_questions_handler,
post_answer_handler, post_form_comment, post_message_handler, public_form_list_handler,
put_question_handler, replace_answer_labels, replace_form_labels, update_answer_handler,
update_form_handler, update_message_handler,
},
health_check_handler::health_check,
search_handler::cross_search,
Expand Down Expand Up @@ -150,7 +150,7 @@ async fn main() -> anyhow::Result<()> {
.with_state(shared_repository.to_owned())
.route(
"/forms/answers/:answer_id/messages/:message_id",
delete(delete_form_comment_handler).patch(update_message_handler),
delete(delete_message_handler).patch(update_message_handler),
)
.with_state(shared_repository.to_owned())
.route("/health", get(health_check))
Expand Down
11 changes: 7 additions & 4 deletions server/presentation/src/form_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,15 +597,15 @@ pub async fn post_message_handler(
pub async fn update_message_handler(
Extension(user): Extension<User>,
State(repository): State<RealInfrastructureRepository>,
Path(message_id): Path<MessageId>,
Path((answer_id, message_id)): Path<(AnswerId, MessageId)>,
Json(body_schema): Json<MessageUpdateSchema>,
) -> impl IntoResponse {
let form_use_case = FormUseCase {
repository: repository.form_repository(),
};

match form_use_case
.update_message_body(&user, &message_id, body_schema.body)
.update_message_body(&user, &answer_id, &message_id, body_schema.body)
.await
{
Ok(_) => StatusCode::NO_CONTENT.into_response(),
Expand Down Expand Up @@ -669,13 +669,16 @@ pub async fn get_messages_handler(
pub async fn delete_message_handler(
Extension(user): Extension<User>,
State(repository): State<RealInfrastructureRepository>,
Path(message_id): Path<MessageId>,
Path((answer_id, message_id)): Path<(AnswerId, MessageId)>,
) -> impl IntoResponse {
let form_use_case = FormUseCase {
repository: repository.form_repository(),
};

match form_use_case.delete_message(&user, &message_id).await {
match form_use_case
.delete_message(&user, &answer_id, &message_id)
.await
{
Ok(_) => StatusCode::NO_CONTENT.into_response(),
Err(err) => handle_error(err).into_response(),
}
Expand Down
16 changes: 15 additions & 1 deletion server/usecase/src/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ impl<R: FormRepository> FormUseCase<'_, R> {
pub async fn update_message_body(
&self,
actor: &User,
answer_id: &AnswerId,
message_id: &MessageId,
body: String,
) -> Result<(), Error> {
Expand All @@ -381,18 +382,31 @@ impl<R: FormRepository> FormUseCase<'_, R> {
.await?
.ok_or(MessageNotFound)?;

if &message.try_read(actor)?.related_answer().id != answer_id {
return Err(Error::from(MessageNotFound));
}

self.repository
.update_message_body(actor, message.into_update(), body)
.await
}

pub async fn delete_message(&self, actor: &User, message_id: &MessageId) -> Result<(), Error> {
pub async fn delete_message(
&self,
actor: &User,
answer_id: &AnswerId,
message_id: &MessageId,
) -> Result<(), Error> {
let message = self
.repository
.fetch_message(message_id)
.await?
.ok_or(MessageNotFound)?;

if &message.try_read(actor)?.related_answer().id != answer_id {
return Err(Error::from(MessageNotFound));
}

self.repository
.delete_message(actor, message.into_delete())
.await
Expand Down

0 comments on commit 6987cc5

Please sign in to comment.