#9 TI-1: Make undo work with interval actions

- Add creator method to create interval from JSON string
- Add getters for UndoAction fields
- Make Database::modifyInterval work with empty intervals (i.e. ignore them at the respective command)
This commit is contained in:
Thomas Lauf 2018-07-21 22:35:17 +02:00
parent 86258cdb93
commit 29e305033b
6 changed files with 82 additions and 2 deletions

View file

@ -165,8 +165,17 @@ void Database::deleteInterval (const Interval& interval)
void Database::modifyInterval (const Interval& from, const Interval& to) void Database::modifyInterval (const Interval& from, const Interval& to)
{ {
startTransaction (); startTransaction ();
deleteInterval (from);
addInterval (to); if (!from.empty ())
{
deleteInterval (from);
}
if (!to.empty ())
{
addInterval (to);
}
endTransaction (); endTransaction ();
} }

View file

@ -30,6 +30,7 @@
#include <format.h> #include <format.h>
#include <Lexer.h> #include <Lexer.h>
#include <sstream> #include <sstream>
#include <JSON.h>
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Syntax: // Syntax:
@ -205,4 +206,29 @@ std::string Interval::dump () const
return out.str (); return out.str ();
} }
Interval Interval::fromJson (std::string jsonString)
{
Interval interval = Interval ();
if (!jsonString.empty ())
{
auto* json = (json::object*) json::parse (jsonString);
json::array* tags = (json::array*) json->_data["tags"];
for (auto& tag : tags->_data)
{
auto* value = (json::string*) tag;
interval.tag(value->_data);
}
json::string* start = (json::string*) json->_data["start"];
interval.range.start = Datetime(start->_data, "YmdTHnsZ");
json::string* end = (json::string*) json->_data["end"];
interval.range.end = Datetime(end->_data, "YmdTHnsZ");
}
return interval;
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////

View file

@ -47,6 +47,8 @@ public:
std::string json () const; std::string json () const;
std::string dump () const; std::string dump () const;
static Interval fromJson (std::string json);
public: public:
Range range {}; Range range {};
int id {0}; int id {0};

View file

@ -42,3 +42,20 @@ std::string UndoAction::toString ()
" before: " + _before + "\n" + " before: " + _before + "\n" +
" after: " + _after + "\n"; " after: " + _after + "\n";
} }
std::string UndoAction::getType ()
{
return _type;
}
std::string UndoAction::getBefore ()
{
return _before;
}
std::string UndoAction::getAfter ()
{
return _after;
}

View file

@ -34,6 +34,10 @@ class UndoAction
public: public:
UndoAction(const std::string&, const std::string&, const std::string&); UndoAction(const std::string&, const std::string&, const std::string&);
std::string getType();
std::string getBefore();
std::string getAfter();
std::string toString (); std::string toString ();
private: private:

View file

@ -29,6 +29,14 @@
#include <iostream> #include <iostream>
#include <format.h> #include <format.h>
static void undoIntervalAction(UndoAction& action, Database& database)
{
Interval before = Interval::fromJson (action.getBefore ());
Interval after = Interval::fromJson (action.getAfter ());
database.modifyInterval (after, before);
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
int CmdUndo (Rules& rules, Database& database) int CmdUndo (Rules& rules, Database& database)
{ {
@ -49,7 +57,21 @@ int CmdUndo (Rules& rules, Database& database)
for (auto& action : actions) for (auto& action : actions)
{ {
// Select database... // Select database...
std::string type = action.getType ();
// Rollback action... // Rollback action...
if (type == "interval")
{
undoIntervalAction (action, database);
}
else if (type == "config")
{
throw "Undo of config actions not yet implemented!";
}
else
{
throw format ("Unknown undo action type '{}'", type);
}
} }
if (rules.getBoolean ("verbose")) if (rules.getBoolean ("verbose"))