Skip to content

Commit abc4d9a

Browse files
committed
Add command for creating tasks.
1 parent b14a148 commit abc4d9a

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

lib/application_business_rules/serial_interface/Protocol.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ static const auto edit = [](const TaskId id, const std::basic_string<ProtocolHan
3737
auto &task = device::tasks.at(id);
3838
task.setLabel(label);
3939
task.setRecordedDuration(std::chrono::seconds(duration));
40-
const TaskObject taskObject = {.id = id, .label = task.getLabel(), .duration = task.getRecordedDuration().count()};
40+
const TaskObject taskObject = {.id = id, .label = task.getLabel(), .duration = task.getLastRecordedDuration().count()};
4141
serial_port::cout << toJsonString(taskObject) << std::endl;
4242
}
4343
catch (std::out_of_range &e)
@@ -50,7 +50,27 @@ static const cli::Option<std::basic_string<ProtocolHandler::CharType>> label = {
5050
static const cli::Option<Task::Duration::rep> duration = {.labels = {"--duration"}, .defaultValue = 0};
5151
static const auto editCmd = cli::makeCommand("edit", std::function(edit), std::make_tuple(&id, &label, &duration));
5252

53-
static const std::array<const cli::BaseCommand<char> *, 3> commands = {&listCmd, &editCmd, &infoCmd};
53+
// command for create/add
54+
static const auto add = [](const TaskId id, const std::basic_string<ProtocolHandler::CharType> label, const Task::Duration::rep duration) {
55+
try
56+
{
57+
const auto &[element, created] = device::tasks.try_emplace(id, label, std::chrono::seconds(duration));
58+
const auto &task = element->second;
59+
const TaskObject taskObject = {.id = element->first, .label = task.getLabel(), .duration = task.getLastRecordedDuration().count()};
60+
serial_port::cout << toJsonString(taskObject) << std::endl;
61+
if (!created)
62+
{
63+
serial_port::cout << "ERROR: Task with the specified ID already exists." << std::endl;
64+
}
65+
}
66+
catch (std::out_of_range &e)
67+
{
68+
serial_port::cout << "ERROR: Task not found." << std::endl;
69+
}
70+
};
71+
static const auto addCmd = cli::makeCommand("add", std::function(add), std::make_tuple(&id, &label, &duration));
72+
73+
static const std::array<const cli::BaseCommand<char> *, 4> commands = {&listCmd, &editCmd, &infoCmd, &addCmd};
5474

5575
bool ProtocolHandler::execute(const CharType *const commandLine)
5676
{

0 commit comments

Comments
 (0)