merchantstore_controller.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "merchantstore_controller.hpp"
  4. #include <string>
  5. #include <nlohmann/json.hpp>
  6. #include <common/showmsg.hpp>
  7. #include <common/sql.hpp>
  8. #include "auth.hpp"
  9. #include "http.hpp"
  10. #include "sqllock.hpp"
  11. #include "webutils.hpp"
  12. #include "web.hpp"
  13. HANDLER_FUNC(merchantstore_save) {
  14. if (!isAuthorized(req, false)) {
  15. res.status = HTTP_BAD_REQUEST;
  16. res.set_content("Error", "text/plain");
  17. return;
  18. }
  19. auto account_id = std::stoi(req.get_file_value("AID").content);
  20. auto char_id = std::stoi(req.get_file_value("GID").content);
  21. auto world_name_str = req.get_file_value("WorldName").content;
  22. auto world_name = world_name_str.c_str();
  23. auto store_type = std::stoi(req.get_file_value("Type").content);
  24. std::string data;
  25. if (req.has_file("data")) {
  26. data = req.get_file_value("data").content;
  27. }
  28. SQLLock sl(WEB_SQL_LOCK);
  29. sl.lock();
  30. auto handle = sl.getHandle();
  31. SqlStmt* stmt = SqlStmt_Malloc(handle);
  32. if (SQL_SUCCESS != SqlStmt_Prepare(stmt,
  33. "SELECT `account_id` FROM `%s` WHERE (`account_id` = ? AND `char_id` = ? AND `world_name` = ? AND `store_type` = ?) LIMIT 1",
  34. merchant_configs_table)
  35. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_INT, &account_id, sizeof(account_id))
  36. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 1, SQLDT_INT, &char_id, sizeof(char_id))
  37. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 2, SQLDT_STRING, (void *)world_name, strlen(world_name))
  38. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 3, SQLDT_INT, &store_type, sizeof(store_type))
  39. || SQL_SUCCESS != SqlStmt_Execute(stmt)
  40. ) {
  41. SqlStmt_ShowDebug(stmt);
  42. SqlStmt_Free(stmt);
  43. sl.unlock();
  44. res.status = HTTP_BAD_REQUEST;
  45. res.set_content("Error", "text/plain");
  46. return;
  47. }
  48. if (SqlStmt_NumRows(stmt) <= 0) {
  49. if (SQL_SUCCESS != SqlStmt_Prepare(stmt,
  50. "INSERT INTO `%s` (`account_id`, `char_id`, `world_name`, `store_type`, `data`) VALUES (?, ?, ?, ?, ?)",
  51. merchant_configs_table)
  52. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_INT, &account_id, sizeof(account_id))
  53. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 1, SQLDT_INT, &char_id, sizeof(char_id))
  54. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 2, SQLDT_STRING, (void *)world_name, strlen(world_name))
  55. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 3, SQLDT_INT, &store_type, sizeof(store_type))
  56. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 4, SQLDT_STRING, (void *)data.c_str(), strlen(data.c_str()))
  57. || SQL_SUCCESS != SqlStmt_Execute(stmt)
  58. ) {
  59. SqlStmt_ShowDebug(stmt);
  60. SqlStmt_Free(stmt);
  61. sl.unlock();
  62. res.status = HTTP_BAD_REQUEST;
  63. res.set_content("Error", "text/plain");
  64. return;
  65. }
  66. }
  67. else {
  68. if (SQL_SUCCESS != SqlStmt_Prepare(stmt,
  69. "UPDATE `%s` SET `data` = ? WHERE (`account_id` = ? AND `char_id` = ? AND `world_name` = ? AND `store_type` = ?)",
  70. merchant_configs_table)
  71. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_STRING, (void *)data.c_str(), strlen(data.c_str()))
  72. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 1, SQLDT_INT, &account_id, sizeof(account_id))
  73. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 2, SQLDT_INT, &char_id, sizeof(char_id))
  74. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 3, SQLDT_STRING, (void *)world_name, strlen(world_name))
  75. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 4, SQLDT_INT, &store_type, sizeof(store_type))
  76. || SQL_SUCCESS != SqlStmt_Execute(stmt)
  77. ) {
  78. SqlStmt_ShowDebug(stmt);
  79. SqlStmt_Free(stmt);
  80. sl.unlock();
  81. res.status = HTTP_BAD_REQUEST;
  82. res.set_content("Error", "text/plain");
  83. return;
  84. }
  85. }
  86. SqlStmt_Free(stmt);
  87. sl.unlock();
  88. res.set_content(data, "application/json");
  89. }
  90. HANDLER_FUNC(merchantstore_load) {
  91. if (!req.has_file("AID") || !req.has_file("WorldName")) {
  92. res.status = HTTP_BAD_REQUEST;
  93. res.set_content("Error", "text/plain");
  94. return;
  95. }
  96. // TODO: Figure out when client sends AuthToken for this path, then add packetver check
  97. // if (!isAuthorized(req)) {
  98. // ShowError("Not authorized!\n");
  99. // message.reply(web::http::status_codes::Forbidden);
  100. // return;
  101. // }
  102. auto account_id = std::stoi(req.get_file_value("AID").content);
  103. auto char_id = std::stoi(req.get_file_value("GID").content);
  104. auto world_name_str = req.get_file_value("WorldName").content;
  105. auto world_name = world_name_str.c_str();
  106. auto store_type = std::stoi(req.get_file_value("Type").content);
  107. SQLLock sl(WEB_SQL_LOCK);
  108. sl.lock();
  109. auto handle = sl.getHandle();
  110. SqlStmt* stmt = SqlStmt_Malloc(handle);
  111. if (SQL_SUCCESS != SqlStmt_Prepare(stmt,
  112. "SELECT `data` FROM `%s` WHERE (`account_id` = ? AND `char_id` = ? AND `world_name` = ? AND `store_type` = ?) LIMIT 1",
  113. merchant_configs_table)
  114. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_INT, &account_id, sizeof(account_id))
  115. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 1, SQLDT_INT, &char_id, sizeof(char_id))
  116. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 2, SQLDT_STRING, (void *)world_name, strlen(world_name))
  117. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 3, SQLDT_INT, &store_type, sizeof(store_type))
  118. || SQL_SUCCESS != SqlStmt_Execute(stmt)
  119. ) {
  120. SqlStmt_ShowDebug(stmt);
  121. SqlStmt_Free(stmt);
  122. sl.unlock();
  123. res.status = HTTP_BAD_REQUEST;
  124. res.set_content("Error", "text/plain");
  125. return;
  126. }
  127. if (SqlStmt_NumRows(stmt) <= 0) {
  128. SqlStmt_Free(stmt);
  129. ShowDebug("[AccountID: %d, World: \"%s\"] Not found in table, sending new info.\n", account_id, world_name);
  130. sl.unlock();
  131. res.set_content("{\"Type\": 1}", "application/json");
  132. return;
  133. }
  134. char databuf[SQL_BUFFER_SIZE] = { 0 };
  135. if (SQL_SUCCESS != SqlStmt_BindColumn(stmt, 0, SQLDT_STRING, &databuf, sizeof(databuf), nullptr, nullptr)
  136. || SQL_SUCCESS != SqlStmt_NextRow(stmt)
  137. ) {
  138. SqlStmt_ShowDebug(stmt);
  139. SqlStmt_Free(stmt);
  140. sl.unlock();
  141. res.status = HTTP_BAD_REQUEST;
  142. res.set_content("Error", "text/plain");
  143. return;
  144. }
  145. SqlStmt_Free(stmt);
  146. sl.unlock();
  147. databuf[sizeof(databuf) - 1] = 0;
  148. auto response = nlohmann::json::parse(databuf);
  149. response["Type"] = 1;
  150. res.set_content(response.dump(), "application/json");
  151. }