You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
#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())) {
|
|
|
|
|
load();
|
|
|
|
|
} else {
|
|
|
|
|
write();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void orders::write() {
|
|
|
|
|
try {
|
|
|
|
|
std::ofstream of(get_path());
|
|
|
|
|
of << *this;
|
|
|
|
|
|
|
|
|
|
} catch (const std::exception &e) {
|
|
|
|
|
std::cerr << e.what() << std::endl;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
orders::~orders() { write(); }
|
|
|
|
|
|
|
|
|
|
void orders::push(json data) {
|
|
|
|
|
data["id"] = std::to_string(size() + 1);
|
|
|
|
|
push_back(data);
|
|
|
|
|
}
|