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

View File

@ -22,7 +22,6 @@ public:
void setInvalidMenu( bool invalid = true ); void setInvalidMenu( bool invalid = true );
bool isValid() const; bool isValid() const;
std::string jsonify() const; std::string jsonify() const;
std::string jsonify(const std::vector<Meal> &permanent) const;
void setDay(const std::string &day); void setDay(const std::string &day);
const std::string &getDay() const; const std::string &getDay() const;
private: private:

View File

@ -33,12 +33,18 @@ public:
} }
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{};
ss << "{\"restaurant\": \"" << getRestaurant() << "\", \"menus\": ["; ss << "{\"restaurant\": \"" << getRestaurant() << "\", \"dailymenus\": [";
for(auto &day : days) { for(auto &day : days) {
ss << menus[day].jsonify(permanent) << ","; ss << menus[day].jsonify() << ",";
} }
if(!days.empty()) if(!days.empty())
ss.seekp(-1, ss.cur); ss.seekp(-1, ss.cur);
ss << "], \"permanentmeals\": [";
for(auto &meal : permanent) {
ss << meal.jsonify() << ",";
}
if(permanent.size() > 0)
ss.seekp(-1, ss.cur);
ss << "]}"; ss << "]}";
return ss.str(); return ss.str();
} }