You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
146 lines
3.8 KiB
C++
146 lines
3.8 KiB
C++
#include "example/rest_book_server/services.h"
|
|
|
|
#include <iostream>
|
|
#include <list>
|
|
|
|
#include "boost/thread/thread.hpp"
|
|
#include "json/json.h"
|
|
#include "webcc/logger.h"
|
|
|
|
#include "example/common/book.h"
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
static BookStore g_book_store;
|
|
|
|
static Json::Value BookToJson(const Book& book) {
|
|
Json::Value root;
|
|
root["id"] = book.id;
|
|
root["title"] = book.title;
|
|
root["price"] = book.price;
|
|
return root;
|
|
}
|
|
|
|
static bool JsonToBook(const std::string& json, Book* book) {
|
|
Json::Value root;
|
|
Json::CharReaderBuilder builder;
|
|
std::stringstream stream(json);
|
|
std::string errs;
|
|
if (!Json::parseFromStream(builder, stream, &root, &errs)) {
|
|
std::cerr << errs << std::endl;
|
|
return false;
|
|
}
|
|
|
|
book->id = root["id"].asString();
|
|
book->title = root["title"].asString();
|
|
book->price = root["price"].asDouble();
|
|
|
|
return true;
|
|
}
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// Return all books as a JSON array.
|
|
// TODO: Support query parameters.
|
|
bool BookListService::Get(const webcc::UrlQuery& /* query */,
|
|
std::string* response_content) {
|
|
if (sleep_seconds_ > 0) {
|
|
LOG_INFO("Sleep %d seconds...", sleep_seconds_);
|
|
boost::this_thread::sleep_for(boost::chrono::seconds(sleep_seconds_));
|
|
}
|
|
|
|
Json::Value root(Json::arrayValue);
|
|
for (const Book& book : g_book_store.books()) {
|
|
root.append(BookToJson(book));
|
|
}
|
|
|
|
Json::StreamWriterBuilder builder;
|
|
*response_content = Json::writeString(builder, root);
|
|
|
|
return true;
|
|
}
|
|
|
|
// Add a new book.
|
|
// No response content.
|
|
bool BookListService::Post(const std::string& request_content,
|
|
std::string* /* response_content */) {
|
|
if (sleep_seconds_ > 0) {
|
|
LOG_INFO("Sleep %d seconds...", sleep_seconds_);
|
|
boost::this_thread::sleep_for(boost::chrono::seconds(sleep_seconds_));
|
|
}
|
|
|
|
Book book;
|
|
if (JsonToBook(request_content, &book)) {
|
|
g_book_store.AddBook(book); // TODO: return ID
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
bool BookDetailService::Get(const std::vector<std::string>& url_sub_matches,
|
|
const webcc::UrlQuery& query,
|
|
std::string* response_content) {
|
|
if (sleep_seconds_ > 0) {
|
|
LOG_INFO("Sleep %d seconds...", sleep_seconds_);
|
|
boost::this_thread::sleep_for(boost::chrono::seconds(sleep_seconds_));
|
|
}
|
|
|
|
if (url_sub_matches.size() != 1) {
|
|
return false;
|
|
}
|
|
|
|
const std::string& book_id = url_sub_matches[0];
|
|
|
|
const Book& book = g_book_store.GetBook(book_id);
|
|
if (!book.IsNull()) {
|
|
Json::StreamWriterBuilder builder;
|
|
*response_content = Json::writeString(builder, BookToJson(book));
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
// Update a book.
|
|
bool BookDetailService::Put(const std::vector<std::string>& url_sub_matches,
|
|
const std::string& request_content,
|
|
std::string* response_content) {
|
|
if (sleep_seconds_ > 0) {
|
|
LOG_INFO("Sleep %d seconds...", sleep_seconds_);
|
|
boost::this_thread::sleep_for(boost::chrono::seconds(sleep_seconds_));
|
|
}
|
|
|
|
if (url_sub_matches.size() != 1) {
|
|
return false;
|
|
}
|
|
|
|
const std::string& book_id = url_sub_matches[0];
|
|
|
|
Book book;
|
|
if (JsonToBook(request_content, &book)) {
|
|
book.id = book_id;
|
|
return g_book_store.UpdateBook(book);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
bool BookDetailService::Delete(
|
|
const std::vector<std::string>& url_sub_matches) {
|
|
if (sleep_seconds_ > 0) {
|
|
LOG_INFO("Sleep %d seconds...", sleep_seconds_);
|
|
boost::this_thread::sleep_for(boost::chrono::seconds(sleep_seconds_));
|
|
}
|
|
|
|
if (url_sub_matches.size() != 1) {
|
|
return false;
|
|
}
|
|
|
|
const std::string& book_id = url_sub_matches[0];
|
|
|
|
return g_book_store.DeleteBook(book_id);
|
|
}
|