Permalink
Browse files

#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)
  • Loading branch information...
lauft committed Jul 21, 2018
1 parent 86258cd commit 29e305033bfef440dd284fad1a6f163fd76e9c8f
Showing with 82 additions and 2 deletions.
  1. +11 −2 src/Database.cpp
  2. +26 −0 src/Interval.cpp
  3. +2 −0 src/Interval.h
  4. +17 −0 src/UndoAction.cpp
  5. +4 −0 src/UndoAction.h
  6. +22 −0 src/commands/CmdUndo.cpp
@@ -165,8 +165,17 @@ void Database::deleteInterval (const Interval& interval)
void Database::modifyInterval (const Interval& from, const Interval& to)
{
startTransaction ();
deleteInterval (from);
addInterval (to);

if (!from.empty ())
{
deleteInterval (from);
}

if (!to.empty ())
{
addInterval (to);
}

endTransaction ();
}

@@ -30,6 +30,7 @@
#include <format.h>
#include <Lexer.h>
#include <sstream>
#include <JSON.h>

////////////////////////////////////////////////////////////////////////////////
// Syntax:
@@ -205,4 +206,29 @@ std::string Interval::dump () const
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;
}

////////////////////////////////////////////////////////////////////////////////
@@ -47,6 +47,8 @@ class Interval
std::string json () const;
std::string dump () const;

static Interval fromJson (std::string json);

public:
Range range {};
int id {0};
@@ -42,3 +42,20 @@ std::string UndoAction::toString ()
" before: " + _before + "\n" +
" after: " + _after + "\n";
}

std::string UndoAction::getType ()
{
return _type;
}

std::string UndoAction::getBefore ()
{
return _before;
}

std::string UndoAction::getAfter ()
{
return _after;
}


@@ -34,6 +34,10 @@ class UndoAction
public:
UndoAction(const std::string&, const std::string&, const std::string&);

std::string getType();
std::string getBefore();
std::string getAfter();

std::string toString ();

private:
@@ -29,6 +29,14 @@
#include <iostream>
#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)
{
@@ -49,7 +57,21 @@ int CmdUndo (Rules& rules, Database& database)
for (auto& action : actions)
{
// Select database...
std::string type = action.getType ();

// 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"))

0 comments on commit 29e3050

Please sign in to comment.