diff --git a/Makefile b/Makefile index 151c8a9..21ed062 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CXX ?= g++ CFLAGS ?= -O2 -Wall -Wextra `pkg-config libxml-2.0 --cflags` `pkg-config libxml++-3.0 --cflags` PREFIX ?= /usr/local/bin -LDFLAGS ?= -lcurl `pkg-config libxml-2.0 --libs` `pkg-config libxml++-3.0 --libs` +LDFLAGS ?= -lcurl -lrestbed `pkg-config libxml-2.0 --libs` `pkg-config libxml++-3.0 --libs` PARSERS = udrevaka.o padagali.o lightofindia.o ukarla.o diff --git a/main.cpp b/main.cpp index 35e2895..9ab3037 100644 --- a/main.cpp +++ b/main.cpp @@ -5,10 +5,13 @@ #include #include +#include +#include std::vector days = {"MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY"}; +std::vector> parsers; -int main() { +/*int main() { std::vector> parsers; parsers.emplace_back(new LunchRest::UDrevakaParser()); parsers.emplace_back(new LunchRest::PadagaliParser()); @@ -31,4 +34,48 @@ int main() { } } } +}*/ + +void get_all( const std::shared_ptr< restbed::Session > session ) { + std::stringstream ss{}; + ss << "["; + for(auto &x : parsers) { + ss << "{\"restaurant\": \"" << x->getRestaurant() << "\", \"menus\": ["; + for(const auto &y : x->getMenus()) { + if(y.isValid()) { + ss << y.jsonify() << ","; + } + } + ss.seekp(-1, ss.cur); + ss << "]},"; + } + ss.seekp(-1, ss.cur); + ss << "]"; + auto res = ss.str(); + session->close(restbed::OK, res, { { "Content-Length", std::to_string(res.length()) } }); +} + +int main() { + parsers.emplace_back(new LunchRest::UDrevakaParser()); + parsers.emplace_back(new LunchRest::PadagaliParser()); + parsers.emplace_back(new LunchRest::LightOfIndiaParser()); + parsers.emplace_back(new LunchRest::UKarlaParser()); + std::cout << "Initial parsing" << std::endl; + for(auto &x : parsers) + x->parse(); + std::cout << "Finished parsing" << std::endl; + + auto resource = std::make_shared< restbed::Resource >(); + resource->set_path("/get_all"); + resource->set_method_handler( "GET", get_all ); + + auto settings = std::make_shared< restbed::Settings >(); + settings->set_port(1984); + settings->set_default_header( "Connection", "close" ); + + restbed::Service service; + service.publish(resource); + service.start(settings); + + return 0; } diff --git a/meal.cpp b/meal.cpp index a64b898..032e3d7 100644 --- a/meal.cpp +++ b/meal.cpp @@ -1,4 +1,5 @@ #include "meal.hpp" +#include std::ostream &operator<<(std::ostream &os, const LunchRest::Meal &meal) { if(meal.isSoup()) @@ -10,3 +11,9 @@ std::ostream &operator<<(std::ostream &os, const LunchRest::Meal &meal) { os << ", " << meal.getPrice() << " czk"; return os; } + +std::string LunchRest::Meal::jsonify() const { + std::stringstream ss; + ss << "{\"name\": \"" << _name << "\", \"description\": \"" << _desc << "\", \"isSoup\": " << (_isSoup ? "true" : "false") << ", \"price\": " << _price << "}"; + return ss.str(); +} diff --git a/meal.hpp b/meal.hpp index 0e1d1e6..3806110 100644 --- a/meal.hpp +++ b/meal.hpp @@ -35,6 +35,7 @@ public: void setDesc(const std::string &desc) { _desc = desc; } + std::string jsonify() const; private: bool _isSoup = false; std::string _name; diff --git a/menu.cpp b/menu.cpp index beac1e3..4c0b6e1 100644 --- a/menu.cpp +++ b/menu.cpp @@ -1,4 +1,5 @@ #include "menu.hpp" +#include void LunchRest::Menu::addMeal(bool soup, const std::string &name, const std::string &desc, int price) { _meals.emplace_back(soup, name, desc, price); @@ -52,3 +53,17 @@ void LunchRest::Menu::setInvalidMenu() { bool LunchRest::Menu::isValid() const { return _valid; } + +std::string LunchRest::Menu::jsonify() const { + std::stringstream ss{}; + ss << "["; + auto soupInd = getSoupIndex(); + ss << getMeals()[soupInd].jsonify() << ","; + for(unsigned long int i = 0; i < getMeals().size(); i++) { + if(i != soupInd) + ss << getMeals()[i].jsonify() << ","; + } + ss.seekp(-1, ss.cur); + ss << "]"; + return ss.str(); +} diff --git a/menu.hpp b/menu.hpp index de15fa8..bd61a03 100644 --- a/menu.hpp +++ b/menu.hpp @@ -21,6 +21,7 @@ public: unsigned long int getSoupIndex() const; void setInvalidMenu(); bool isValid() const; + std::string jsonify() const; private: std::vector _meals; bool _valid = true;