@@ -9,10 +9,12 @@ namespace cli = command_line_interpreter;
99// --- define commands ------
1010// --------------------------
1111#include " JsonGenerator.hpp"
12+ #include < serial_protocol/DeletedTaskObject.hpp>
1213#include < serial_protocol/ProtocolVersionObject.hpp>
1314#include < serial_protocol/TaskList.hpp>
1415#include < serial_protocol/TaskObject.hpp>
1516#include < string>
17+ #include < tasks/Task.hpp>
1618
1719using namespace task_tracker_systems ;
1820
@@ -24,25 +26,62 @@ static const auto info = []() {
2426static const auto infoCmd = cli::makeCommand(" info" , std::function(info));
2527
2628// command for list
27- static const auto list = []() {
28- const TaskList dummyList = {
29- {.id = 1 , .label = " first" , .duration = 100U },
30- {.id = 2 , .label = " second" , .duration = 200U },
31- };
32- serial_port::cout << toJsonString (dummyList) << std::endl; };
29+ static const auto list = []() { serial_port::cout << toJsonString (device::tasks) << std::endl; };
3330static const auto listCmd = cli::makeCommand(" list" , std::function(list));
3431
3532// command for edit
36- static const auto edit = [](const unsigned int id, const std::basic_string<ProtocolHandler::CharType> label, const std::chrono::seconds::rep duration) {
37- const TaskObject task = {.id = id, .label = label, .duration = duration};
38- serial_port::cout << toJsonString (task) << std::endl;
33+ static const auto edit = [](const TaskId id, const std::basic_string<ProtocolHandler::CharType> label, const Task::Duration::rep duration) {
34+ try
35+ {
36+ auto &task = device::tasks.at (id);
37+ task.setLabel (label);
38+ task.setRecordedDuration (std::chrono::seconds (duration));
39+ const TaskObject taskObject = {.id = id, .label = task.getLabel (), .duration = task.getLastRecordedDuration ().count ()};
40+ serial_port::cout << toJsonString (taskObject) << std::endl;
41+ }
42+ catch (std::out_of_range &e)
43+ {
44+ serial_port::cout << " ERROR: Task not found." << std::endl;
45+ }
3946};
40- static const cli::Option<unsigned int > id = {.labels = {" --id" }, .defaultValue = 0 };
47+ static const cli::Option<TaskId > id = {.labels = {" --id" }, .defaultValue = 0 };
4148static const cli::Option<std::basic_string<ProtocolHandler::CharType>> label = {.labels = {" --name" }, .defaultValue = " foo" };
42- static const cli::Option<std::chrono::seconds ::rep> duration = {.labels = {" --duration" }, .defaultValue = 0 };
49+ static const cli::Option<Task::Duration ::rep> duration = {.labels = {" --duration" }, .defaultValue = 0 };
4350static const auto editCmd = cli::makeCommand(" edit" , std::function(edit), std::make_tuple(&id, &label, &duration));
4451
45- static const std::array<const cli::BaseCommand<char > *, 3 > commands = {&listCmd, &editCmd, &infoCmd};
52+ // command for create/add
53+ static const auto add = [](const TaskId id, const std::basic_string<ProtocolHandler::CharType> label, const Task::Duration::rep duration) {
54+ try
55+ {
56+ const auto &[element, created] = device::tasks.try_emplace (id, label, std::chrono::seconds (duration));
57+ const auto &task = element->second ;
58+ const TaskObject taskObject = {.id = element->first , .label = task.getLabel (), .duration = task.getLastRecordedDuration ().count ()};
59+ serial_port::cout << toJsonString (taskObject) << std::endl;
60+ if (!created)
61+ {
62+ serial_port::cout << " ERROR: Task with the specified ID already exists." << std::endl;
63+ }
64+ }
65+ catch (std::out_of_range &e)
66+ {
67+ serial_port::cout << " ERROR: Task not found." << std::endl;
68+ }
69+ };
70+ static const auto addCmd = cli::makeCommand(" add" , std::function(add), std::make_tuple(&id, &label, &duration));
71+
72+ // command for delete/remove
73+ static const auto del = [](const TaskId id) {
74+ const bool deleted = device::tasks.erase (id) > 0 ;
75+ const DeletedTaskObject taskObject{.id = id};
76+ serial_port::cout << toJsonString (taskObject) << std::endl;
77+ if (!deleted)
78+ {
79+ serial_port::cout << " ERROR: No task deleted." << std::endl;
80+ }
81+ };
82+ static const auto delCmd = cli::makeCommand(" delete" , std::function(del), std::make_tuple(&id));
83+
84+ static const std::array<const cli::BaseCommand<char > *, 5 > commands = {&listCmd, &editCmd, &infoCmd, &addCmd, &delCmd};
4685
4786bool ProtocolHandler::execute (const CharType *const commandLine)
4887{
0 commit comments