More stuff in json, cors

This commit is contained in:
zvon 2020-09-15 21:28:41 +02:00
parent 2d7cc4fab9
commit 09b314c5b0
5 changed files with 23 additions and 10 deletions

View File

@ -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() {

View File

@ -60,10 +60,10 @@ std::string LunchRest::Menu::jsonify() const {
std::string LunchRest::Menu::jsonify(const std::vector<Meal> &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<Meal> &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;
}

View File

@ -23,9 +23,12 @@ public:
bool isValid() const;
std::string jsonify() const;
std::string jsonify(const std::vector<Meal> &permanent) const;
void setDay(const std::string &day);
const std::string &getDay() const;
private:
std::vector<Meal> _meals;
bool _valid = true;
std::string _day = "";
};
} // end of namespace LunchRest

View File

@ -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<int> &days = {0,1,2,3,4}) {
std::stringstream ss{};

View File

@ -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");