More stuff in json, cors
This commit is contained in:
parent
2d7cc4fab9
commit
09b314c5b0
6
main.cpp
6
main.cpp
@ -23,7 +23,7 @@ void get_all( const std::shared_ptr< restbed::Session > session ) {
|
|||||||
ss.seekp(-1, ss.cur);
|
ss.seekp(-1, ss.cur);
|
||||||
ss << "]";
|
ss << "]";
|
||||||
auto res = ss.str();
|
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 ) {
|
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) {
|
if(day == -1 && restaurant == -1) {
|
||||||
std::string error = "DIDN'T SPECIFY DAY OR RESTAURANT";
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,7 +82,7 @@ void get( const std::shared_ptr< restbed::Session > session ) {
|
|||||||
ss << "[" << restaurants[restaurant]->jsonify() << "]";
|
ss << "[" << restaurants[restaurant]->jsonify() << "]";
|
||||||
}
|
}
|
||||||
auto res = ss.str();
|
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() {
|
int main() {
|
||||||
|
14
menu.cpp
14
menu.cpp
@ -60,10 +60,10 @@ std::string LunchRest::Menu::jsonify() const {
|
|||||||
|
|
||||||
std::string LunchRest::Menu::jsonify(const std::vector<Meal> &permanent) const {
|
std::string LunchRest::Menu::jsonify(const std::vector<Meal> &permanent) const {
|
||||||
if(!isValid() && permanent.empty())
|
if(!isValid() && permanent.empty())
|
||||||
return "[]";
|
return "{\"day\": \"" + getDay() + "\", \"meals\": []}";
|
||||||
std::stringstream ss{};
|
std::stringstream ss{};
|
||||||
bool atleastone = false;
|
bool atleastone = false;
|
||||||
ss << "[";
|
ss << "{\"day\": \"" << getDay() << "\", \"meals\": [";
|
||||||
auto soupInd = getSoupIndex();
|
auto soupInd = getSoupIndex();
|
||||||
if(soupInd != (unsigned long int)-1)
|
if(soupInd != (unsigned long int)-1)
|
||||||
ss << getMeals()[soupInd].jsonify() << ",";
|
ss << getMeals()[soupInd].jsonify() << ",";
|
||||||
@ -77,6 +77,14 @@ std::string LunchRest::Menu::jsonify(const std::vector<Meal> &permanent) const {
|
|||||||
}
|
}
|
||||||
if(atleastone)
|
if(atleastone)
|
||||||
ss.seekp(-1, ss.cur);
|
ss.seekp(-1, ss.cur);
|
||||||
ss << "]";
|
ss << "]}";
|
||||||
return ss.str();
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LunchRest::Menu::setDay(const std::string &day) {
|
||||||
|
_day = day;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string &LunchRest::Menu::getDay() const {
|
||||||
|
return _day;
|
||||||
|
}
|
||||||
|
3
menu.hpp
3
menu.hpp
@ -23,9 +23,12 @@ public:
|
|||||||
bool isValid() const;
|
bool isValid() const;
|
||||||
std::string jsonify() const;
|
std::string jsonify() const;
|
||||||
std::string jsonify(const std::vector<Meal> &permanent) const;
|
std::string jsonify(const std::vector<Meal> &permanent) const;
|
||||||
|
void setDay(const std::string &day);
|
||||||
|
const std::string &getDay() const;
|
||||||
private:
|
private:
|
||||||
std::vector<Meal> _meals;
|
std::vector<Meal> _meals;
|
||||||
bool _valid = true;
|
bool _valid = true;
|
||||||
|
std::string _day = "";
|
||||||
};
|
};
|
||||||
} // end of namespace LunchRest
|
} // end of namespace LunchRest
|
||||||
|
|
||||||
|
@ -25,6 +25,11 @@ public:
|
|||||||
menus.resize(5);
|
menus.resize(5);
|
||||||
for(auto &x : menus)
|
for(auto &x : menus)
|
||||||
x.setInvalidMenu();
|
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::string jsonify(const std::vector<int> &days = {0,1,2,3,4}) {
|
||||||
std::stringstream ss{};
|
std::stringstream ss{};
|
||||||
|
@ -10,14 +10,11 @@ std::string removeAlergens(const std::string &name) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void LunchRest::AlCaponeRestaurant::parse() {
|
void LunchRest::AlCaponeRestaurant::parse() {
|
||||||
menus.clear();
|
clearMenus();
|
||||||
Request r;
|
Request r;
|
||||||
auto html = r.get(_url);
|
auto html = r.get(_url);
|
||||||
if(html == "")
|
if(html == "")
|
||||||
return;
|
return;
|
||||||
menus.resize(5);
|
|
||||||
for(auto &menu : menus)
|
|
||||||
menu.setInvalidMenu();
|
|
||||||
HtmlParser hparse(html);
|
HtmlParser hparse(html);
|
||||||
auto &root = hparse.getRoot();
|
auto &root = hparse.getRoot();
|
||||||
auto rows = root.find("//table[@class='table table-responsive']/tbody/tr");
|
auto rows = root.find("//table[@class='table table-responsive']/tbody/tr");
|
||||||
|
Loading…
Reference in New Issue
Block a user