Browse Source

refactor data directory and validator a bit

master
Jørgen Sverre Lien Sellæg 5 years ago
parent
commit
9f4b82eb7d
  1. 23
      include/data.hpp
  2. 3
      include/store.hpp
  3. 40
      src/data.cpp
  4. 22
      src/store.cpp

23
include/data.hpp

@ -4,13 +4,24 @@
namespace fs = std::experimental::filesystem;
class data : public json {
fs::path &get_data_path();
class with_data_directory {
void create_data_directory();
fs::path get_store_path();
public:
data();
json process(const json &);
json validate(const json &);
with_data_directory();
static const fs::path &get_data_path();
};
class data_validator {
json err = json::object({
{"name", json::array()},
{"address", json::array()},
{"phone", json::array()},
{"email", json::array()},
});
int count = 0;
public:
bool validate(const json &);
const json &errors();
};

3
include/store.hpp

@ -4,8 +4,9 @@
#include <web_server.hpp>
class Application {
with_data_directory data_directory;
HttpServer http_server;
data dat;
static void web_server_started(std::size_t port);
static void secure_web_server_started(std::size_t port);

40
src/data.cpp

@ -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; }

22
src/store.cpp

@ -46,11 +46,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, dat.dump(),
// {header_access_control, header_application_data});
// };
http_server.resource["^/orders/?$"]["PUT"] =
[&](std::shared_ptr<HttpServer::Response> response,
@ -78,15 +78,15 @@ int Application::run() {
return response::not_found(response);
}
const auto errors = dat.validate(data);
int error_count = errors["count"];
if (error_count != 0) {
return response::bad_request(response, errors["errors"]);
data_validator v;
if (v.validate(data)) {
return response::bad_request(response, v.errors());
}
const auto json_response = dat.process(data);
// const auto json_response = dat.process(data);
response->write(Status::success_created, json_response.dump(),
response->write(Status::success_created, data.dump(),
{header_access_control, header_application_data});
};

Loading…
Cancel
Save