67 lines
2.2 KiB
C++
67 lines
2.2 KiB
C++
|
|
#include "UserService.hpp"
|
|
|
|
oatpp::Object<UserDto> UserService::createUser(const oatpp::Object<UserDto>& dto) {
|
|
|
|
auto dbResult = m_database->createUser(dto);
|
|
OATPP_ASSERT_HTTP(dbResult->isSuccess(), Status::CODE_500, dbResult->getErrorMessage());
|
|
|
|
auto userId = oatpp::sqlite::Utils::getLastInsertRowId(dbResult->getConnection());
|
|
|
|
return getUserById((v_int32) userId);
|
|
|
|
}
|
|
|
|
oatpp::Object<UserDto> UserService::updateUser(const oatpp::Object<UserDto>& dto) {
|
|
|
|
auto dbResult = m_database->updateUser(dto);
|
|
OATPP_ASSERT_HTTP(dbResult->isSuccess(), Status::CODE_500, dbResult->getErrorMessage());
|
|
return getUserById(dto->id);
|
|
|
|
}
|
|
|
|
oatpp::Object<UserDto> UserService::getUserById(const oatpp::Int32& id, const oatpp::provider::ResourceHandle<oatpp::orm::Connection>& connection) {
|
|
|
|
auto dbResult = m_database->getUserById(id, connection);
|
|
OATPP_ASSERT_HTTP(dbResult->isSuccess(), Status::CODE_500, dbResult->getErrorMessage());
|
|
OATPP_ASSERT_HTTP(dbResult->hasMoreToFetch(), Status::CODE_404, "User not found");
|
|
|
|
auto result = dbResult->fetch<oatpp::Vector<oatpp::Object<UserDto>>>();
|
|
OATPP_ASSERT_HTTP(result->size() == 1, Status::CODE_500, "Unknown error");
|
|
|
|
return result[0];
|
|
|
|
}
|
|
|
|
oatpp::Object<PageDto<oatpp::Object<UserDto>>> UserService::getAllUsers(const oatpp::UInt32& offset, const oatpp::UInt32& limit) {
|
|
|
|
oatpp::UInt32 countToFetch = limit;
|
|
|
|
if(limit > 10) {
|
|
countToFetch = 10;
|
|
}
|
|
|
|
auto dbResult = m_database->getAllUsers(offset, countToFetch);
|
|
OATPP_ASSERT_HTTP(dbResult->isSuccess(), Status::CODE_500, dbResult->getErrorMessage());
|
|
|
|
auto items = dbResult->fetch<oatpp::Vector<oatpp::Object<UserDto>>>();
|
|
|
|
auto page = PageDto<oatpp::Object<UserDto>>::createShared();
|
|
page->offset = offset;
|
|
page->limit = countToFetch;
|
|
page->count = items->size();
|
|
page->items = items;
|
|
|
|
return page;
|
|
|
|
}
|
|
|
|
oatpp::Object<StatusDto> UserService::deleteUserById(const oatpp::Int32& userId) {
|
|
auto dbResult = m_database->deleteUserById(userId);
|
|
OATPP_ASSERT_HTTP(dbResult->isSuccess(), Status::CODE_500, dbResult->getErrorMessage());
|
|
auto status = StatusDto::createShared();
|
|
status->status = "OK";
|
|
status->code = 200;
|
|
status->message = "User was successfully deleted";
|
|
return status;
|
|
} |