Browse Source

add orders data

master
Jørgen Sverre Lien Sellæg 5 years ago
parent
commit
3d30dfc15b
  1. 16
      include/orders.hpp
  2. 36
      src/orders.cpp
  3. 13
      src/store.cpp

16
include/orders.hpp

@ -0,0 +1,16 @@
#pragma once
#include <data.hpp>
#include <fs.hpp>
#include <future>
#include <json.hpp>
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 &);
};

36
src/orders.cpp

@ -0,0 +1,36 @@
#include <fstream>
#include <iostream>
#include <orders.hpp>
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;
}
}

13
src/store.cpp

@ -1,5 +1,6 @@
#include <http.hpp>
#include <json_converters.hpp>
#include <orders.hpp>
#include <store.hpp>
void Application::web_server_started(std::size_t port) {
@ -27,6 +28,8 @@ Application::Application(const nlohmann::json &cfg) {
int Application::run() {
std::vector<std::thread> 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<HttpServer::Response> response, ...) {
// response->write(Status::success_ok, dat.dump(),
// {header_access_control, header_application_data});
// };
http_server.resource["^/orders/?$"]["GET"] =
[&](std::shared_ptr<HttpServer::Response> response, ...) {
response->write(Status::success_ok, orders.dump(),
{header_access_control, header_application_data});
};
http_server.resource["^/orders/?$"]["PUT"] =
[&](std::shared_ptr<HttpServer::Response> response,

Loading…
Cancel
Save