Skip to content

Commit

Permalink
check if entity exists before update, delete and read
Browse files Browse the repository at this point in the history
  • Loading branch information
dr3mro committed Nov 26, 2024
1 parent 82938e6 commit 93e0acb
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
5 changes: 5 additions & 0 deletions src/controllers/base/controller/controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ class Controller
template <typename T>
void Read(T &entity, CALLBACK_ &&callback)
{
if (!entity.template check_id_exists<Types::Read_t>())
{
callback(HttpStatus::BAD_REQUEST, "ID does not exist");
return;
}
std::optional<std::string> (T::*sqlstatement)() = &T::getSqlReadStatement;
cruds(entity, sqlstatement, dbrexec, std::forward<CALLBACK_>(callback));
}
Expand Down
7 changes: 7 additions & 0 deletions src/controllers/clientcontroller/clientcontroller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ void ClientController<T>::Update(CALLBACK_&& callback, std::string_view data, co
if (success)
{
T client(client_data);

if (!client.template check_id_exists<Types::UpdateClient_t>())
{
callback(HttpStatus::BAD_REQUEST, "ID does not exist");
return;
}

Controller::Update(client, std::move(callback));
}
else
Expand Down
25 changes: 16 additions & 9 deletions src/controllers/entitycontroller/entitycontroller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,30 @@ void EntityController<T>::Create(CALLBACK_ &&callback, std::string_view data)
api::v2::Global::HttpError error;
std::unordered_set<std::string> exclude;
auto next_id = this->template getNextID<T>(error);

if (!next_id.has_value())
{
callback(error.code, fmt::format("Failed to generate next ID, {}.", error.message));
return;
}

std::optional<jsoncons::json> request_json = jsoncons::json::parse(data);
std::optional<jsoncons::json> request_j = jsoncons::json::parse(data);

if (!request_json.has_value())
if (!request_j.has_value())
{
callback(HttpStatus::Code::BAD_REQUEST, "Invalid request body.");
return;
}

success = Validator::validateDatabaseCreateSchema(T::getTableName(), request_json, error);
success = Validator::validateDatabaseCreateSchema(T::getTableName(), request_j, error);

if (!success)
{
callback(error.code, fmt::format("Failed to validate request body, {}.", error.message));
return;
}

Types::Create_t entity_data = Types::Create_t(request_json.value(), next_id.value());
Types::Create_t entity_data = Types::Create_t(request_j.value(), next_id.value());

T entity(entity_data);

Expand All @@ -70,12 +71,11 @@ void EntityController<T>::Create(CALLBACK_ &&callback, std::string_view data)
template <typename T>
void EntityController<T>::Read(CALLBACK_ &&callback, std::string_view data)
{
jsoncons::json request_json;
try
{
request_json = jsoncons::json::parse(data);
uint64_t id = request_json.at("id").as<uint64_t>();
std::unordered_set<std::string> schema = request_json.at("schema").as<std::unordered_set<std::string>>();
jsoncons::json request_j = jsoncons::json::parse(data);
uint64_t id = request_j.at("id").as<uint64_t>();
std::unordered_set<std::string> schema = request_j.at("schema").as<std::unordered_set<std::string>>();
api::v2::Global::HttpError error;

if (!Validator::validateDatabaseReadSchema(schema, std::format("{}_safe", T::getTableName()), error))
Expand Down Expand Up @@ -125,7 +125,14 @@ void EntityController<T>::Update(CALLBACK_ &&callback, std::string_view data, co
}

Types::Update_t entity_data = Types::Update_t(request_json.value(), id.value());
T entity(entity_data);

T entity(entity_data);

if (!entity.template check_id_exists<Types::Update_t>())
{
callback(HttpStatus::BAD_REQUEST, "ID does not exist");
return;
}

Controller::Update(entity, std::move(callback));
}
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/staffcontroller/staffcontroller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void StaffController<T>::InviteStaffToEntity(CALLBACK_ &&callback, std::string_v

if (response.has_value())
{
callback(200, response.value());
callback(HttpStatus::Code::OK, response.value());
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/entities/base/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ class Types
StaffData() = default;

private:
std::shared_ptr<Configurator> cfg_ = Store::getObject<Configurator>();
const std::shared_ptr<Configurator> cfg_ = Store::getObject<Configurator>();
const Configurator::ServerConfig &servercfg_ = cfg_->get<Configurator::ServerConfig>();
const Configurator::FrontEndConfig &frontendcfg_ = cfg_->get<Configurator::FrontEndConfig>();
};
Expand Down

0 comments on commit 93e0acb

Please sign in to comment.