diff --git a/include/orders.hpp b/include/orders.hpp new file mode 100644 index 0000000..e5acdcf --- /dev/null +++ b/include/orders.hpp @@ -0,0 +1,16 @@ +#pragma once +#include +#include +#include +#include + +class orders : public json { + const with_data_directory &data_directory; + const fs::path &get_path(); + + +public: + void write(const json &); + void load(); + orders(const with_data_directory &); +}; diff --git a/src/orders.cpp b/src/orders.cpp new file mode 100644 index 0000000..e437bda --- /dev/null +++ b/src/orders.cpp @@ -0,0 +1,36 @@ +#include +#include +#include + +void orders::load() { + try { + std::fstream f(get_path()); + f >> *this; + } catch (const std::exception &e) { + std::cerr << e.what() << std::endl; + } +} + +const fs::path &orders::get_path() { + static fs::path orders_json; + if (orders_json.empty()) + orders_json = data_directory.get_path() / "orders.json"; + return orders_json; +} + +orders::orders(const with_data_directory &dir) + : json(json::array()), data_directory(dir) { + if (!fs::exists(get_path())) { + write(*this); + } +} + +void orders::write(const json &input) { + try { + std::ofstream of(get_path()); + of << input; + + } catch (const std::exception &e) { + std::cerr << e.what() << std::endl; + } +} \ No newline at end of file diff --git a/src/store.cpp b/src/store.cpp index 5907bc1..266b5bb 100644 --- a/src/store.cpp +++ b/src/store.cpp @@ -1,5 +1,6 @@ #include #include +#include #include void Application::web_server_started(std::size_t port) { @@ -27,6 +28,8 @@ Application::Application(const nlohmann::json &cfg) { int Application::run() { std::vector servers; + orders orders(data_directory); + orders.load(); for (const auto &m : {"GET", "POST", "PUT", "PATCH", "OPTIONS", "HEAD"}) { if (https_server) { @@ -46,11 +49,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, orders.dump(), + {header_access_control, header_application_data}); + }; http_server.resource["^/orders/?$"]["PUT"] = [&](std::shared_ptr response,