Skip to content

Commit

Permalink
[Add]: Database for language-config command
Browse files Browse the repository at this point in the history
  • Loading branch information
harshfeudal committed Sep 21, 2023
1 parent 606ea9e commit 07cb989
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"xstddef": "cpp",
"xtr1common": "cpp",
"xtree": "cpp",
"xutility": "cpp"
"xutility": "cpp",
"*.rh": "cpp"
}
}
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ project(

aux_source_directory(src MAINSRC)
aux_source_directory(src/cores CORES)
aux_source_directory(src/database DATABASE)

aux_source_directory(src/commands COMMANDS)
aux_source_directory(src/commands/slash/cmd_common COMMON_CMDS)
Expand All @@ -45,6 +46,7 @@ add_executable(${PROJECT_NAME}
${COMMON_CMDS}
${MODERATION_CMDS}
${CORES}
${DATABASE}
)

set_target_properties(${PROJECT_NAME} PROPERTIES
Expand Down
2 changes: 2 additions & 0 deletions premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ project "Harshie"
"src/commands/slash/cmd_common/**.cpp",
"src/cores/**.h",
"src/cores/**.cpp",
"src/database/**.h",
"src/database/**.cpp",
"resources/*.h",
"resources/*.rc",
}
Expand Down
43 changes: 43 additions & 0 deletions src/commands/slash/cmd_common/config-language.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (C) 2023 harshfeudal
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/

#include "../cmd_lists.h"

void config_language(dpp::cluster& client, const dpp::slashcommand_t& event)
{
const auto selectLanguage = std::get<std::string>(event.get_parameter("language"));
HarshieDatabase& database = HarshieDatabase::getInstance();

auto recordUser = fmt::format("'{}'", event.command.usr.id);
auto searchUser = database.findRecord("language_config", "id=" + recordUser);

std::string setLanguage = "en-us";

if (selectLanguage == "english")
{
if (searchUser)
database.deleteData("language_config", "id=" + searchUser);
}
else if (selectLanguage == "japanese")
{
if (searchUser)
database.deleteData("language_config", "id=" + searchUser);

setLanguage = "ja-jp";
database.insertData("language_config", searchUser + ", '" + setLanguage + "'");
}
}
11 changes: 11 additions & 0 deletions src/commands/slash/cmd_layout.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,15 @@ inline std::map<std::string, HarshieCmndConstructor> commands
"Check Harshie latency", ping
}
},
{
"config-language",
{
"Language configuration", config_language,
{
dpp::command_option(dpp::co_string, "language", "Your prefer language", true)
.add_choice(dpp::command_option_choice("English", std::string("english")))
.add_choice(dpp::command_option_choice("日本語", std::string("japanese")))
}
}
}
};
3 changes: 3 additions & 0 deletions src/commands/slash/cmd_lists.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@
#include <dpp/dpp.h>
#include <spdlog/spdlog.h>

#include "../../database/database.h"

// Common commands
void ping(dpp::cluster& client, const dpp::slashcommand_t& event);
void config_language(dpp::cluster& client, const dpp::slashcommand_t& event);
16 changes: 12 additions & 4 deletions src/cores/harshie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ void Harshie::HarshieStart()

HarshieOnReady();
HarshieOnSlashCmnd();

HarshieOnDatabaseConnect();
HarshieOnDatabaseCreate();

client->start(dpp::st_wait);
}
Expand All @@ -51,7 +53,7 @@ void Harshie::HarshieOnReady()
void Harshie::HarshieActivites(const dpp::ready_t& event)
{
client->set_presence(dpp::presence(dpp::ps_dnd, dpp::at_game, "Looking for moderation"));
fmt::print("[{}]: {} is online!\n", dpp::utility::current_date_time(), client->me.format_username());
fmt::print("[{}] {} is online!\n", dpp::utility::current_date_time(), client->me.format_username());
}

void Harshie::HarshieRegisterSlashCmnd()
Expand All @@ -61,14 +63,14 @@ void Harshie::HarshieRegisterSlashCmnd()
registerSlashCommand(*client);

fmt::print(
"[{}]: Successfully registered application (/) commands!\n",
"[{}] Successfully registered application (/) commands!\n",
dpp::utility::current_date_time()
);
}
catch (...)
{
fmt::print(
"[{}]: Fail to register application (/) commands!\n",
"[{}] Fail to register application (/) commands!\n",
dpp::utility::current_date_time()
);
}
Expand Down Expand Up @@ -100,5 +102,11 @@ void Harshie::HarshieOnDatabaseConnect()
databaseName, databaseHost, databasePort, databaseUser, databasePass, databaseAppName
);

HarshieDatabase database(databaseConnectStr);
HarshieDatabase& database = HarshieDatabase::getInstance();
database.connectDatabase(databaseConnectStr);
}

void Harshie::HarshieOnDatabaseCreate()
{

}
3 changes: 2 additions & 1 deletion src/cores/harshie.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

#include "dotenv.h"
#include "decoder.h"
#include "database.h"
#include "../database/database.h"

