diff --git a/main.cpp b/main.cpp index 798d01c..833ed5b 100644 --- a/main.cpp +++ b/main.cpp @@ -23,7 +23,7 @@ void get_all( const std::shared_ptr< restbed::Session > session ) { ss.seekp(-1, ss.cur); ss << "]"; auto res = ss.str(); - session->close(restbed::OK, res, { { "Content-Length", std::to_string(res.length()) } }); + session->close(restbed::OK, res, { { "Content-Length", std::to_string(res.length()) }, { "Access-Control-Allow-Origin", "*" } }); } void get( const std::shared_ptr< restbed::Session > session ) { @@ -61,7 +61,7 @@ void get( const std::shared_ptr< restbed::Session > session ) { } if(day == -1 && restaurant == -1) { std::string error = "DIDN'T SPECIFY DAY OR RESTAURANT"; - session->close(restbed::OK, error, { { "Content-Length", std::to_string(error.length()) } }); + session->close(restbed::OK, error, { { "Content-Length", std::to_string(error.length()) }, { "Access-Control-Allow-Origin", "*" } }); return; } @@ -82,7 +82,7 @@ void get( const std::shared_ptr< restbed::Session > session ) { ss << "[" << restaurants[restaurant]->jsonify() << "]"; } auto res = ss.str(); - session->close(restbed::OK, res, { { "Content-Length", std::to_string(res.length()) } }); + session->close(restbed::OK, res, { { "Content-Length", std::to_string(res.length()) }, { "Access-Control-Allow-Origin", "*" } }); } int main() { diff --git a/menu.cpp b/menu.cpp index cb35bba..8b12628 100644 --- a/menu.cpp +++ b/menu.cpp @@ -60,10 +60,10 @@ std::string LunchRest::Menu::jsonify() const { std::string LunchRest::Menu::jsonify(const std::vector &permanent) const { if(!isValid() && permanent.empty()) - return "[]"; + return "{\"day\": \"" + getDay() + "\", \"meals\": []}"; std::stringstream ss{}; bool atleastone = false; - ss << "["; + ss << "{\"day\": \"" << getDay() << "\", \"meals\": ["; auto soupInd = getSoupIndex(); if(soupInd != (unsigned long int)-1) ss << getMeals()[soupInd].jsonify() << ","; @@ -77,6 +77,14 @@ std::string LunchRest::Menu::jsonify(const std::vector &permanent) const { } if(atleastone) ss.seekp(-1, ss.cur); - ss << "]"; + ss << "]}"; return ss.str(); } + +void LunchRest::Menu::setDay(const std::string &day) { + _day = day; +} + +const std::string &LunchRest::Menu::getDay() const { + return _day; +} diff --git a/menu.hpp b/menu.hpp index 7559464..ebbfc27 100644 --- a/menu.hpp +++ b/menu.hpp @@ -23,9 +23,12 @@ public: bool isValid() const; std::string jsonify() const; std::string jsonify(const std::vector &permanent) const; + void setDay(const std::string &day); + const std::string &getDay() const; private: std::vector _meals; bool _valid = true; + std::string _day = ""; }; } // end of namespace LunchRest diff --git a/restaurant.hpp b/restaurant.hpp index eb0c449..d9662e1 100644 --- a/restaurant.hpp +++ b/restaurant.hpp @@ -25,6 +25,11 @@ public: menus.resize(5); for(auto &x : menus) x.setInvalidMenu(); + menus[0].setDay("Monday"); + menus[1].setDay("Tuesday"); + menus[2].setDay("Wednesday"); + menus[3].setDay("Thursday"); + menus[4].setDay("Friday"); } std::string jsonify(const std::vector &days = {0,1,2,3,4}) { std::stringstream ss{}; diff --git a/restaurants/alcapone.cpp b/restaurants/alcapone.cpp index 7ceddc8..0f02c26 100644 --- a/restaurants/alcapone.cpp +++ b/restaurants/alcapone.cpp @@ -10,14 +10,11 @@ std::string removeAlergens(const std::string &name) { } void LunchRest::AlCaponeRestaurant::parse() { - menus.clear(); + clearMenus(); Request r; auto html = r.get(_url); if(html == "") return; - menus.resize(5); - for(auto &menu : menus) - menu.setInvalidMenu(); HtmlParser hparse(html); auto &root = hparse.getRoot(); auto rows = root.find("//table[@class='table table-responsive']/tbody/tr");