feat: added PATCH /me endpoint

This commit is contained in:
2026-06-14 22:07:13 +02:00 Unverified
parent cb922baa46
commit a784094692
4 changed files with 50 additions and 0 deletions
+21
View File
@@ -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<void(const drogon::HttpResponsePtr &)> &&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));
}
+5
View File
@@ -22,6 +22,9 @@ class AuthController : public drogon::HttpController<AuthController>
"/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<AuthController>
std::function<void(const drogon::HttpResponsePtr &)> &&callback);
void me(const drogon::HttpRequestPtr &req,
std::function<void(const drogon::HttpResponsePtr &)> &&callback);
void editUser(const drogon::HttpRequestPtr &req,
std::function<void(const drogon::HttpResponsePtr &)> &&callback);
};
+23
View File
@@ -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
+1
View File
@@ -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