|
|
@ -1,7 +1,10 @@
|
|
|
|
#include "book_services.h"
|
|
|
|
#include "book_services.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include <list>
|
|
|
|
#include <list>
|
|
|
|
|
|
|
|
|
|
|
|
#include "boost/lexical_cast.hpp"
|
|
|
|
#include "boost/lexical_cast.hpp"
|
|
|
|
|
|
|
|
#include "boost/thread.hpp"
|
|
|
|
|
|
|
|
#include "boost/date_time/posix_time/posix_time.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
@ -21,6 +24,7 @@ static const Book kNullBook{};
|
|
|
|
class BookStore {
|
|
|
|
class BookStore {
|
|
|
|
public:
|
|
|
|
public:
|
|
|
|
BookStore() {
|
|
|
|
BookStore() {
|
|
|
|
|
|
|
|
// Prepare test data.
|
|
|
|
books_.push_back({ "1", "Title1", 11.1 });
|
|
|
|
books_.push_back({ "1", "Title1", 11.1 });
|
|
|
|
books_.push_back({ "2", "Title2", 22.2 });
|
|
|
|
books_.push_back({ "2", "Title2", 22.2 });
|
|
|
|
books_.push_back({ "3", "Title3", 33.3 });
|
|
|
|
books_.push_back({ "3", "Title3", 33.3 });
|
|
|
@ -77,8 +81,8 @@ static BookStore g_book_store;
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
// Naively create JSON object string for a book.
|
|
|
|
// Naively create JSON object for a book.
|
|
|
|
// You should use real JSON library in your product code.
|
|
|
|
// You should use real JSON library in real product.
|
|
|
|
static std::string CreateBookJson(const Book& book) {
|
|
|
|
static std::string CreateBookJson(const Book& book) {
|
|
|
|
std::string json = "{ ";
|
|
|
|
std::string json = "{ ";
|
|
|
|
json += "\"id\": " + book.id + ", ";
|
|
|
|
json += "\"id\": " + book.id + ", ";
|
|
|
@ -88,17 +92,36 @@ static std::string CreateBookJson(const Book& book) {
|
|
|
|
return json;
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Naively create JSON array object for a list of books.
|
|
|
|
|
|
|
|
// You should use real JSON library in real product.
|
|
|
|
|
|
|
|
static std::string CreateBookListJson(const std::list<Book>& books) {
|
|
|
|
|
|
|
|
std::string json = "[ ";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (const Book& book : books) {
|
|
|
|
|
|
|
|
json += CreateBookJson(book);
|
|
|
|
|
|
|
|
json += ",";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Remove last ','.
|
|
|
|
|
|
|
|
if (!books.empty()) {
|
|
|
|
|
|
|
|
json[json.size() - 1] = ' ';
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
std::string* response_content) {
|
|
|
|
std::string* response_content) {
|
|
|
|
if (http_method == webcc::kHttpGet) {
|
|
|
|
if (http_method == webcc::kHttpGet) {
|
|
|
|
*response_content = "{ ";
|
|
|
|
*response_content = CreateBookListJson(g_book_store.books());
|
|
|
|
for (const Book& book : g_book_store.books()) {
|
|
|
|
|
|
|
|
*response_content += CreateBookJson(book);
|
|
|
|
// Sleep for testing timeout control.
|
|
|
|
*response_content += ",";
|
|
|
|
boost::this_thread::sleep(boost::posix_time::seconds(2));
|
|
|
|
}
|
|
|
|
|
|
|
|
*response_content += " }";
|
|
|
|
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -126,6 +149,9 @@ bool BookDetailService::Handle(const std::string& http_method,
|
|
|
|
|
|
|
|
|
|
|
|
*response_content = CreateBookJson(book);
|
|
|
|
*response_content = CreateBookJson(book);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Sleep for testing timeout control.
|
|
|
|
|
|
|
|
//boost::this_thread::sleep(boost::posix_time::seconds(2));
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
|
|
} else if (http_method == webcc::kHttpPost) {
|
|
|
|
} else if (http_method == webcc::kHttpPost) {
|
|
|
|