|
|
|
|
@ -1,9 +1,8 @@
|
|
|
|
|
#include <data.hpp> |
|
|
|
|
#include <fstream> |
|
|
|
|
#include <iostream> |
|
|
|
|
#include <unistd.h> |
|
|
|
|
|
|
|
|
|
fs::path &data::get_data_path() { |
|
|
|
|
const fs::path &with_data_directory::get_data_path() { |
|
|
|
|
static fs::path p(""); |
|
|
|
|
if (p.native().length() != 0) { |
|
|
|
|
return p; |
|
|
|
|
@ -17,34 +16,23 @@ fs::path &data::get_data_path() {
|
|
|
|
|
return p; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
data::data() : json(json::object()) { |
|
|
|
|
with_data_directory::with_data_directory() { |
|
|
|
|
if (!fs::exists(get_data_path())) { |
|
|
|
|
create_data_directory(); |
|
|
|
|
} |
|
|
|
|
const auto data_file = get_store_path(); |
|
|
|
|
if (!fs::exists(data_file)) { |
|
|
|
|
try { |
|
|
|
|
std::ofstream f(data_file); |
|
|
|
|
f << json::object(); |
|
|
|
|
} catch (const std::exception &e) { |
|
|
|
|
std::cerr << "Could not create config file:\n" << e.what() << std::endl; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void data::create_data_directory() { |
|
|
|
|
void with_data_directory::create_data_directory() { |
|
|
|
|
std::error_code ec; |
|
|
|
|
fs::create_directories(get_data_path(), ec); |
|
|
|
|
if (ec) { |
|
|
|
|
std::cerr << "Unable to create configuration directory:\n" << ec.message(); |
|
|
|
|
std::cerr << "Unable to create data directory:\n" << ec.message(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fs::path data::get_store_path() { return get_data_path() / "store.json"; } |
|
|
|
|
// json data::process(const json &input) { return input; }
|
|
|
|
|
|
|
|
|
|
json data::process(const json &input) { return input; } |
|
|
|
|
|
|
|
|
|
json data::validate(const json &input) { |
|
|
|
|
bool data_validator::validate(const json &input) { |
|
|
|
|
const std::string name = input.value("name", ""); |
|
|
|
|
const std::string address = input.value("address", ""); |
|
|
|
|
const std::string phone = input.value("phone", ""); |
|
|
|
|
@ -53,23 +41,13 @@ json data::validate(const json &input) {
|
|
|
|
|
const std::unordered_map<std::string, std::string> strings{ |
|
|
|
|
{"name", name}, {"address", address}, {"phone", phone}, {"email", email}}; |
|
|
|
|
|
|
|
|
|
json errors = json::object({ |
|
|
|
|
{"name", json::array()}, |
|
|
|
|
{"address", json::array()}, |
|
|
|
|
{"phone", json::array()}, |
|
|
|
|
{"email", json::array()}, |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int count = 0; |
|
|
|
|
|
|
|
|
|
const auto add_error = [&](json &field, const std::string &str) { |
|
|
|
|
count++; |
|
|
|
|
field.push_back(str); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
for (const auto &f : strings) { |
|
|
|
|
auto &field = errors[f.first]; |
|
|
|
|
auto &field = err[f.first]; |
|
|
|
|
|
|
|
|
|
if (f.second.size() == 0) { |
|
|
|
|
add_error(field, "is missing from form."); |
|
|
|
|
@ -79,5 +57,7 @@ json data::validate(const json &input) {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return json{{"errors", errors}, {"count", count}}; |
|
|
|
|
return count != 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const json &data_validator::errors() { return err; } |