Timesheets-Server
Timesheets server
Načítám...
Vyhledávám...
Nebylo nic nalezeno
controllertimesheet.cpp
Zobrazit dokumentaci tohoto souboru.
1
8#include "httprequest.h"
9#include "db.h"
10#include "pdebug.h"
11
12using namespace Httpd;
13
14
15ControllerTimesheet::ControllerTimesheet(HobrasoftHttpd::HttpConnection *parent) : AbstractController(parent) {
16}
17
18
19void ControllerTimesheet::serviceStart(HobrasoftHttpd::HttpRequest *request, HobrasoftHttpd::HttpResponse *response, int id) {
20 PDEBUG;
21 QList<Dbt::TicketTimesheets> list1 = db()->runningTimesheets(id);
22 if (!list1.isEmpty()) {
23 QVariantMap item = list1[0].toMap();
24 item.remove("created");
25 item.remove("modified");
26 item["ok"] = false;
27 item["error"] = "conflict";
28 item["reason"] = "Timesheet is already started";
29 serviceError(request, response, 409, "conflict", item);
30 return;
31 }
32
33 QList<Dbt::TicketTimesheets> list2 = db()->startTimesheet(id);
34 if (list2.isEmpty()) {
35 serviceError(request, response, 400, "error", "Timesheet could not be started");
36 return;
37 }
38
39 QVariantMap item = list2[0].toMap();
40 item.remove("created");
41 item.remove("modified");
42 serviceOK(request, response, item);
43}
44
45
46void ControllerTimesheet::serviceStop(HobrasoftHttpd::HttpRequest *request, HobrasoftHttpd::HttpResponse *response, int id) {
47 PDEBUG << id;
48 QList<Dbt::TicketTimesheets> list1 = db()->runningTimesheets(id);
49 if (list1.isEmpty()) {
50 QVariantMap item;
51 item["ok"] = false;
52 item["error"] = "conflict";
53 item["reason"] = "No running timesheet found";
54 serviceError(request, response, 409, "conflict", item);
55 return;
56 }
57
58 QVariantList items;
59 for (int i=0; i<list1.size(); i++) {
60 int xid = list1[0].ticket.toInt();
61 PDEBUG << "Stopping" << xid;
62 QList<Dbt::TicketTimesheets> list2 = db()->stopTimesheet(xid);
63 if (list2.isEmpty()) {
64 PDEBUG << "error, Some timesheets could not be stopped, id: " << xid;
65 continue;
66 }
67
68 QVariantMap item = list2[0].toMap();
69 item.remove("created");
70 item.remove("modified");
71 items << item;
72 }
73
74 if (items.isEmpty()) {
75 serviceError(request, response, 400, "error", "Some timesheets could not be stopped");
76 return;
77 }
78
79 serviceOK(request, response, items);
80}
81
82
83void ControllerTimesheet::serviceToggle(HobrasoftHttpd::HttpRequest *request, HobrasoftHttpd::HttpResponse *response, int id) {
84 PDEBUG;
85 QList<Dbt::TicketTimesheets> list2 = db()->toggleTimesheet(id);
86 if (list2.isEmpty()) {
87 serviceError(request, response, 400, "error", "Timesheet could not be toggled");
88 return;
89 }
90
91 QVariantMap item = list2[0].toMap();
92 item.remove("created");
93 item.remove("modified");
94 serviceOK(request, response, item);
95}
96
97
98void ControllerTimesheet::service(HobrasoftHttpd::HttpRequest *request, HobrasoftHttpd::HttpResponse *response) {
99 QStringList parts = request->path().split("/");
100 PDEBUG << parts;
101 parts.removeFirst();
102 parts.removeFirst();
103 parts.removeFirst();
104
105 while (parts.size() == 3) {
106 bool ok;
107 QString sid = parts[2];
108 int id = sid.toInt(&ok);
109 if (sid == "all") { ok = true; id = 0; }
110 if (!ok) { break; }
111
112 if (parts[0] == "timesheet" && parts[1] == "start") {
113 serviceStart(request, response, id);
114 return;
115 }
116
117 if (parts[0] == "timesheet" && parts[1] == "stop") {
118 serviceStop(request, response, id);
119 return;
120 }
121
122 if (parts[0] == "timesheet" && parts[1] == "toggle") {
123 serviceToggle(request, response, id);
124 return;
125 }
126
127 break;
128 }
129
130 serviceError(request, response, 405, "bad-request", "Invalid request");
131}
132
Virtuální třída pro vyřizování unifikovaných Http požadavků na server API.
virtual void serviceError(HobrasoftHttpd::HttpRequest *request, HobrasoftHttpd::HttpResponse *response, int code, const QString &error, const QVariantMap &data)
Pošle chybovou odpověď
virtual void serviceOK(HobrasoftHttpd::HttpRequest *request, HobrasoftHttpd::HttpResponse *response, const QVariant &data=QVariant())
Pošle odpověď 200 OK.
HobrasoftHttpd::HttpRequest * request() const
Vrací ukazatel na aktuální request.
void service(HobrasoftHttpd::HttpRequest *request, HobrasoftHttpd::HttpResponse *response) Q_DECL_OVERRIDE
Obsluha požadavku.
Jmenný prostor pro obsluhu konkrétních HTTP požadavků aplikace.