-
Notifications
You must be signed in to change notification settings - Fork 62
/
views.cc
230 lines (176 loc) · 5.69 KB
/
views.cc
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include "views.h"
#include <filesystem>
#include "json/json.h"
#include "webcc/response_builder.h"
#include "book.h"
#include "book_db.h"
#include "book_json.h"
namespace sfs = std::filesystem;
// -----------------------------------------------------------------------------
static BookDB g_book_db;
// -----------------------------------------------------------------------------
webcc::ResponsePtr BookListView::Handle(webcc::RequestPtr request) {
if (request->method() == "GET") {
return Get(request);
}
if (request->method() == "POST") {
return Post(request);
}
return {};
}
webcc::ResponsePtr BookListView::Get(webcc::RequestPtr request) {
Json::Value json(Json::arrayValue);
for (const Book& book : g_book_db.books()) {
json.append(BookToJson(book));
}
// Return all books as a JSON array.
return webcc::ResponseBuilder{}.OK().Body(JsonToString(json)).Json().Utf8()();
}
webcc::ResponsePtr BookListView::Post(webcc::RequestPtr request) {
Book book;
if (JsonStringToBook(request->data(), &book)) {
std::string id = g_book_db.Add(book);
Json::Value json;
json["id"] = id;
return webcc::ResponseBuilder{}.Created().Body(JsonToString(json)).Json().Utf8()();
} else {
// Invalid JSON
return webcc::ResponseBuilder{}.BadRequest()();
}
}
// -----------------------------------------------------------------------------
BookDetailView::BookDetailView(sfs::path photo_dir)
: photo_dir_(std::move(photo_dir)) {
}
webcc::ResponsePtr BookDetailView::Handle(webcc::RequestPtr request) {
if (request->method() == "GET") {
return Get(request);
}
if (request->method() == "PUT") {
return Put(request);
}
if (request->method() == "DELETE") {
return Delete(request);
}
return {};
}
webcc::ResponsePtr BookDetailView::Get(webcc::RequestPtr request) {
if (request->args().size() != 1) {
// NotFound means the resource specified by the URL cannot be found.
// BadRequest could be another choice.
return webcc::ResponseBuilder{}.NotFound()();
}
const std::string& id = request->args()[0];
const Book& book = g_book_db.Get(id);
if (book.IsNull()) {
return webcc::ResponseBuilder{}.NotFound()();
}
return webcc::ResponseBuilder{}
.OK()
.Body(BookToJsonString(book))
.Json()
.Utf8()();
}
webcc::ResponsePtr BookDetailView::Put(webcc::RequestPtr request) {
if (request->args().size() != 1) {
return webcc::ResponseBuilder{}.NotFound()();
}
const std::string& id = request->args()[0];
Book book;
if (!JsonStringToBook(request->data(), &book)) {
return webcc::ResponseBuilder{}.BadRequest()();
}
book.id = id;
g_book_db.Set(book);
return webcc::ResponseBuilder{}.OK()();
}
webcc::ResponsePtr BookDetailView::Delete(webcc::RequestPtr request) {
if (request->args().size() != 1) {
return webcc::ResponseBuilder{}.NotFound()();
}
const std::string& id = request->args()[0];
std::string photo_name = g_book_db.GetPhoto(id);
// Delete the book from DB.
if (!g_book_db.Delete(id)) {
return webcc::ResponseBuilder{}.NotFound()();
}
// Delete the photo from file system.
if (!photo_name.empty()) {
std::error_code ec;
sfs::remove(photo_dir_ / photo_name, ec);
}
return webcc::ResponseBuilder{}.OK()();
}
// -----------------------------------------------------------------------------
BookPhotoView::BookPhotoView(sfs::path photo_dir)
: photo_dir_(std::move(photo_dir)) {
}
webcc::ResponsePtr BookPhotoView::Handle(webcc::RequestPtr request) {
if (request->method() == "GET") {
return Get(request);
}
if (request->method() == "PUT") {
return Put(request);
}
if (request->method() == "DELETE") {
return Delete(request);
}
return {};
}
// TODO: Check content type to see if it's JPEG.
webcc::ResponsePtr BookPhotoView::Get(webcc::RequestPtr request) {
if (request->args().size() != 1) {
return webcc::ResponseBuilder{}.NotFound()();
}
const std::string& id = request->args()[0];
const Book& book = g_book_db.Get(id);
if (book.IsNull() || book.photo.empty()) {
return webcc::ResponseBuilder{}.NotFound()();
}
sfs::path photo_path = photo_dir_ / book.photo;
if (!sfs::exists(photo_path)) {
return webcc::ResponseBuilder{}.NotFound()();
}
// File() might throw error_codes::kFileError.
// TODO: Avoid exception handling.
try {
return webcc::ResponseBuilder{}.OK().File(photo_path)();
} catch (const webcc::Error&) {
return webcc::ResponseBuilder{}.NotFound()();
}
}
webcc::ResponsePtr BookPhotoView::Put(webcc::RequestPtr request) {
if (request->args().size() != 1) {
return webcc::ResponseBuilder{}.NotFound()();
}
const std::string& id = request->args()[0];
const Book& book = g_book_db.Get(id);
if (book.IsNull()) {
return webcc::ResponseBuilder{}.NotFound()();
}
// Use ID as the name of the photo.
// You can also use a UUID or any unique string as the name.
auto photo_name = id + ".jpg";
if (!request->file_body()->Move(photo_dir_ / photo_name)) {
return webcc::ResponseBuilder{}.InternalServerError()();
}
// Set photo name to DB.
if (!g_book_db.SetPhoto(id, photo_name)) {
return webcc::ResponseBuilder{}.InternalServerError()();
}
return webcc::ResponseBuilder{}.OK()();
}
webcc::ResponsePtr BookPhotoView::Delete(webcc::RequestPtr request) {
if (request->args().size() != 1) {
return webcc::ResponseBuilder{}.NotFound()();
}
const std::string& id = request->args()[0];
const Book& book = g_book_db.Get(id);
if (book.IsNull() || book.photo.empty()) {
return webcc::ResponseBuilder{}.NotFound()();
}
// Error handling is simplified.
std::error_code ec;
sfs::remove(photo_dir_ / book.photo, ec);
return webcc::ResponseBuilder{}.OK()();
}