Refine the REST book server example.

master
Adam Gu 7 years ago
parent e2bf8aaaf4
commit 9abf8d99d5

@ -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,42 +38,34 @@ 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_.push_back(new_book);
books_.end(), return true;
[&new_book](const Book& book) { return book.id == new_book.id; });
if (it != books_.end()) {
return false;
} }
return false;
books_.push_back(new_book);
return true;
} }
bool DeleteBook(const std::string& id) { bool DeleteBook(const std::string& id) {
auto it = std::find_if(books_.begin(), auto it = FindBook(id);
books_.end(),
[&id](const Book& book) { return book.id == id; });
if (it == books_.end()) { if (it != books_.end()) {
return false; books_.erase(it);
return true;
} }
books_.erase(it); return false;
return true; }
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:
@ -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

Loading…
Cancel
Save