#include "UserController.h" #include "Repository/UserRepository.h" UserController::UserController() { } int UserController::get(HttpRequest *req, HttpResponse *resp) { auto id = QString::fromStdString(req->query_params["id"]); if (!id.isEmpty()) { auto user = UserRepository::findById(id.toLong()); resp->json["id"] = user->id; resp->json["username"] = user->username.toStdString(); } return HTTP_STATUS_OK; } int UserController::post(HttpRequest *req, HttpResponse *resp) { nlohmann::json data = nlohmann::json::parse(req->body.c_str()); QSharedPointer user(new UserEntity()); user->username = QString::fromStdString(data["username"]); UserRepository::create(user); return HTTP_STATUS_CREATED; } int UserController::put(HttpRequest *req, HttpResponse *resp) { nlohmann::json data = nlohmann::json::parse(req->body.c_str()); QSharedPointer user(new UserEntity()); user->id = QString::fromStdString(data["id"]).toLong(); user->username = QString::fromStdString(data["username"]); UserRepository::update(user); return HTTP_STATUS_OK; } int UserController::del(HttpRequest *req, HttpResponse *resp) { auto id = QString::fromStdString(req->query_params["id"]); if (!id.isEmpty()) { bool ret = UserRepository::deleteById(id.toLong()); if (!ret) { return HTTP_STATUS_INTERNAL_SERVER_ERROR; } } return HTTP_STATUS_NO_CONTENT; }