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.

45 lines
871 B

5 years ago
#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();
5 years ago
}
}
void orders::write() {
5 years ago
try {
std::ofstream of(get_path());
of << *this;
5 years ago
} catch (const std::exception &e) {
std::cerr << e.what() << std::endl;
}
5 years ago
}
orders::~orders() { write(); }
void orders::push(json data) {
data["id"] = std::to_string(size() + 1);
push_back(data);
}