Jørgen Sverre Lien Sellæg 5 years ago
parent
commit
f533b121f8
  1. 4
      CMakeLists.txt
  2. 15
      include/orders.hpp
  3. 87
      src/orders.cpp
  4. 14
      src/store.cpp

4
CMakeLists.txt

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 2.8)
cmake_minimum_required(VERSION 2.9)
project(store)
@ -10,7 +10,7 @@ else()
message(STATUS "ccache was not found.")
endif(CCACHE_FOUND)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wunused-parameter -g")
set(BUILD_TESTING OFF CACHE INTERNAL "")

15
include/orders.hpp

@ -4,9 +4,22 @@
#include <future>
#include <json.hpp>
class orders : public json {
struct order {
std::string name;
std::string address;
std::string phone;
std::string email;
int id;
};
inline void to_json(json &, const order &order);
inline void from_json(const json &, order &order);
class orders {
std::vector<order> data;
const with_data_directory &data_directory;
const fs::path &get_path();
void sort();
public:
void write();

87
src/orders.cpp

@ -2,15 +2,60 @@
#include <iostream>
#include <orders.hpp>
void from_json(const json &j, order &order) {
j.at("name").get_to(order.name);
j.at("address").get_to(order.address);
j.at("phone").get_to(order.phone);
j.at("email").get_to(order.email);
j.at("id").get_to(order.id);
}
void to_json(nlohmann::json &j, const order &order) {
j = json{{"name", order.name},
{"address", order.address},
{"phone", order.phone},
{"email", order.email},
{"id", order.id}};
}
void orders::load() {
try {
json j;
std::fstream f(get_path());
f >> *this;
f >> j;
j.get_to(data);
} catch (const std::exception &e) {
std::cerr << e.what() << std::endl;
}
}
void orders::sort() {
// std::cout << "sorting..." << std::endl;
// std::vector<json> vec;
// vec.reserve(data.size());
// for (auto &el : *this) {
// vec.push_back(el);
// }
// std::stable_sort(vec.begin(), vec.end(), [](const auto &a, const auto &b) {
// int aid = a["id"];
// int bid = b["id"];
// if (aid > bid) {
// return 1;
// }
// if (aid < bid) {
// return -1;
// }
// return 0;
// });
// clear();
// for (const auto &json : vec) {
// push_back(json);
// }
}
const fs::path &orders::get_path() {
static fs::path orders_json;
if (orders_json.empty())
@ -18,8 +63,7 @@ const fs::path &orders::get_path() {
return orders_json;
}
orders::orders(const with_data_directory &dir)
: json(json::array()), data_directory(dir) {
orders::orders(const with_data_directory &dir) : data_directory(dir) {
if (fs::exists(get_path())) {
load();
} else {
@ -30,7 +74,8 @@ orders::orders(const with_data_directory &dir)
void orders::write() {
try {
std::ofstream of(get_path());
of << *this;
json j{data};
of << j;
} catch (const std::exception &e) {
std::cerr << e.what() << std::endl;
@ -39,7 +84,37 @@ void orders::write() {
orders::~orders() { write(); }
auto max_element(
json::iterator begin, json::iterator end,
std::function<bool(const json::iterator &, const json::iterator &)> cmp) {
auto largest = begin;
if (begin == end)
return end;
for (; begin != end; begin++)
if (cmp(largest, begin))
largest = begin;
return largest;
}
void orders::push(json data) {
data["id"] = std::to_string(size() + 1);
push_back(data);
// int id = 0;
// if (size() > 0) {
// const auto res =
// max_element(begin(), end(), [](const json &a, const json &b) {
// std::cout << "max_element " << a["id"] << b["id"] << std::endl;
// std::cout << a["id"] << std::endl;
// std::cout << b["id"] << std::endl;
// return a["id"] < b["id"];
// });
// id = (*res)["id"];
// }
// std::cout << "--\n" << id << std::endl;
// data["id"] = id + 1;
// std::cout << data["id"] << std::endl;
order o;
data["id"] = 0;
data.get_to(o);
std::cout << data << std::endl;
data.push_back(o);
}

14
src/store.cpp

@ -50,9 +50,16 @@ int Application::run() {
http_server.resource["^/orders/?$"]["GET"] =
[&](std::shared_ptr<HttpServer::Response> response, ...) {
response->write(Status::success_ok, orders.dump(),
response->write(Status::success_ok, json({}).dump(),
{header_access_control, header_application_data});
};
http_server.resource["^/stop/?$"]["PUT"] =
[&](std::shared_ptr<HttpServer::Response> response, ...) {
{ response->write("stopping"); }
http_server.stop();
if (https_server)
https_server->stop();
};
http_server.resource["^/orders/?$"]["PUT"] =
[&](std::shared_ptr<HttpServer::Response> response,
@ -93,6 +100,11 @@ int Application::run() {
{header_access_control, header_application_data});
};
http_server.on_error = [](std::shared_ptr<HttpServer::Request> request,
const SimpleWeb::error_code &ec) {
std::cout << ec.message() << ", " << ec.category().name() << std::endl;
};
servers.emplace_back([&]() { http_server.start(web_server_started); });
for (auto &server : servers) {

Loading…
Cancel
Save