From 59cd6f34cb1bbe9811ceea3bf9a2f7064d5209cb Mon Sep 17 00:00:00 2001 From: gabdevele Date: Thu, 18 Jun 2026 19:29:35 +0200 Subject: [PATCH] feat: add feedback existence check and update API spec for feedback submission --- controllers/ReportController.cc | 3 ++- docs/API_SPEC.md | 2 +- services/ReportService.cc | 30 +++++++++++++++++++++++------- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/controllers/ReportController.cc b/controllers/ReportController.cc index 739e2c8..3a45b84 100644 --- a/controllers/ReportController.cc +++ b/controllers/ReportController.cc @@ -57,7 +57,8 @@ void sendService(Json::Value body, else if (error == "invalid_status_transition" || error == "media_limit_reached" || error == "report_not_open" || - error == "report_owned_by_other_operator") + error == "report_owned_by_other_operator" || + error == "feedback_already_exists") { status = drogon::k409Conflict; } diff --git a/docs/API_SPEC.md b/docs/API_SPEC.md index ba6a587..e7d4917 100644 --- a/docs/API_SPEC.md +++ b/docs/API_SPEC.md @@ -74,7 +74,7 @@ Authorization: Bearer - `POST /reports/{id}/media` max 5 total media, image/video only - `POST /reports/{id}/feedback` - only owner, only when status is `RESOLVED` + only owner, only when status is `RESOLVED`, one feedback per report ## Operator Workflow - `GET /operator/teams` diff --git a/services/ReportService.cc b/services/ReportService.cc index 689397d..5c2ef39 100644 --- a/services/ReportService.cc +++ b/services/ReportService.cc @@ -7,6 +7,7 @@ #include "services/UserService.h" #include +#include #include #include @@ -1057,13 +1058,28 @@ Json::Value ReportService::addFeedback(const auth::UserContext &user, return failure("report_not_resolved"); } - db->execSqlSync( - "INSERT INTO feedback (report_id, user_id, rating, comment) " - "VALUES (?, ?, ?, ?)", - reportId, - user.id, - rating, - body.get("comment", "").asString()); + const auto existingFeedback = db->execSqlSync( + "SELECT id FROM feedback WHERE report_id = ?", + reportId); + if (!existingFeedback.empty()) + { + return failure("feedback_already_exists"); + } + + try + { + db->execSqlSync( + "INSERT INTO feedback (report_id, user_id, rating, comment) " + "VALUES (?, ?, ?, ?)", + reportId, + user.id, + rating, + body.get("comment", "").asString()); + } + catch (const drogon::orm::DrogonDbException &) + { + return failure("feedback_already_exists"); + } return success(); }