-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.h
206 lines (193 loc) · 6.3 KB
/
router.h
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//
// Created by Shahid Yousuf on 28/10/22.
//
#ifndef CPP_TODOS_ROUTER_H
#define CPP_TODOS_ROUTER_H
#include "api.h"
class Router {
public:
void route(Controller controller, CommandWithOptions commandWithOptions) {
auto command = commandWithOptions.command();
switch (command) {
case Command::LIST:
return route_list(controller, commandWithOptions);
case Command::GET:
return route_get(controller, commandWithOptions);
case Command::CREATE:
return route_create(controller, commandWithOptions);
case Command::EDIT:
return route_edit(controller, commandWithOptions);
case Command::DELETE:
return route_delete(controller, commandWithOptions);
case Command::CHECK:
return route_check(controller, commandWithOptions);
case Command::UNCHECK:
return route_uncheck(controller, commandWithOptions);
case Command::HELP:
return route_help(controller, commandWithOptions);
default:
return controller.help();
}
}
private:
void route_list(Controller &controller, CommandWithOptions &commandWithOptions) {
auto options = commandWithOptions.options();
auto option = options.find(Option::STATUS);
if (option == options.end()) {
controller.list();
return;
}
auto value = options[Option::STATUS];
if (value == "completed") {
controller.list_complete();
return;
}
if (value == "pending") {
controller.list_pending();
return;
}
controller.list();
}
void route_get(Controller &controller, CommandWithOptions &commandWithOptions) {
auto options = commandWithOptions.options();
auto option = options.find(Option::INDEX);
if (option == options.end()) {
controller.help();
return;
}
auto value = options[Option::INDEX];
if (!check_valid_index(value)) {
controller.help();
return;
}
int id = std::stoi(value);
if (id <= 0) {
controller.help();
return;
}
controller.get(id);
}
void route_create(Controller &controller, CommandWithOptions &commandWithOptions) {
auto options = commandWithOptions.options();
auto title_option = options.find(Option::TITLE);
if (title_option == options.end()) {
controller.help();
return;
}
std::string title = options[Option::TITLE];
if (!check_valid_title(title)) {
controller.help();
return;
}
controller.create(title);
}
void route_edit(Controller &controller, CommandWithOptions &commandWithOptions) {
auto options = commandWithOptions.options();
auto index_option = options.find(Option::INDEX);
auto title_option = options.find(Option::TITLE);
if (index_option == options.end()) {
controller.help();
return;
}
if (title_option == options.end()) {
controller.help();
return;
}
auto index = options[Option::INDEX];
if (!check_valid_index(index)) {
controller.help();
return;
}
int id = std::stoi(index);
if (id <= 0) {
controller.help();
return;
}
std::string title = options[Option::TITLE];
if (!check_valid_title(title)) {
controller.help();
return;
}
controller.edit(id, title);
}
void route_delete(Controller &controller, CommandWithOptions &commandWithOptions) {
auto options = commandWithOptions.options();
auto option = options.find(Option::INDEX);
if (option == options.end()) {
controller.help();
return;
}
auto index = options[Option::INDEX];
if (!check_valid_index(index)) {
controller.help();
return;
}
int id = std::stoi(index);
if (id <= 0) {
controller.help();
return;
}
controller.remove(id);
}
void route_check(Controller &controller, CommandWithOptions &commandWithOptions) {
auto options = commandWithOptions.options();
auto option = options.find(Option::INDEX);
if (option == options.end()) {
controller.help();
return;
}
auto index = options[Option::INDEX];
if (!check_valid_index(index)) {
controller.help();
return;
}
int id = std::stoi(index);
if (id <= 0) {
controller.help();
return;
}
controller.check(id);
}
void route_uncheck(Controller &controller, CommandWithOptions &commandWithOptions) {
auto options = commandWithOptions.options();
auto option = options.find(Option::INDEX);
if (option == options.end()) {
controller.help();
return;
}
auto index = options[Option::INDEX];
if (!check_valid_index(index)) {
controller.help();
return;
}
int id = std::stoi(index);
if (id <= 0) {
controller.help();
return;
}
controller.uncheck(id);
}
void route_help(Controller &controller, CommandWithOptions &commandWithOptions) {
auto options = commandWithOptions.options();
auto option = options.find(Option::COMMAND);
if (option == options.end()) {
controller.help();
return;
}
auto value = options[Option::COMMAND];
auto command = getBaseCommand(value);
controller.help(command);
}
bool check_valid_index(std::string &index) {
std::string::const_iterator it = index.cbegin();
while (it != index.cend() && std::isdigit(*it)) ++it;
return !index.empty() && it == index.cend();
}
bool check_valid_title(std::string &title) {
if (title.empty() || title.length() < 1 || title.length() > 80) {
return false;
}
return true;
}
};
#endif //CPP_TODOS_ROUTER_H