Ejemplo n.º 1
0
      void CartController::handle_get(web::http::http_request request)
      {

        web::http::http_response response;

        granada::http::session::MapSession simple_session(request,response);
        business::Cart cart(&simple_session);

        auto paths = uri::split_path(uri::decode(request.relative_uri().path()));
        if (!paths.empty()){
          std::string name = utility::conversions::to_utf8string(paths[0]);

          if(name == "count"){

            // returns the number of products added to the cart.
            int count = cart.Count();
            response.set_body("{\"count\":\"" + std::to_string(count) + "\"}");

          }else if (name == "list"){

            // returns a list with the products added to the cart but only
            // if the user is logged in.
            if (simple_session.roles()->Is("USER")){
              response.set_body("{\"data\":" + cart.List() + "}");
            }else{
              response.set_body("{\"data\":[]}");
            }
          }
        }

        response.set_status_code(status_codes::OK);
        response.headers().add(U("Content-Type"), U("text/json; charset=utf-8"));

        request.reply(response);
      }
Ejemplo n.º 2
0
void handle_add(web::http::http_request& request) {

	json::value response;
	std::map<std::string, std::string> querymap = uri::split_query(
			request.relative_uri().query());
	if (querymap.find("name") == querymap.end()
			|| querymap.find("command") == querymap.end()) {
		LOG(error)<<"Bad request";
		response["error_msg"] = json::value::string("Bad request. Read doc for info. Missing name or command field in request.");
		response["status"] = json::value::string("ERROR");
		request.reply(status_codes::BadRequest, response).wait();
		return;

	}
	std::string commandName = uri::decode(querymap["name"]);
	std::string commandString = uri::decode(querymap["command"]);
	LOG(debug)<<"Add command name "<<commandName<<" command string: "<<commandString;
	concurrency::streams::istream body = request.body();
	uint64_t content_lenght = request.headers().content_length();
	LOG(debug)<<"Content lenght of request "<<content_lenght;
	if (content_lenght == 0) {
		LOG(error)<<"Bad request! Empty body";
		response["error_msg"] = json::value::string("Bad request.Empty body!");
		response["status"] = json::value::string("ERROR");
		request.reply(status_codes::BadRequest, response).wait();
		return;

	}
	try {
		uint8_t * waveData = new uint8_t[content_lenght];
		Concurrency::streams::rawptr_buffer<uint8_t> buffer(waveData,
				content_lenght);
		//body.read_to_end(buffer).get();
		body.read(buffer, content_lenght).get();
		WaveFile wave(waveData, content_lenght);
		delete[] waveData;
		Samples waveSamples(wave);
		jsonextend wavepropertiesJSON = g_processAndAnlyze.getSummary(waveSamples);
		wavepropertiesJSON["name"] =  web::json::value::string(commandName);

		try {
			LOG(debug)<<"Saving "<<wavepropertiesJSON.to_string();
			g_mainDB->put(wavepropertiesJSON, commandString);
		} catch (std::exception const & ex) {
			LOG(error)<<"Error "<<ex.what();
			response["status"] = json::value::string("ERROR");
			response["error_msg"] = json::value::string(ex.what());
			request.reply(status_codes::InternalError, response).wait();
			return;
		}
		response["status"] = json::value::string("OK");
		response["command"] = json::value::string(commandString);
		request.reply(status_codes::OK, response).wait();
	} catch (std::exception &e) {

		LOG(error)<<"Error "<<e.what();
		response["status"] = json::value::string("ERROR");
		response["command"] = json::value::string(e.what());
		request.reply(status_codes::BadRequest, response).wait();
	}

}