class Harshie {
public:
Expand All @@ -44,6 +44,7 @@ class Harshie {
void HarshieOnReady();
void HarshieOnSlashCmnd();
void HarshieOnDatabaseConnect();
void HarshieOnDatabaseCreate();

void HarshieActivites(const dpp::ready_t& event);
void HarshieRegisterSlashCmnd();
Expand Down
35 changes: 21 additions & 14 deletions src/cores/database.cpp → src/database/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,21 @@

#include "database.h"

HarshieDatabase::HarshieDatabase(const std::string& connectionString)
HarshieDatabase::HarshieDatabase() { }

void HarshieDatabase::connectDatabase(const std::string& connectionString)
{
connection = PQconnectdb(connectionString.c_str());

if (PQstatus(connection) != CONNECTION_OK)
{
const auto errorLog = fmt::format("[{}]: Failed to connect to the database!", dpp::utility::current_date_time());
const auto errorLog = fmt::format("[{}] Failed to connect to the database!\n", dpp::utility::current_date_time());
fmt::print(errorLog);

throw std::runtime_error("Connection to the database failed: " + std::string(PQerrorMessage(connection)));
}
else
fmt::print("[{}]: Connected to database!", dpp::utility::current_date_time());
fmt::print("[{}] Connected to database!\n", dpp::utility::current_date_time());
}

HarshieDatabase::~HarshieDatabase()
Expand All @@ -45,7 +47,7 @@ HarshieDatabase::~HarshieDatabase()

bool HarshieDatabase::createTable(const std::string& tableName, const std::string& columns)
{
std::string query = "CREATE TABLE IF NOT EXISTS " + tableName + " (" + columns + ");";
std::string query = "CREATE TABLE IF NOT EXISTS \"" + tableName + "\" (" + columns + ");";
PGresult* result = PQexec(connection, query.c_str());
ExecStatusType status = PQresultStatus(result);
PQclear(result);
Expand Down Expand Up @@ -73,22 +75,21 @@ bool HarshieDatabase::deleteData(const std::string& tableName, const std::string
return status == PGRES_COMMAND_OK;
}

int HarshieDatabase::getCount(const std::string& tableName, const std::string& searchCondition)
bool HarshieDatabase::findRecord(const std::string& tableName, const std::string& searchCondition)
{
std::string query = "SELECT COUNT(*) FROM " + tableName + " WHERE " + searchCondition + ";";
std::string query = "SELECT * FROM " + tableName + " WHERE " + searchCondition + ";";
PGresult* result = PQexec(connection, query.c_str());

if (PQresultStatus(result) == PGRES_TUPLES_OK)
{
int count = atoi(PQgetvalue(result, 0, 0));
PQclear(result);
return count;
}
else
if (PQresultStatus(result) != PGRES_TUPLES_OK)
{
std::cerr << "Query failed: " << PQerrorMessage(connection) << std::endl;
PQclear(result);
throw std::runtime_error("Error executing query: " + query);

PQfinish(connection);
return false;
}

return true;
}

std::string HarshieDatabase::selectData(const std::string& targetColumn, const std::string& tableName, const std::string& searchCondition)
Expand All @@ -108,3 +109,9 @@ std::string HarshieDatabase::selectData(const std::string& targetColumn, const s
throw std::runtime_error("Error executing query: " + query);
}
}

HarshieDatabase& HarshieDatabase::getInstance()
{
static HarshieDatabase instance;
return instance;
}
12 changes: 10 additions & 2 deletions src/cores/database.h → src/database/database.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,22 @@ class HarshieDatabase
{
private:
PGconn* connection;
HarshieDatabase();

public:
HarshieDatabase(const std::string& connectionString);
~HarshieDatabase();

void connectDatabase(const std::string& connectionString);
static HarshieDatabase& getInstance();

bool createTable(const std::string& tableName, const std::string& columns);
bool insertData(const std::string& tableName, const std::string& values);
bool deleteData(const std::string& tableName, const std::string& condition);
int getCount(const std::string& tableName, const std::string& searchCondition);
bool findRecord(const std::string& tableName, const std::string& searchCondition);
std::string selectData(const std::string& targetColumn, const std::string& tableName, const std::string& searchCondition);

HarshieDatabase(const HarshieDatabase&) = delete;
HarshieDatabase(HarshieDatabase&&) = delete;
HarshieDatabase& operator=(const HarshieDatabase&) = delete;
HarshieDatabase& operator=(HarshieDatabase&&) = delete;
};
14 changes: 14 additions & 0 deletions src/database/db_create.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "db_create.h"

void HarshieCreateDatabase::languageData()
{
try
{
database.createTable("language_config", "id TEXT, language TEXT");
fmt::print("[{}] language_config table created!\n", dpp::utility::current_date_time());
}
catch(...)
{
fmt::print("[{}] Failed to create language_config table\n", dpp::utility::current_date_time());
}
}
15 changes: 15 additions & 0 deletions src/database/db_create.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <dpp/dpp.h>
#include <spdlog/spdlog.h>

#include "database.h"

class HarshieCreateDatabase
{
public:
void languageData();

private:
HarshieDatabase& database = HarshieDatabase::getInstance();
};

0 comments on commit 07cb989

Please sign in to comment.