summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cardTypeHandlerInfo.cpp41
-rw-r--r--src/postgresHandler.cpp15
2 files changed, 45 insertions, 11 deletions
diff --git a/src/cardTypeHandlerInfo.cpp b/src/cardTypeHandlerInfo.cpp
new file mode 100644
index 0000000..b49057a
--- /dev/null
+++ b/src/cardTypeHandlerInfo.cpp
@@ -0,0 +1,41 @@
+#include "cardTypeHandlerInfo.hpp"
+
+void CardTypeHandlerInfo::addCardInfo(
+ const std::string &kCardType, const std::string &kCardValueRegex,
+ const std::function<const std::string(const std::string &)>
+ kJSONToSQLConverter) {
+ // TODO: complete
+}
+
+const CardTypeHandlerInfo cardTypeHandlerInfoList() {
+ CardTypeHandlerInfo aCardTypeHandlerInfo;
+
+ aCardTypeHandlerInfo.addCardInfo(
+ "INT", "[0-9]+",
+ [](const std::string &kJSONContent) { return kJSONContent; });
+
+ aCardTypeHandlerInfo.addCardInfo(
+ "BOOL", "true|false",
+ [](const std::string &kJSONContent) { return kJSONContent; });
+
+ aCardTypeHandlerInfo.addCardInfo(
+ "STRING", "\"[^\"?]*\"", [](const std::string &kJSONContent) {
+ std::string aCopyOfJSONValue = kJSONContent;
+ *aCopyOfJSONValue.begin() = '\'';
+ *std::prev(aCopyOfJSONValue.end()) = '\'';
+ return aCopyOfJSONValue;
+ });
+
+ aCardTypeHandlerInfo.addCardInfo(
+ "LIST_OF_INT", "\\[[0-9,]*\\]", [](const std::string &kJSONContent) {
+ std::string aCopyOfJSONValue = kJSONContent;
+ aCopyOfJSONValue.append("\'");
+ aCopyOfJSONValue.insert(aCopyOfJSONValue.begin(), '\'');
+ return aCopyOfJSONValue;
+ });
+
+ aCardTypeHandlerInfo.addCardInfo(
+ "NULL", "null", [](const std::string &kJSONContent) { return "NULL"; });
+
+ return aCardTypeHandlerInfo;
+}
diff --git a/src/postgresHandler.cpp b/src/postgresHandler.cpp
index 9241214..4574f95 100644
--- a/src/postgresHandler.cpp
+++ b/src/postgresHandler.cpp
@@ -1,4 +1,5 @@
#include "postgresHandler.hpp"
+#include "cardTypeHandlerInfo.hpp"
#include "hearthstoneAPIHandler.hpp"
#include <cctype>
#include <cstddef>
@@ -10,14 +11,6 @@
#define KEY_ID 1
-// TODO: create a list/array which contains CardTypeHandlerInfo struct objects
-// and fill these info in a single function
-struct CardTypeHandlerInfo {
- std::string aCardType;
- std::string aCardValueRegex;
- std::function<const std::string(const std::string &)> aJSONToSQLConverter;
-};
-
const std::map<std::string, std::string> aCardValueTypeToRegex = {
{"INT", "[0-9]+"},
{"STRING", "\"[^\"?]*\""},
@@ -131,7 +124,7 @@ add_single_card_to_database(const std::string &kCards, pqxx::work &aTransaction,
}
}
-const std::regex keyValueJSONRegex() {
+const std::regex regexKeyValueInJSON() {
std::string aRegexString = "\"([^\"?]+)\":(";
for (auto &[fieldName, regex] : aCardValueTypeToRegex) {
aRegexString += "(" + regex + ")|";
@@ -155,7 +148,7 @@ endPosOfCardJSON(const std::string &kCardsPageJSON,
void addCardsToDatabase(HearthstoneAPIHandler &aAPIIntermediate,
pqxx::work &aTransaction,
const std::string &kURLOfCardsPage) {
- const std::regex aKeyValueJSONRegex = keyValueJSONRegex();
+ const std::regex aRegexKeyValueInJSON = regexKeyValueInJSON();
const std::string &kCardsPageJSON =
aAPIIntermediate.get_cards(kURLOfCardsPage);
std::string::size_type aCardStartPos = startPosOfCardJSON(kCardsPageJSON);
@@ -163,7 +156,7 @@ void addCardsToDatabase(HearthstoneAPIHandler &aAPIIntermediate,
while (aCardStartPos != std::string::npos &&
aCardEndPos != std::string::npos) {
add_single_card_to_database(kCardsPageJSON, aTransaction,
- aKeyValueJSONRegex, aCardStartPos,
+ aRegexKeyValueInJSON, aCardStartPos,
aCardEndPos);
aCardStartPos = startPosOfCardJSON(kCardsPageJSON, aCardStartPos + 1);
aCardEndPos = endPosOfCardJSON(kCardsPageJSON, aCardEndPos + 1);