Use map for storing restaurants
This commit is contained in:
parent
5b67c65a03
commit
b390a57e2f
76
main.cpp
76
main.cpp
@ -4,39 +4,43 @@
|
||||
#include "restaurants/restaurants.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <restbed>
|
||||
#include <sstream>
|
||||
|
||||
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 ) {
|
||||
for(auto &x : restaurants)
|
||||
x->parse();
|
||||
for(auto &restaurant : restaurants)
|
||||
restaurant.second->parse();
|
||||
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 ) {
|
||||
std::stringstream ss{};
|
||||
bool atleastonerest = false;
|
||||
ss << "[";
|
||||
for(auto &x : restaurants) {
|
||||
for(auto &restaurant : restaurants) {
|
||||
atleastonerest = true;
|
||||
ss << x->jsonify() << ",";
|
||||
ss << restaurant.second->jsonify() << ",";
|
||||
}
|
||||
if(atleastonerest)
|
||||
ss.seekp(-1, ss.cur);
|
||||
ss << "]";
|
||||
auto res = ss.str();
|
||||
session->close(restbed::OK, res, { { "Content-Length", std::to_string(res.length()) }, { "Access-Control-Allow-Origin", "*" } });
|
||||
sendResponse(ss.str(), restbed::OK, session);
|
||||
}
|
||||
|
||||
void get( const std::shared_ptr< restbed::Session > session ) {
|
||||
// TODO better
|
||||
int day = -1;
|
||||
int restaurant = -1;
|
||||
std::string restaurant = "";
|
||||
auto request = session->get_request();
|
||||
for(const auto &query_param : request->get_query_parameters()) {
|
||||
if(query_param.first == "day") {
|
||||
@ -48,59 +52,55 @@ void get( const std::shared_ptr< restbed::Session > session ) {
|
||||
day = 2;
|
||||
} else if(query_param.second == "thursday") {
|
||||
day = 3;
|
||||
} else {
|
||||
} else if(query_param.second == "friday") {
|
||||
day = 4;
|
||||
}
|
||||
}
|
||||
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 if(query_param.second == "saturday") {
|
||||
day = 5;
|
||||
} else {
|
||||
restaurant = 4;
|
||||
day = 6;
|
||||
}
|
||||
}
|
||||
if(query_param.first == "restaurant")
|
||||
restaurant = query_param.second;
|
||||
}
|
||||
if(day == -1 && restaurant == -1) {
|
||||
std::string error = "DIDN'T SPECIFY DAY OR RESTAURANT";
|
||||
session->close(restbed::OK, error, { { "Content-Length", std::to_string(error.length()) }, { "Access-Control-Allow-Origin", "*" } });
|
||||
if(day == -1 && restaurant == "") {
|
||||
sendResponse("DIDN'T SPECIFY DAY OR RESTAURANT", restbed::BAD_REQUEST, session);
|
||||
return;
|
||||
}
|
||||
|
||||
if(restaurant != "" && restaurants.find(restaurant) == restaurants.end()) {
|
||||
sendResponse("YOU DIDN'T SPECIFY A VALID RESTAURANT!", restbed::BAD_REQUEST, session);
|
||||
return;
|
||||
}
|
||||
|
||||
std::stringstream ss{};
|
||||
if(day != -1 && restaurant != -1) {
|
||||
if(day != -1 && restaurant != "") {
|
||||
ss << "[" << restaurants[restaurant]->jsonify({day}) << "]";
|
||||
} else if(day != -1) {
|
||||
ss << "[";
|
||||
bool atleastone = false;
|
||||
for(auto &x : restaurants) {
|
||||
for(auto &restaurant : restaurants) {
|
||||
atleastone = true;
|
||||
ss << x->jsonify({day}) << ",";
|
||||
ss << restaurant.second->jsonify({day}) << ",";
|
||||
}
|
||||
if(atleastone)
|
||||
ss.seekp(-1, ss.cur);
|
||||
ss << "]";
|
||||
} else if(restaurant != -1) {
|
||||
} else if(restaurant != "") {
|
||||
ss << "[" << restaurants[restaurant]->jsonify() << "]";
|
||||
}
|
||||
auto res = ss.str();
|
||||
session->close(restbed::OK, res, { { "Content-Length", std::to_string(res.length()) }, { "Access-Control-Allow-Origin", "*" } });
|
||||
sendResponse(ss.str(), restbed::OK, session);
|
||||
}
|
||||
|
||||
int main() {
|
||||
restaurants.emplace_back(new LunchRest::UDrevakaRestaurant());
|
||||
restaurants.emplace_back(new LunchRest::PadagaliRestaurant());
|
||||
restaurants.emplace_back(new LunchRest::LightOfIndiaRestaurant());
|
||||
restaurants.emplace_back(new LunchRest::UKarlaRestaurant());
|
||||
restaurants.emplace_back(new LunchRest::AlCaponeRestaurant());
|
||||
restaurants["udrevaka"] = std::make_unique<LunchRest::UDrevakaRestaurant>();
|
||||
restaurants["padagali"] = std::make_unique<LunchRest::PadagaliRestaurant>();
|
||||
restaurants["lightofindia"] = std::make_unique<LunchRest::LightOfIndiaRestaurant>();
|
||||
restaurants["ukarla"] = std::make_unique<LunchRest::UKarlaRestaurant>();
|
||||
restaurants["alcapone"] = std::make_unique<LunchRest::AlCaponeRestaurant>();
|
||||
std::cout << "Initial parsing" << std::endl;
|
||||
for(auto &x : restaurants)
|
||||
x->parse();
|
||||
for(auto &restaurant : restaurants)
|
||||
restaurant.second->parse();
|
||||
std::cout << "Finished parsing" << std::endl;
|
||||
|
||||
restbed::Service service;
|
||||
|
Loading…
Reference in New Issue
Block a user