From 9f4b82eb7dbf24cc45e2b7ecc54feb4645f6df51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Sverre=20Lien=20Sell=C3=A6g?= Date: Sun, 23 May 2021 17:48:31 +0200 Subject: [PATCH] refactor data directory and validator a bit --- include/data.hpp | 23 +++++++++++++++++------ include/store.hpp | 3 ++- src/data.cpp | 40 ++++++++++------------------------------ src/store.cpp | 22 +++++++++++----------- 4 files changed, 40 insertions(+), 48 deletions(-) diff --git a/include/data.hpp b/include/data.hpp index e0bec4c..070e5eb 100644 --- a/include/data.hpp +++ b/include/data.hpp @@ -4,13 +4,24 @@ namespace fs = std::experimental::filesystem; -class data : public json { - fs::path &get_data_path(); +class with_data_directory { void create_data_directory(); - fs::path get_store_path(); public: - data(); - json process(const json &); - json validate(const json &); + with_data_directory(); + static const fs::path &get_data_path(); +}; + +class data_validator { + json err = json::object({ + {"name", json::array()}, + {"address", json::array()}, + {"phone", json::array()}, + {"email", json::array()}, + }); + int count = 0; + +public: + bool validate(const json &); + const json &errors(); }; diff --git a/include/store.hpp b/include/store.hpp index 02c8161..72223b9 100644 --- a/include/store.hpp +++ b/include/store.hpp @@ -4,8 +4,9 @@ #include class Application { + with_data_directory data_directory; + HttpServer http_server; - data dat; static void web_server_started(std::size_t port); static void secure_web_server_started(std::size_t port); diff --git a/src/data.cpp b/src/data.cpp index 31c1aec..e34d723 100644 --- a/src/data.cpp +++ b/src/data.cpp @@ -1,9 +1,8 @@ #include -#include #include #include -fs::path &data::get_data_path() { +const fs::path &with_data_directory::get_data_path() { static fs::path p(""); if (p.native().length() != 0) { return p; @@ -17,34 +16,23 @@ fs::path &data::get_data_path() { return p; } -data::data() : json(json::object()) { +with_data_directory::with_data_directory() { if (!fs::exists(get_data_path())) { create_data_directory(); } - const auto data_file = get_store_path(); - if (!fs::exists(data_file)) { - try { - std::ofstream f(data_file); - f << json::object(); - } catch (const std::exception &e) { - std::cerr << "Could not create config file:\n" << e.what() << std::endl; - } - } } -void data::create_data_directory() { +void with_data_directory::create_data_directory() { std::error_code ec; fs::create_directories(get_data_path(), ec); if (ec) { - std::cerr << "Unable to create configuration directory:\n" << ec.message(); + std::cerr << "Unable to create data directory:\n" << ec.message(); } } -fs::path data::get_store_path() { return get_data_path() / "store.json"; } +// json data::process(const json &input) { return input; } -json data::process(const json &input) { return input; } - -json data::validate(const json &input) { +bool data_validator::validate(const json &input) { const std::string name = input.value("name", ""); const std::string address = input.value("address", ""); const std::string phone = input.value("phone", ""); @@ -53,23 +41,13 @@ json data::validate(const json &input) { const std::unordered_map strings{ {"name", name}, {"address", address}, {"phone", phone}, {"email", email}}; - json errors = json::object({ - {"name", json::array()}, - {"address", json::array()}, - {"phone", json::array()}, - {"email", json::array()}, - }); - - - int count = 0; - const auto add_error = [&](json &field, const std::string &str) { count++; field.push_back(str); }; for (const auto &f : strings) { - auto &field = errors[f.first]; + auto &field = err[f.first]; if (f.second.size() == 0) { add_error(field, "is missing from form."); @@ -79,5 +57,7 @@ json data::validate(const json &input) { } } - return json{{"errors", errors}, {"count", count}}; + return count != 0; } + +const json &data_validator::errors() { return err; } \ No newline at end of file diff --git a/src/store.cpp b/src/store.cpp index c9d9aeb..5907bc1 100644 --- a/src/store.cpp +++ b/src/store.cpp @@ -46,11 +46,11 @@ int Application::run() { [&]() { https_server->start(secure_web_server_started); }); } - http_server.resource["^/orders/?$"]["GET"] = - [&](std::shared_ptr response, ...) { - response->write(Status::success_ok, dat.dump(), - {header_access_control, header_application_data}); - }; + // http_server.resource["^/orders/?$"]["GET"] = + // [&](std::shared_ptr response, ...) { + // response->write(Status::success_ok, dat.dump(), + // {header_access_control, header_application_data}); + // }; http_server.resource["^/orders/?$"]["PUT"] = [&](std::shared_ptr response, @@ -78,15 +78,15 @@ int Application::run() { return response::not_found(response); } - const auto errors = dat.validate(data); - int error_count = errors["count"]; - if (error_count != 0) { - return response::bad_request(response, errors["errors"]); + data_validator v; + + if (v.validate(data)) { + return response::bad_request(response, v.errors()); } - const auto json_response = dat.process(data); + // const auto json_response = dat.process(data); - response->write(Status::success_created, json_response.dump(), + response->write(Status::success_created, data.dump(), {header_access_control, header_application_data}); };