Use map for storing restaurants

This commit is contained in:
zv0n 2020-09-16 09:48:28 +02:00
parent 5b67c65a03
commit b390a57e2f

View File

@ -4,39 +4,43 @@
#include "restaurants/restaurants.hpp" #include "restaurants/restaurants.hpp"
#include <iostream> #include <iostream>
#include <map>
#include <memory> #include <memory>
#include <restbed> #include <restbed>
#include <sstream> #include <sstream>
std::vector<std::string> days = {"MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY"}; std::vector<std::string> days = {"MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY"};
std::vector<std::unique_ptr<LunchRest::Restaurant>> restaurants; std::map<std::string, std::unique_ptr<LunchRest::Restaurant>> restaurants;
void sendResponse(const std::string &response, int status_code, const std::shared_ptr< restbed::Session > session) {
session->close(status_code, response, { { "Content-Length", std::to_string(response.length()) }, { "Access-Control-Allow-Origin", "*" } });
}
void refresh( const std::shared_ptr< restbed::Session > session ) { void refresh( const std::shared_ptr< restbed::Session > session ) {
for(auto &x : restaurants) for(auto &restaurant : restaurants)
x->parse(); restaurant.second->parse();
std::string response = "Refreshed menus!"; std::string response = "Refreshed menus!";
session->close(restbed::OK, response, { { "Content-Length", std::to_string(response.length()) }, { "Access-Control-Allow-Origin", "*" } }); sendResponse( "Refreshed menus!", restbed::OK, session );
} }
void get_all( const std::shared_ptr< restbed::Session > session ) { void get_all( const std::shared_ptr< restbed::Session > session ) {
std::stringstream ss{}; std::stringstream ss{};
bool atleastonerest = false; bool atleastonerest = false;
ss << "["; ss << "[";
for(auto &x : restaurants) { for(auto &restaurant : restaurants) {
atleastonerest = true; atleastonerest = true;
ss << x->jsonify() << ","; ss << restaurant.second->jsonify() << ",";
} }
if(atleastonerest) if(atleastonerest)
ss.seekp(-1, ss.cur); ss.seekp(-1, ss.cur);
ss << "]"; ss << "]";
auto res = ss.str(); sendResponse(ss.str(), restbed::OK, session);
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 ) {
// TODO better // TODO better
int day = -1; int day = -1;
int restaurant = -1; std::string restaurant = "";
auto request = session->get_request(); auto request = session->get_request();
for(const auto &query_param : request->get_query_parameters()) { for(const auto &query_param : request->get_query_parameters()) {
if(query_param.first == "day") { if(query_param.first == "day") {
@ -48,59 +52,55 @@ void get( const std::shared_ptr< restbed::Session > session ) {
day = 2; day = 2;
} else if(query_param.second == "thursday") { } else if(query_param.second == "thursday") {
day = 3; day = 3;
} else { } else if(query_param.second == "friday") {
day = 4; day = 4;
} } else if(query_param.second == "saturday") {
} day = 5;
if(query_param.first == "restaurant") {
if(query_param.second == "udrevaka") {
restaurant = 0;
} else if(query_param.second == "padagali") {
restaurant = 1;
} else if(query_param.second == "lightofindia") {
restaurant = 2;
} else if(query_param.second == "ukarla") {
restaurant = 3;
} else { } else {
restaurant = 4; day = 6;
} }
} }
if(query_param.first == "restaurant")
restaurant = query_param.second;
} }
if(day == -1 && restaurant == -1) { if(day == -1 && restaurant == "") {
std::string error = "DIDN'T SPECIFY DAY OR RESTAURANT"; sendResponse("DIDN'T SPECIFY DAY OR RESTAURANT", restbed::BAD_REQUEST, session);
session->close(restbed::OK, error, { { "Content-Length", std::to_string(error.length()) }, { "Access-Control-Allow-Origin", "*" } }); return;
}
if(restaurant != "" && restaurants.find(restaurant) == restaurants.end()) {
sendResponse("YOU DIDN'T SPECIFY A VALID RESTAURANT!", restbed::BAD_REQUEST, session);
return; return;
} }
std::stringstream ss{}; std::stringstream ss{};
if(day != -1 && restaurant != -1) { if(day != -1 && restaurant != "") {
ss << "[" << restaurants[restaurant]->jsonify({day}) << "]"; ss << "[" << restaurants[restaurant]->jsonify({day}) << "]";
} else if(day != -1) { } else if(day != -1) {
ss << "["; ss << "[";
bool atleastone = false; bool atleastone = false;
for(auto &x : restaurants) { for(auto &restaurant : restaurants) {
atleastone = true; atleastone = true;
ss << x->jsonify({day}) << ","; ss << restaurant.second->jsonify({day}) << ",";
} }
if(atleastone) if(atleastone)
ss.seekp(-1, ss.cur); ss.seekp(-1, ss.cur);
ss << "]"; ss << "]";
} else if(restaurant != -1) { } else if(restaurant != "") {
ss << "[" << restaurants[restaurant]->jsonify() << "]"; ss << "[" << restaurants[restaurant]->jsonify() << "]";
} }
auto res = ss.str(); sendResponse(ss.str(), restbed::OK, session);
session->close(restbed::OK, res, { { "Content-Length", std::to_string(res.length()) }, { "Access-Control-Allow-Origin", "*" } });
} }
int main() { int main() {
restaurants.emplace_back(new LunchRest::UDrevakaRestaurant()); restaurants["udrevaka"] = std::make_unique<LunchRest::UDrevakaRestaurant>();
restaurants.emplace_back(new LunchRest::PadagaliRestaurant()); restaurants["padagali"] = std::make_unique<LunchRest::PadagaliRestaurant>();
restaurants.emplace_back(new LunchRest::LightOfIndiaRestaurant()); restaurants["lightofindia"] = std::make_unique<LunchRest::LightOfIndiaRestaurant>();
restaurants.emplace_back(new LunchRest::UKarlaRestaurant()); restaurants["ukarla"] = std::make_unique<LunchRest::UKarlaRestaurant>();
restaurants.emplace_back(new LunchRest::AlCaponeRestaurant()); restaurants["alcapone"] = std::make_unique<LunchRest::AlCaponeRestaurant>();
std::cout << "Initial parsing" << std::endl; std::cout << "Initial parsing" << std::endl;
for(auto &x : restaurants) for(auto &restaurant : restaurants)
x->parse(); restaurant.second->parse();
std::cout << "Finished parsing" << std::endl; std::cout << "Finished parsing" << std::endl;
restbed::Service service; restbed::Service service;