verion which did not include the handling in RTService wth creating the
Json file
This commit is contained in:
114
vrpmdvaotserver/src/service/MonitoringService.cpp
Normal file
114
vrpmdvaotserver/src/service/MonitoringService.cpp
Normal file
@@ -0,0 +1,114 @@
|
||||
|
||||
#include "MonitoringService.hpp"
|
||||
|
||||
oatpp::Object<MonitoringDto> MonitoringService::createMonitoring(const oatpp::Object<InputMonitoringDto>& dto) {
|
||||
|
||||
time_t _tm =time(NULL );
|
||||
struct tm * curtime = localtime ( &_tm );
|
||||
oatpp:: String created_at = asctime(curtime);
|
||||
|
||||
auto dbResult = m_database->createMonitoring(dto, created_at);
|
||||
OATPP_ASSERT_HTTP(dbResult->isSuccess(), Status::CODE_500, dbResult->getErrorMessage());
|
||||
|
||||
auto id = oatpp::sqlite::Utils::getLastInsertRowId(dbResult->getConnection());
|
||||
|
||||
//now init the montask and create the monitoring
|
||||
rtsMonTask.Init();
|
||||
//int isamplerate = dto->samplerate;
|
||||
//rtsMonTask.CreateMonitoring(id, dto->samplerate, dto->sampleperiod, dto->downtime, dto->status);
|
||||
|
||||
return getMonitoringById((v_int32) id);
|
||||
|
||||
}
|
||||
|
||||
oatpp::Object<MonitoringDto> MonitoringService::updateMonitoring(const oatpp::Object<MonitoringDto>& dto, const oatpp::Int32& id) {
|
||||
|
||||
auto dbResult = m_database->updateMonitoring(dto, id);
|
||||
OATPP_ASSERT_HTTP(dbResult->isSuccess(), Status::CODE_500, dbResult->getErrorMessage());
|
||||
return getMonitoringById(id);
|
||||
|
||||
}
|
||||
|
||||
oatpp::Object<MonitoringDto> MonitoringService::getMonitoringById(const oatpp::Int32& id, const oatpp::provider::ResourceHandle<oatpp::orm::Connection>& connection) {
|
||||
|
||||
auto dbResult = m_database->getMonitoringById(id);
|
||||
OATPP_ASSERT_HTTP(dbResult->isSuccess(), Status::CODE_500, dbResult->getErrorMessage());
|
||||
OATPP_ASSERT_HTTP(dbResult->hasMoreToFetch(), Status::CODE_404, "Monitoring not found");
|
||||
|
||||
auto result = dbResult->fetch<oatpp::Vector<oatpp::Object<MonitoringDto>>>();
|
||||
OATPP_ASSERT_HTTP(result->size() == 1, Status::CODE_500, "Unknown error");
|
||||
|
||||
return result[0];
|
||||
|
||||
}
|
||||
|
||||
oatpp::Object<PageDto<oatpp::Object<MonitoringDto>>> MonitoringService::getSelectedMonitorings(const oatpp::UInt32& offset, const oatpp::UInt32& limit) {
|
||||
|
||||
oatpp::UInt32 countToFetch = limit;
|
||||
|
||||
if(limit > 10) {
|
||||
countToFetch = 10;
|
||||
}
|
||||
|
||||
auto dbResult = m_database->getSelectedMonitorings(offset, countToFetch);
|
||||
OATPP_ASSERT_HTTP(dbResult->isSuccess(), Status::CODE_500, dbResult->getErrorMessage());
|
||||
|
||||
auto items = dbResult->fetch<oatpp::Vector<oatpp::Object<MonitoringDto>>>();
|
||||
|
||||
auto page = PageDto<oatpp::Object<MonitoringDto>>::createShared();
|
||||
page->offset = offset;
|
||||
page->limit = countToFetch;
|
||||
page->count = items->size();
|
||||
page->items = items;
|
||||
|
||||
return page;
|
||||
|
||||
}
|
||||
|
||||
|
||||
oatpp::Vector<oatpp::Object<MonitoringDto>> MonitoringService::getAllMonitorings() {
|
||||
|
||||
auto dbResult = m_database->getAllMonitorings();
|
||||
OATPP_ASSERT_HTTP(dbResult->isSuccess(), Status::CODE_500, dbResult->getErrorMessage());
|
||||
|
||||
auto items = dbResult->fetch<oatpp::Vector<oatpp::Object<MonitoringDto>>>();
|
||||
|
||||
return items;
|
||||
|
||||
}
|
||||
|
||||
// oatpp::Vector<oatpp::Object<MonitoringDto>> MonitoringService::getAllMonitorings() {
|
||||
|
||||
// auto dbResult = m_database->getAllMonitorings();
|
||||
// OATPP_ASSERT_HTTP(dbResult->isSuccess(), Status::CODE_500, dbResult->getErrorMessage());
|
||||
|
||||
// auto items = dbResult->fetch<oatpp::Vector<oatpp::Object<MonitoringDto>>>();
|
||||
|
||||
// return items;
|
||||
|
||||
// }
|
||||
|
||||
// oatpp::Object<MonPageDto<oatpp::Object<MonitoringDto>>> MonitoringService::getAllMonitorings() {
|
||||
|
||||
// auto dbResult = m_database->getAllMonitorings();
|
||||
// OATPP_ASSERT_HTTP(dbResult->isSuccess(), Status::CODE_500, dbResult->getErrorMessage());
|
||||
|
||||
// auto items = dbResult->fetch<oatpp::Vector<oatpp::Object<MonitoringDto>>>();
|
||||
|
||||
// auto page = MonPageDto<oatpp::Object<MonitoringDto>>::createShared();
|
||||
// page->items = items;
|
||||
|
||||
// return page;
|
||||
|
||||
// }
|
||||
|
||||
|
||||
oatpp::Object<StatusDto> MonitoringService::deleteMonitoringById(const oatpp::Int32& id) {
|
||||
auto dbResult = m_database->deleteMonitoringById(id);
|
||||
OATPP_ASSERT_HTTP(dbResult->isSuccess(), Status::CODE_500, dbResult->getErrorMessage());
|
||||
auto status = StatusDto::createShared();
|
||||
status->status = "OK";
|
||||
status->code = 200;
|
||||
status->message = "Monitoring was successfully deleted";
|
||||
return status;
|
||||
}
|
||||
34
vrpmdvaotserver/src/service/MonitoringService.hpp
Normal file
34
vrpmdvaotserver/src/service/MonitoringService.hpp
Normal file
@@ -0,0 +1,34 @@
|
||||
|
||||
#ifndef VRPMD_MONITORINGSERVICE_HPP
|
||||
#define VRPMD_MONITORINGSERVICE_HPP
|
||||
|
||||
#include "db/MonitoringDb.hpp"
|
||||
#include "dto/PageDto.hpp"
|
||||
#include "dto/StatusDto.hpp"
|
||||
|
||||
#include "oatpp/web/protocol/http/Http.hpp"
|
||||
#include "oatpp/core/macro/component.hpp"
|
||||
|
||||
#include "../RTSMonitoring/RTSMonitoringTask.h"
|
||||
|
||||
class MonitoringService {
|
||||
private:
|
||||
typedef oatpp::web::protocol::http::Status Status;
|
||||
|
||||
private:
|
||||
OATPP_COMPONENT(std::shared_ptr<MonitoringDb>, m_database); // Inject database component
|
||||
|
||||
RTSMonitoringTask rtsMonTask;
|
||||
public:
|
||||
|
||||
oatpp::Object<MonitoringDto> createMonitoring(const oatpp::Object<InputMonitoringDto>& dto);
|
||||
oatpp::Object<MonitoringDto> updateMonitoring(const oatpp::Object<MonitoringDto>& dto, const oatpp::Int32& id);
|
||||
oatpp::Object<MonitoringDto> getMonitoringById(const oatpp::Int32& id, const oatpp::provider::ResourceHandle<oatpp::orm::Connection>& connection = nullptr);
|
||||
oatpp::Object<PageDto<oatpp::Object<MonitoringDto>>> getSelectedMonitorings(const oatpp::UInt32& offset, const oatpp::UInt32& limit);
|
||||
//oatpp::List<oatpp::Object<MonitoringDto>> getAllMonitorings();
|
||||
oatpp::Vector<oatpp::Object<MonitoringDto>> getAllMonitorings();
|
||||
oatpp::Object<StatusDto> deleteMonitoringById(const oatpp::Int32& id);
|
||||
//oatpp::Object<MonPageDto<oatpp::Object<MonitoringDto>>> getAllMonitorings();
|
||||
};
|
||||
|
||||
#endif //VRPMDV_MONITORINGERVICE_HPP
|
||||
67
vrpmdvaotserver/src/service/UserService.cpp
Normal file
67
vrpmdvaotserver/src/service/UserService.cpp
Normal file
@@ -0,0 +1,67 @@
|
||||
|
||||
#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;
|
||||
}
|
||||
29
vrpmdvaotserver/src/service/UserService.hpp
Normal file
29
vrpmdvaotserver/src/service/UserService.hpp
Normal file
@@ -0,0 +1,29 @@
|
||||
|
||||
#ifndef VRPMDV_USERSERVICE_HPP
|
||||
#define VRPMDV_USERSERVICE_HPP
|
||||
|
||||
//#include "db/UserDb.hpp"
|
||||
#include "db/MonitoringDb.hpp"
|
||||
#include "dto/PageDto.hpp"
|
||||
#include "dto/StatusDto.hpp"
|
||||
|
||||
#include "oatpp/web/protocol/http/Http.hpp"
|
||||
#include "oatpp/core/macro/component.hpp"
|
||||
|
||||
class UserService {
|
||||
private:
|
||||
typedef oatpp::web::protocol::http::Status Status;
|
||||
private:
|
||||
// OATPP_COMPONENT(std::shared_ptr<UserDb>, m_database); // Inject database component
|
||||
OATPP_COMPONENT(std::shared_ptr<MonitoringDb>, m_database); // Inject database component
|
||||
public:
|
||||
|
||||
oatpp::Object<UserDto> createUser(const oatpp::Object<UserDto>& dto);
|
||||
oatpp::Object<UserDto> updateUser(const oatpp::Object<UserDto>& dto);
|
||||
oatpp::Object<UserDto> getUserById(const oatpp::Int32& id, const oatpp::provider::ResourceHandle<oatpp::orm::Connection>& connection = nullptr);
|
||||
oatpp::Object<PageDto<oatpp::Object<UserDto>>> getAllUsers(const oatpp::UInt32& offset, const oatpp::UInt32& limit);
|
||||
oatpp::Object<StatusDto> deleteUserById(const oatpp::Int32& id);
|
||||
|
||||
};
|
||||
|
||||
#endif //CRUD_USERSERVICE_HPP
|
||||
Reference in New Issue
Block a user