You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.5 KiB
57 lines
1.5 KiB
3 days ago
|
#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<UserEntity> 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<UserEntity> 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;
|
||
|
}
|