From a784094692026dd6a5fe9125595454d2d1dca17a Mon Sep 17 00:00:00 2001 From: ReLoia Date: Sun, 14 Jun 2026 22:07:13 +0200 Subject: [PATCH] feat: added PATCH /me endpoint --- controllers/AuthController.cc | 21 +++++++++++++++++++++ controllers/AuthController.h | 5 +++++ services/AuthService.cc | 23 +++++++++++++++++++++++ services/AuthService.h | 1 + 4 files changed, 50 insertions(+) diff --git a/controllers/AuthController.cc b/controllers/AuthController.cc index b7630b6..3a4a077 100644 --- a/controllers/AuthController.cc +++ b/controllers/AuthController.cc @@ -117,3 +117,24 @@ void AuthController::me( visione::services::AuthService service; callback(visione::http::json(service.currentUser(*user))); } + +void AuthController::editUser( + const drogon::HttpRequestPtr &req, + std::function &&callback) { + const auto user = visione::auth::authenticate(req); + if (!user) + { + callback(visione::http::error("unauthorized", drogon::k401Unauthorized)); + return; + } + + const auto body = visione::http::requireJson(req); + if (body.isNull()) + { + callback(visione::http::error("invalid_json", drogon::k400BadRequest)); + return; + } + + visione::services::AuthService service; + serviceResponse(service.editUser(*user, body), std::move(callback)); +} diff --git a/controllers/AuthController.h b/controllers/AuthController.h index cfbbd10..9c044e4 100644 --- a/controllers/AuthController.h +++ b/controllers/AuthController.h @@ -22,6 +22,9 @@ class AuthController : public drogon::HttpController "/api/v1/auth/change-password", drogon::Post); ADD_METHOD_TO(AuthController::me, "/api/v1/me", drogon::Get); + ADD_METHOD_TO(AuthController::editUser, + "/api/v1/me", + drogon::Patch); METHOD_LIST_END void registerCitizen( @@ -41,4 +44,6 @@ class AuthController : public drogon::HttpController std::function &&callback); void me(const drogon::HttpRequestPtr &req, std::function &&callback); + void editUser(const drogon::HttpRequestPtr &req, + std::function &&callback); }; diff --git a/services/AuthService.cc b/services/AuthService.cc index 0d6109c..e947d6b 100644 --- a/services/AuthService.cc +++ b/services/AuthService.cc @@ -271,4 +271,27 @@ Json::Value AuthService::currentUser(const auth::UserContext &user) } return response; } + +Json::Value AuthService::editUser(const auth::UserContext &user, Json::Value::const_iterator::reference body) { + const auto allowedFields = {"first_name", "last_name", "phone"}; + if (!body.isMember("field") || !body.isMember("value") || + !body["field"].isString() || !body["value"].isString()) + { + return failure("missing_fields"); + } + const auto field = body["field"].asString(); + if (std::find(allowedFields.begin(), allowedFields.end(), field) == + allowedFields.end()) + { + return failure("invalid_field"); + } + + auto db = drogon::app().getDbClient(); + db->execSqlSync( + "UPDATE users SET " + field + " = ? WHERE id = ?", + body["value"].asString(), + user.id); + + return success(); +} } // namespace visione::services diff --git a/services/AuthService.h b/services/AuthService.h index e9fa18c..852e347 100644 --- a/services/AuthService.h +++ b/services/AuthService.h @@ -15,5 +15,6 @@ class AuthService Json::Value loginWithPassword(const Json::Value &body); Json::Value changePassword(const auth::UserContext &user, const Json::Value &body); Json::Value currentUser(const auth::UserContext &user); + Json::Value editUser(const auth::UserContext & user, Json::Value::const_iterator::reference body); }; } // namespace visione::services