-
Notifications
You must be signed in to change notification settings - Fork 0
/
TaskController.cpp
182 lines (156 loc) · 4.28 KB
/
TaskController.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* TaskController.cpp
*
* Created on: 15 Feb 2012
* Author: Max Foster
*/
#include "TaskController.h"
#include "Databases.h"
#include "Task.h"
#include "dialogs/task/TaskShow.h"
#include "dialogs/task/TaskForm.h"
void TaskController::Index(QWidget *) {}
void TaskController::Show(const int taskId, QWidget *caller)
{
Task task;
try { Databases::tasks().findRecord("id", taskId); }
catch (const std::exception &e)
{
showErrorDialog(e.what());
return;
}
if (task.null())
{
showErrorDialog(("Task with ID " + toString(taskId) + " could not be found").c_str());
return;
}
Show(task, caller);
}
void TaskController::Show(Task &task, QWidget *caller)
{
TaskShow view(task, caller);
view.setModal(true);
view.exec();
}
Task TaskController::New(QWidget *caller)
{
Task task;
TaskForm view(task, caller);
view.setModal(true);
return (view.exec() == QDialog::Rejected ? Task() : task);
}
void TaskController::Edit(const int taskId, QWidget *caller)
{
Task task;
try { Databases::tasks().findRecord("id", taskId); }
catch (const std::exception &e)
{
showErrorDialog(e.what());
return;
}
if (task.null())
{
showErrorDialog(("Task with ID " + toString(taskId) + " could not be found").c_str());
return;
}
Edit(task, caller);
}
void TaskController::Edit(Task &task, QWidget *caller)
{
Task tempTask = task;
TaskForm view(tempTask, caller);
view.setModal(true);
if (view.exec() == TaskForm::Accepted) task = tempTask;
}
bool TaskController::Create(Task &taskAttributes, QWidget *)
{
try { Databases::tasks().addRecord(taskAttributes); }
catch (const std::exception &e)
{
showErrorDialog(e.what());
return false;
}
if (taskAttributes.null())
{
showErrorDialog("There was an error with adding the task to the database");
return false;
}
return true;
}
bool TaskController::Update(const Task &task, QWidget *)
{
bool success = false;
try { success = Databases::tasks().updateRecord(task); }
catch (const std::exception &e)
{
showErrorDialog(e.what());
return false;
}
if (!success) showErrorDialog("There was an error with updating the task in the database");
return success;
}
bool TaskController::Destroy(const int taskId, QWidget *)
{
bool success = false;
try { success = Databases::tasks().deleteRecord(taskId); }
catch (const std::exception &e)
{
showErrorDialog(e.what());
return false;
}
if (!success) showErrorDialog("There was an error with removing the task from the database");
return success;
}
bool TaskController::Destroy(Task &task, QWidget *caller)
{
if (Destroy(task.getId(), caller))
{
task = Task();
return true;
}
return false;
}
Task TaskController::getTask(const int taskId)
{
Task task;
try { task = Databases::tasks().findRecord("id", taskId); }
catch (const std::exception &e)
{
showErrorDialog(e.what());
return Task();
}
return task;
}
Database<Task>::recordListPtr TaskController::getAllTasks()
{
Database<Task>::recordListPtr tasks;
try { tasks = Databases::tasks().allRecords(); }
catch (const std::exception &e)
{
showErrorDialog(e.what());
return Database<Task>::recordListPtr(new Database<Task>::recordList);
}
return tasks;
}
void TaskController::sortTasksByDate(Database<Task>::recordList &tasks, bool ascending)
{
struct NestedFunctions
{
static int dateCompareAsc(const Task &task1, const Task &task2)
{
const time_t date1 = task1.getDate(), date2 = task2.getDate();
if (date1 < date2) return -1;
if (date1 > date2) return 1;
return 0;
}
static int dateCompareDec(const Task &task1, const Task &task2)
{
const time_t date1 = task1.getDate(), date2 = task2.getDate();
if (date1 < date2) return 1;
if (date1 > date2) return -1;
return 0;
}
};
Databases::tasks().sortRecords(tasks, 0, tasks.size() - 1, ascending ?
NestedFunctions::dateCompareAsc : NestedFunctions::dateCompareDec);
}