This commit is contained in:
Chunting Gu
2019-09-10 10:52:56 +08:00
2 changed files with 4 additions and 16 deletions
+2 -9
View File
@@ -198,7 +198,7 @@ int main() {
} }
``` ```
### Book Store ### Book Server
Suppose you want to create a book server and provide the following operations with RESTful API: Suppose you want to create a book server and provide the following operations with RESTful API:
@@ -217,11 +217,9 @@ public:
if (request->method() == "GET") { if (request->method() == "GET") {
return Get(request); return Get(request);
} }
if (request->method() == "POST") { if (request->method() == "POST") {
return Post(request); return Post(request);
} }
return {}; return {};
} }
@@ -238,23 +236,18 @@ private:
Other operations are implemented by `BookDetailView`: Other operations are implemented by `BookDetailView`:
```cpp ```cpp
// The URL is like '/books/{BookID}', and the 'args' parameter contains
// the matched book ID.
class BookDetailView : public webcc::View { class BookDetailView : public webcc::View {
public: public:
webcc::ResponsePtr Handle(webcc::RequestPtr request) override { webcc::ResponsePtr Handle(webcc::RequestPtr request) override {
if (request->method() == "GET") { if (request->method() == "GET") {
return Get(request); return Get(request);
} }
if (request->method() == "PUT") { if (request->method() == "PUT") {
return Put(request); return Put(request);
} }
if (request->method() == "DELETE") { if (request->method() == "DELETE") {
return Delete(request); return Delete(request);
} }
return {}; return {};
} }
@@ -323,4 +316,4 @@ int main(int argc, char* argv[]) {
return 0; return 0;
``` ```
Please see [examples/rest_book_server.cc](https://github.com/sprinfall/webcc/tree/master/examples/rest_book_server.cc) for more details. Please see [examples/book_server](https://github.com/sprinfall/webcc/tree/master/examples/book_server) for more details.
+2 -7
View File
@@ -204,7 +204,7 @@ int main() {
下面看一个更复杂的例子。 下面看一个更复杂的例子。
### 在线书店 ### Book Server
假定你想创建一个关于书的服务,提供下面这些 REST API: 假定你想创建一个关于书的服务,提供下面这些 REST API:
@@ -227,11 +227,9 @@ public:
if (request->method() == "GET") { if (request->method() == "GET") {
return Get(request); return Get(request);
} }
if (request->method() == "POST") { if (request->method() == "POST") {
return Post(request); return Post(request);
} }
return {}; return {};
} }
@@ -253,15 +251,12 @@ public:
if (request->method() == "GET") { if (request->method() == "GET") {
return Get(request); return Get(request);
} }
if (request->method() == "PUT") { if (request->method() == "PUT") {
return Put(request); return Put(request);
} }
if (request->method() == "DELETE") { if (request->method() == "DELETE") {
return Delete(request); return Delete(request);
} }
return {}; return {};
} }
@@ -335,4 +330,4 @@ int main(int argc, char* argv[]) {
} }
``` ```
完整实现请见 [examples/rest_book_server.cc](https://github.com/sprinfall/webcc/tree/master/examples/rest_book_server.cc)。 完整实现请见 [examples/book_server](https://github.com/sprinfall/webcc/tree/master/examples/book_server)。