lunch-rest/menu.cpp

82 lines
1.9 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 {
if(!isValid())
return "{\"day\": \"" + getDay() + "\", \"meals\": []}";
std::stringstream ss{};
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++) {
if(i != soupInd)
ss << getMeals()[i].jsonify() << ",";
}
if(getMeals().size() > 0)
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;
}