lunch-rest/menu.cpp
2020-09-15 21:28:41 +02:00

91 lines
2.2 KiB
C++

#include "menu.hpp"
#include <sstream>
void LunchRest::Menu::addMeal(bool soup, const std::string &name, const std::string &desc, int price) {
_meals.emplace_back(soup, name, desc, price);
}
void LunchRest::Menu::addMeal(const LunchRest::Meal &meal) {
_meals.push_back(meal);
}
bool LunchRest::Menu::hasSoup() const {
bool ret = false;
for(auto &x : _meals) {
ret |= x.isSoup();
}
return ret;
}
LunchRest::Meal LunchRest::Menu::getSoup() const {
for(auto &x : _meals) {
if(x.isSoup())
return x;
}
return Meal(true, "", "", 0);
}
std::vector<LunchRest::Meal> LunchRest::Menu::getNonSoupMeals() {
std::vector<Meal> ret{};
for(auto &x : _meals) {
if(!x.isSoup())
ret.push_back(x);
}
return ret;
}
const std::vector<LunchRest::Meal> &LunchRest::Menu::getMeals() const {
return _meals;
}
unsigned long int LunchRest::Menu::getSoupIndex() const {
for(unsigned long int i = 0; i < _meals.size(); i++) {
if(_meals[i].isSoup())
return i;
}
return -1;
}
void LunchRest::Menu::setInvalidMenu(bool invalid) {
_valid = !invalid;
}
bool LunchRest::Menu::isValid() const {
return _valid;
}
std::string LunchRest::Menu::jsonify() const {
return jsonify({});
}
std::string LunchRest::Menu::jsonify(const std::vector<Meal> &permanent) const {
if(!isValid() && permanent.empty())
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)
ss.seekp(-1, ss.cur);
ss << "]}";
return ss.str();
}
void LunchRest::Menu::setDay(const std::string &day) {
_day = day;
}
const std::string &LunchRest::Menu::getDay() const {
return _day;
}