Add Command: proj add
This commit is contained in:
parent
7c447f69d1
commit
b0461a4748
17
src/data.cpp
17
src/data.cpp
|
@ -27,17 +27,26 @@ void from_json(const json& j, Project& proj) {
|
|||
j.at("isDone").get_to(proj.isDone);
|
||||
}
|
||||
void to_json(json& j, const Skid& skid) {
|
||||
std::vector<Project> projects;
|
||||
if (skid.projects != nullptr) {
|
||||
projects = *skid.projects;
|
||||
}
|
||||
j = json{
|
||||
{"projects", *skid.projects},
|
||||
{"projects", projects},
|
||||
{"creationDate", skid.creationDate},
|
||||
};
|
||||
}
|
||||
void from_json(const json& j, Skid& skid) {
|
||||
skid.projects = new std::vector<Project>();
|
||||
j.at("projects").get_to(*skid.projects);
|
||||
j.at("creationDate").get_to(skid.creationDate);
|
||||
}
|
||||
void to_json(json& j, const DB& db) {
|
||||
j = json{{"projects", *db.projects},
|
||||
std::map<std::string, Project> projects;
|
||||
if (db.projects != nullptr) {
|
||||
projects = *db.projects;
|
||||
};
|
||||
j = json{{"projects", projects},
|
||||
{"creationDate", db.creationDate},
|
||||
{"lastAccessTime", db.lastAccessTime}};
|
||||
}
|
||||
|
@ -50,9 +59,11 @@ void from_json(const json& j, DB& db) {
|
|||
|
||||
void writeDB(DB db, const std::string& dest) {
|
||||
try {
|
||||
std::cout << "Writing database..." << std::endl;
|
||||
std::ofstream f(dest);
|
||||
json j = db;
|
||||
f << j;
|
||||
std::cout << "Wrote database successfully!" << std::endl;
|
||||
} catch (std::exception& ex) {
|
||||
std::cout << "Failed writing database to: " << dest << std::endl;
|
||||
std::cout << "Error: " << ex.what() << std::endl;
|
||||
|
@ -64,6 +75,7 @@ bool is_empty(std::ifstream& pFile) {
|
|||
|
||||
void loadDB(DB& db, const std::string& src) {
|
||||
try {
|
||||
std::cout << "Loading database..." << std::endl;
|
||||
std::ifstream f(src);
|
||||
json j;
|
||||
if (is_empty(f)) {
|
||||
|
@ -73,6 +85,7 @@ void loadDB(DB& db, const std::string& src) {
|
|||
}
|
||||
f >> j;
|
||||
db = j;
|
||||
std::cout << "Loaded database successfully!" << std::endl;
|
||||
} catch (const std::exception& ex) {
|
||||
std::cout << "Failed loading database from: " << src << std::endl;
|
||||
std::cout << "Error: " << ex.what() << std::endl;
|
||||
|
|
|
@ -2,9 +2,43 @@
|
|||
|
||||
#include <string.h>
|
||||
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <boost/uuid/uuid.hpp> // uuid class
|
||||
#include <boost/uuid/uuid_generators.hpp> // generators
|
||||
#include <boost/uuid/uuid_io.hpp> // streaming operators etc.
|
||||
|
||||
#include "../data.h"
|
||||
|
||||
void proj_add(proj_args args) {}
|
||||
void proj_add(proj_args args) {
|
||||
Project proj;
|
||||
proj.name = args.action_param;
|
||||
|
||||
/* Get Project description */
|
||||
std::cout << "Project description: ";
|
||||
std::cin >> proj.desc;
|
||||
|
||||
/* Generate Project UUID */
|
||||
boost::uuids::uuid uuid = boost::uuids::random_generator()();
|
||||
proj.uuid = boost::lexical_cast<std::string>(uuid);
|
||||
|
||||
/* Get Priority of project */
|
||||
std::cout << "Project Priority: ";
|
||||
std::string temp;
|
||||
std::cin >> temp;
|
||||
proj.pri = stoi(temp);
|
||||
|
||||
/* Creation date */
|
||||
time_t t = time(0);
|
||||
struct tm* currentTime = localtime(&t);
|
||||
|
||||
proj.creationDate.day = currentTime->tm_mday;
|
||||
proj.creationDate.month = currentTime->tm_mon;
|
||||
proj.creationDate.year = currentTime->tm_year;
|
||||
|
||||
(*args.db->projects)[proj.uuid] = proj;
|
||||
writeDB(*args.db, ".db");
|
||||
delete args.db;
|
||||
}
|
||||
|
||||
void proj_rm(proj_args args) {}
|
||||
|
||||
|
@ -49,16 +83,13 @@ void proj_dispatch(const std::vector<std::string>& arguments) {
|
|||
type_action.pop_back();
|
||||
args.action_param = type_action;
|
||||
|
||||
args.db = new DB();
|
||||
loadDB(*args.db, ".db");
|
||||
|
||||
if (type_str == "add")
|
||||
proj_add(args);
|
||||
else if (type_str == "rm")
|
||||
proj_rm(args);
|
||||
else if (type_str == "list")
|
||||
proj_list(args);
|
||||
|
||||
args.db = new DB();
|
||||
loadDB(*args.db, ".db");
|
||||
|
||||
std::cout << "Action: " << type_str << std::endl
|
||||
<< "Args: " << type_action << std::endl;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue