전
json handle_cart_add(const json& data) {
json response;
// 1. 필수 필드 검사 (CART_INFO용)
if (!data.contains("USER_ID") || !data.contains("MENU_ID") || !data.contains("QUANTITY") ||
!data.contains("TOTAL_PRICE"))
{
response["role"] = "server";
response["action"] = "cart_add";
response["status"] = "error";
response["message"] = "필수 데이터 누락 (USER_ID, MENU_ID, QUANTITY, TOTAL_PRICE)";
return response;
}
int user_id = data["USER_ID"];
int menu_id = data["MENU_ID"];
int quantity = data["QUANTITY"];
int total_price = data["TOTAL_PRICE"];
// BIG_OPT_ID는 옵션 상세에 있으므로 기본 0으로 세팅
int big_opt_id = 0;
// DB 연결
MYSQL* conn = connect_db();
if (!conn) {
response["role"] = "server";
response["action"] = "cart_add";
response["status"] = "error";
response["message"] = "DB 연결 실패";
return response;
}
// CART_INFO에 INSERT, STATUS는 항상 1 (활성)
std::string query = "INSERT INTO CART_INFO (USER_ID, MENU_ID, BIG_OPT_ID, QUANTITY, TOTAL_PRICE, STATUS) VALUES (" +
std::to_string(user_id) + ", " +
std::to_string(menu_id) + ", " +
std::to_string(big_opt_id) + ", " +
std::to_string(quantity) + ", " +
std::to_string(total_price) + ", 1)";
if (mysql_query(conn, query.c_str()) != 0) {
response["role"] = "server";
response["action"] = "cart_add";
response["status"] = "error";
response["message"] = "CART_INFO INSERT 실패: " + std::string(mysql_error(conn));
mysql_close(conn);
return response;
}
// 생성된 CART_ID 가져오기
unsigned long long cart_id = mysql_insert_id(conn);
// 옵션 정보 INSERT (단일 옵션)
if (data.contains("BIG_OPT_ID") && data.contains("BIG_OPT_NAME") &&
data.contains("SMALL_OPT_ID") && data.contains("SMALL_OPT_NAME") && data.contains("SMALL_OPT_PRICE"))
{
int opt_big_opt_id = data["BIG_OPT_ID"];
std::string opt_big_opt_name = data["BIG_OPT_NAME"];
int opt_small_opt_id = data["SMALL_OPT_ID"];
std::string opt_small_opt_name = data["SMALL_OPT_NAME"];
int opt_small_opt_price = data["SMALL_OPT_PRICE"];
std::string detail_query = "INSERT INTO CART_DETAIL_INFO (CART_ID, BIG_OPT_NAME, BIG_OPT_ID, SMALL_OPT_ID, SMALL_OPT_NAME, SMALL_OPT_PRICE) VALUES (" +
std::to_string(cart_id) + ", '" +
opt_big_opt_name + "', " +
std::to_string(opt_big_opt_id) + ", " +
std::to_string(opt_small_opt_id) + ", '" +
opt_small_opt_name + "', " +
std::to_string(opt_small_opt_price) + ")";
if (mysql_query(conn, detail_query.c_str()) != 0) {
response["role"] = "server";
response["action"] = "cart_add";
response["status"] = "error";
response["message"] = "CART_DETAIL_INFO INSERT 실패: " + std::string(mysql_error(conn));
mysql_close(conn);
return response;
}
}
// DB 연결 종료 및 성공 응답 반환
mysql_close(conn);
response["role"] = "server";
response["action"] = "cart_add";
response["status"] = "success";
response["message"] = "장바구니 추가 성공";
response["data"] = { {"CART_ID", cart_id} };
return response;
}
후
json handle_cart_add(const json& data) {
json response;
if (!data.contains("USER_ID") || !data.contains("MENU_ID") || !data.contains("QUANTITY") ||
!data.contains("TOTAL_PRICE"))
{
response["role"] = "server";
response["action"] = "cart_add";
response["status"] = "error";
response["message"] = "필수 데이터 누락 (USER_ID, MENU_ID, QUANTITY, TOTAL_PRICE)";
return response;
}
int user_id = data["USER_ID"];
int menu_id = data["MENU_ID"];
int quantity = data["QUANTITY"];
int total_price = data["TOTAL_PRICE"];
int big_opt_id = 0;
MYSQL* conn = connect_db();
if (!conn) {
response["role"] = "server";
response["action"] = "cart_add";
response["status"] = "error";
response["message"] = "DB 연결 실패";
return response;
}
std::string query = "INSERT INTO CART_INFO (USER_ID, MENU_ID, BIG_OPT_ID, QUANTITY, TOTAL_PRICE, STATUS) VALUES (" +
std::to_string(user_id) + ", " +
std::to_string(menu_id) + ", " +
std::to_string(big_opt_id) + ", " +
std::to_string(quantity) + ", " +
std::to_string(total_price) + ", 1)";
if (mysql_query(conn, query.c_str()) != 0) {
response["role"] = "server";
response["action"] = "cart_add";
response["status"] = "error";
response["message"] = "CART_INFO INSERT 실패: " + std::string(mysql_error(conn));
mysql_close(conn);
return response;
}
unsigned long long cart_id = mysql_insert_id(conn);
if (data.contains("BIG_OPT_ID") && data.contains("BIG_OPT_NAME") &&
data.contains("SMALL_OPT_ID") && data.contains("SMALL_OPT_NAME") && data.contains("SMALL_OPT_PRICE"))
{
int opt_big_opt_id = data["BIG_OPT_ID"];
std::string opt_big_opt_name = data["BIG_OPT_NAME"];
int opt_small_opt_id = data["SMALL_OPT_ID"];
std::string opt_small_opt_name = data["SMALL_OPT_NAME"];
int opt_small_opt_price = data["SMALL_OPT_PRICE"];
std::string detail_query = "INSERT INTO CART_DETAIL_INFO (CART_ID, BIG_OPT_NAME, BIG_OPT_ID, SMALL_OPT_ID, SMALL_OPT_NAME, SMALL_OPT_PRICE) VALUES (" +
std::to_string(cart_id) + ", '" +
opt_big_opt_name + "', " +
std::to_string(opt_big_opt_id) + ", " +
std::to_string(opt_small_opt_id) + ", '" +
opt_small_opt_name + "', " +
std::to_string(opt_small_opt_price) + ")";
if (mysql_query(conn, detail_query.c_str()) != 0) {
response["role"] = "server";
response["action"] = "cart_add";
response["status"] = "error";
response["message"] = "CART_DETAIL_INFO INSERT 실패: " + std::string(mysql_error(conn));
mysql_close(conn);
return response;
}
}
mysql_close(conn);
// 클라이언트 파싱 편의를 위해 소문자 키 이름으로 응답
response["role"] = "server";
response["action"] = "cart_add";
response["status"] = "success";
response["message"] = "장바구니 추가 성공";
response["data"] = { {"cart_id", cart_id} };
return response;
}