Seperate daily menu from permanent menu in json

This commit is contained in:
zv0n 2020-09-16 12:56:16 +02:00
parent 0fbba2c010
commit 0702c6ebb1
3 changed files with 10 additions and 14 deletions

View File

@ -55,27 +55,18 @@ bool LunchRest::Menu::isValid() const {
}
std::string LunchRest::Menu::jsonify() const {
return jsonify({});
}
std::string LunchRest::Menu::jsonify(const std::vector<Meal> &permanent) const {
if(!isValid() && permanent.empty())
if(!isValid())
return "{\"day\": \"" + getDay() + "\", \"meals\": []}";
std::stringstream ss{};
bool atleastone = false;
ss << "{\"day\": \"" << getDay() << "\", \"meals\": [";
auto soupInd = getSoupIndex();
if(soupInd != (unsigned long int)-1)
ss << getMeals()[soupInd].jsonify() << ",";
for(unsigned long int i = 0; i < getMeals().size(); i++) {
atleastone = true;
if(i != soupInd)
ss << getMeals()[i].jsonify() << ",";
}
for(auto &meal : permanent) {
ss << meal.jsonify() << ",";
}
if(atleastone)
if(getMeals().size() > 0)
ss.seekp(-1, ss.cur);
ss << "]}";
return ss.str();

View File

@ -22,7 +22,6 @@ public:
void setInvalidMenu( bool invalid = true );
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:

View File

@ -33,12 +33,18 @@ public:
}
std::string jsonify(const std::vector<int> &days = {0,1,2,3,4}) {
std::stringstream ss{};
ss << "{\"restaurant\": \"" << getRestaurant() << "\", \"menus\": [";
ss << "{\"restaurant\": \"" << getRestaurant() << "\", \"dailymenus\": [";
for(auto &day : days) {
ss << menus[day].jsonify(permanent) << ",";
ss << menus[day].jsonify() << ",";
}
if(!days.empty())
ss.seekp(-1, ss.cur);
ss << "], \"permanentmeals\": [";
for(auto &meal : permanent) {
ss << meal.jsonify() << ",";
}
if(permanent.size() > 0)
ss.seekp(-1, ss.cur);
ss << "]}";
return ss.str();
}