Refine the REST book server example.

This commit is contained in:
Adam Gu
2018-04-09 17:21:46 +08:00
parent e2bf8aaaf4
commit 9abf8d99d5
2 changed files with 28 additions and 27 deletions
+23 -26
View File
@@ -8,6 +8,9 @@
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// In-memory test data.
// There should be some database in a real product.
class Book { class Book {
public: public:
std::string id; std::string id;
@@ -35,44 +38,36 @@ public:
} }
const Book& GetBook(const std::string& id) const { const Book& GetBook(const std::string& id) const {
auto it = std::find_if(books_.begin(), auto it = FindBook(id);
books_.end(), return (it == books_.end() ? kNullBook : *it);
[&id](const Book& book) { return book.id == id; });
if (it == books_.end()) {
return kNullBook;
}
return *it;
} }
bool AddBook(const Book& new_book) { bool AddBook(const Book& new_book) {
auto it = std::find_if( if (FindBook(new_book.id) == books_.end()) {
books_.begin(),
books_.end(),
[&new_book](const Book& book) { return book.id == new_book.id; });
if (it != books_.end()) {
return false;
}
books_.push_back(new_book); books_.push_back(new_book);
return true; return true;
} }
bool DeleteBook(const std::string& id) {
auto it = std::find_if(books_.begin(),
books_.end(),
[&id](const Book& book) { return book.id == id; });
if (it == books_.end()) {
return false; return false;
} }
bool DeleteBook(const std::string& id) {
auto it = FindBook(id);
if (it != books_.end()) {
books_.erase(it); books_.erase(it);
return true; return true;
} }
return false;
}
private:
std::list<Book>::const_iterator FindBook(const std::string& id) const {
return std::find_if(books_.begin(),
books_.end(),
[&id](const Book& book) { return book.id == id; });
}
private: private:
std::list<Book> books_; std::list<Book> books_;
}; };
@@ -112,6 +107,8 @@ static std::string CreateBookListJson(const std::list<Book>& books) {
return json; return json;
} }
////////////////////////////////////////////////////////////////////////////////
bool BookListService::Handle(const std::string& http_method, bool BookListService::Handle(const std::string& http_method,
const std::vector<std::string>& url_sub_matches, const std::vector<std::string>& url_sub_matches,
const std::string& request_content, const std::string& request_content,
@@ -120,7 +117,7 @@ bool BookListService::Handle(const std::string& http_method,
*response_content = CreateBookListJson(g_book_store.books()); *response_content = CreateBookListJson(g_book_store.books());
// Sleep for testing timeout control. // Sleep for testing timeout control.
boost::this_thread::sleep(boost::posix_time::seconds(2)); //boost::this_thread::sleep(boost::posix_time::seconds(2));
return true; return true;
} }
@@ -7,6 +7,8 @@
// XxxListService and XxxDetailService are similar to the XxxListView // XxxListService and XxxDetailService are similar to the XxxListView
// and XxxDetailView in Django (a Python web framework). // and XxxDetailView in Django (a Python web framework).
////////////////////////////////////////////////////////////////////////////////
// List Service handles the HTTP GET and returns the book list based on // List Service handles the HTTP GET and returns the book list based on
// query parameters specified in the URL. // query parameters specified in the URL.
// The URL should be like: // The URL should be like:
@@ -24,6 +26,8 @@ public:
std::string* response_content) override; std::string* response_content) override;
}; };
////////////////////////////////////////////////////////////////////////////////
// Detail Service handles the following HTTP methods: // Detail Service handles the following HTTP methods:
// - GET // - GET
// - PUT // - PUT