From dbbcaf84e9a86f0381dff83c1c1a1e32ae4856c0 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Tue, 30 Apr 2024 21:37:17 +0500 Subject: feat: Channel in Request struct --- src/commands/request.hpp | 4 ++++ src/commands/request_util.cpp | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) (limited to 'src/commands') diff --git a/src/commands/request.hpp b/src/commands/request.hpp index efa0655..6149dee 100644 --- a/src/commands/request.hpp +++ b/src/commands/request.hpp @@ -5,6 +5,7 @@ #include #include "../irc/message.hpp" +#include "../schemas/channel.hpp" namespace bot::command { struct Request { @@ -12,6 +13,9 @@ namespace bot::command { std::optional subcommand_id; std::optional message; const irc::Message &irc_message; + + schemas::Channel channel; + pqxx::connection &conn; }; } diff --git a/src/commands/request_util.cpp b/src/commands/request_util.cpp index 08e958a..a0965c3 100644 --- a/src/commands/request_util.cpp +++ b/src/commands/request_util.cpp @@ -2,9 +2,11 @@ #include #include +#include #include "../constants.hpp" #include "../irc/message.hpp" +#include "../schemas/channel.hpp" #include "command.hpp" #include "request.hpp" @@ -13,6 +15,10 @@ namespace bot::command { const command::CommandLoader &command_loader, const irc::Message &irc_message, pqxx::connection &conn) { + pqxx::work *work; + + work = new pqxx::work(conn); + std::vector parts = utils::string::split_text(irc_message.message, ' '); @@ -36,8 +42,32 @@ namespace bot::command { parts.erase(parts.begin()); + pqxx::result channel_query = + work->exec("SELECT * FROM channels WHERE alias_id = " + + std::to_string(irc_message.source.id)); + + // Create new channel data in the database if it didn't exist b4 + if (channel_query.empty()) { + work->exec("INSERT INTO channels (alias_id, alias_name) VALUES (" + + std::to_string(irc_message.source.id) + ", '" + + irc_message.source.login + "')"); + + work->commit(); + + delete work; + work = new pqxx::work(conn); + + channel_query = work->exec("SELECT * FROM channels WHERE alias_id = " + + std::to_string(irc_message.source.id)); + } + + schemas::Channel channel(channel_query[0]); + + delete work; + if (parts.empty()) { - Request req{command_id, std::nullopt, std::nullopt, irc_message, work}; + Request req{command_id, std::nullopt, std::nullopt, + irc_message, channel, conn}; return req; } @@ -54,7 +84,7 @@ namespace bot::command { message = std::nullopt; } - Request req{command_id, subcommand_id, message, irc_message, work}; + Request req{command_id, subcommand_id, message, irc_message, channel, conn}; return req; } } -- cgit v1.2.3