From 11551a0193f20e9678a0bd6d16482a358bdff818 Mon Sep 17 00:00:00 2001 From: Chunting Gu Date: Fri, 24 Aug 2018 17:07:07 +0800 Subject: [PATCH] Update example in the README. --- README.md | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 63a7541..e757941 100644 --- a/README.md +++ b/README.md @@ -23,36 +23,37 @@ class BookListService : public webcc::RestListService { protected: // Query books based on some criterias. // GET /books? - bool Get(const webcc::UrlQuery& query, - std::string* response_content) override; + void Get(const webcc::UrlQuery& query, + webcc::RestResponse* response) override; // Add a new book. // POST /books // The new book's data is attached as request content in JSON format. - bool Post(const std::string& request_content, - std::string* response_content) override; + void Post(const std::string& request_content, + webcc::RestResponse* response) override; }; ``` The others, derive from `webcc::RestDetailService`: ```cpp -// The URL is like '/book/{BookID}', and the 'url_sub_matches' parameter +// The URL is like '/books/{BookID}', and the 'url_sub_matches' parameter // contains the matched book ID. class BookDetailService : public webcc::RestDetailService { protected: // Get the detailed information of a book. - bool Get(const std::vector& url_sub_matches, + void Get(const std::vector& url_sub_matches, const webcc::UrlQuery& query, - std::string* response_content) override; + webcc::RestResponse* response) override; // Update the information of a book. - bool Put(const std::vector& url_sub_matches, + void Put(const std::vector& url_sub_matches, const std::string& request_content, - std::string* response_content) override; + webcc::RestResponse* response) override; // Delete a book. - bool Delete(const std::vector& url_sub_matches) override; + void Delete(const std::vector& url_sub_matches, + webcc::RestResponse* response) override; }; ``` @@ -63,15 +64,23 @@ The detailed implementation is out of the scope of this document, but here is an ```cpp bool BookDetailService::Get(const std::vector& url_sub_matches, const webcc::UrlQuery& query, - std::string* response_content) { + webcc::RestResponse* response) { if (url_sub_matches.size() != 1) { - return false; + response->status = webcc::HttpStatus::kBadRequest; + return; } + const std::string& book_id = url_sub_matches[0]; - // Get the book by ID from database. - // Convert the book details to JSON string. - // Assign JSON string to response_content. + // Get the book by ID from, e.g., database. + // ... + + if () { + response->status = webcc::HttpStatus::kNotFound; + } else { + response->content = ; + response->status = webcc::HttpStatus::kOK; + } } ``` @@ -81,12 +90,12 @@ Last step, bind the services and run the server: webcc::RestServer server(8080, 2); server.Bind(std::make_shared(), "/books", false); -server.Bind(std::make_shared(), "/book/(\\d+)", true); +server.Bind(std::make_shared(), "/books/(\\d+)", true); server.Run(); ``` -**Please see the `example/rest` folder for the complete examples (including the client).** +**Please see `example/rest_book_server` for the complete example.** ## Build Instructions