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); }
int init_module(void) { START_TESTS("Session"); INIT_CALL_END(init(), simple_session(), end(), "Single Session"); INIT_CALL_END(init(), test_address_filtering(), end(), "Address-dependent filtering."); INIT_CALL_END(init(), test_compare_session4(), end(), "compare_session4()"); INIT_CALL_END(init(), test_tcp_v4_init_state_handle_v6syn(), end(), "TCP-V4 INIT-V6 syn"); INIT_CALL_END(init(), test_tcp_v4_init_state_handle_else(), end(), "TCP-V4 INIT-else"); INIT_CALL_END(init(), test_tcp_v6_init_state_handle_v6syn(), end(), "TCP-V6 INIT-V6 SYN"); INIT_CALL_END(init(), test_tcp_v6_init_state_handle_v4syn(), end(), "TCP-V6 INIT-V4 SYN"); INIT_CALL_END(init(), test_tcp_v6_init_state_handle_else(), end(), "TCP-V6 INIT-else"); INIT_CALL_END(init(), test_tcp_established_state_handle_v4fin(), end(), "TCP-established-V4 fin"); INIT_CALL_END(init(), test_tcp_established_state_handle_v6fin(), end(), "TCP-established-V6 fin"); INIT_CALL_END(init(), test_tcp_established_state_handle_v4rst(), end(), "TCP-established-V4 rst"); INIT_CALL_END(init(), test_tcp_established_state_handle_v6rst(), end(), "TCP-established-V6 rst"); INIT_CALL_END(init(), test_tcp_established_state_handle_else(), end(), "TCP-established-else"); INIT_CALL_END(init(), test_tcp_v4_fin_rcv_state_handle_v6fin(), end(), "TCP-V4 FIN RCV-V6 fin"); INIT_CALL_END(init(), test_tcp_v4_fin_rcv_state_handle_else(), end(), "TCP-V4 FIN RCV-else"); INIT_CALL_END(init(), test_tcp_v6_fin_rcv_state_handle_v4fin(), end(), "TCP-V6 FIN RCV-v4fin"); INIT_CALL_END(init(), test_tcp_v6_fin_rcv_state_handle_else(), end(), "TCP-V6 FIN RCV-else"); INIT_CALL_END(init(), test_tcp_trans_state_handle_v6rst(), end(), "TCP-TRANS-V6 rst"); INIT_CALL_END(init(), test_tcp_trans_state_handle_v4rst(), end(), "TCP-TRANS-V4 rst"); INIT_CALL_END(init(), test_tcp_trans_state_handle_else(), end(), "TCP-TRANS-else"); END_TESTS; }
int init_module(void) { START_TESTS("Session"); INIT_CALL_END(init(), simple_session(), end(), "Single Session"); END_TESTS; }
int init_module(void) { START_TESTS("BIB-Session"); INIT_CALL_END(init(), simple_bib(), end(), "Single BIB"); INIT_CALL_END(init(), simple_session(), end(), "Single Session"); INIT_CALL_END(init(), test_clean_old_sessions(), end(), "Session cleansing."); INIT_CALL_END(init(), test_address_filtering(), end(), "Address-dependent filtering."); INIT_CALL_END(init(), test_for_each(), end(), "for-each function."); END_TESTS; }
void CartController::handle_put(web::http::http_request request) { web::http::http_response response; granada::http::session::MapSession simple_session(request,response); business::Cart cart(&simple_session); std::string product_id = ""; int quantity = 0; std::string body = request.extract_utf8string(true).get(); std::unordered_map<std::string,std::string> parsed_query = granada::http::parser::ParseQueryString(body); auto it = parsed_query.find("id"); if (it != parsed_query.end()){ product_id.assign(it->second); } auto it2 = parsed_query.find("quantity"); if (it2 != parsed_query.end()){ quantity = std::atoi(it2->second.c_str()); } int count = 0; if (product_id.empty()){ // invalid product id, do not add the product, // just return the number of products already added. count = cart.Count(); }else{ // adds a product to the cart. count = cart.Add(product_id,quantity); } response.set_status_code(status_codes::OK); response.headers().add(U("Content-Type"), U("text/json; charset=utf-8")); response.set_body("{\"count\":\"" + std::to_string(count) + "\"}"); request.reply(response); }