From a47294d563c940e1423791eaa5f4e0bfe6b9dae1 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Tue, 30 Apr 2024 22:27:49 +0500 Subject: feat: added ChannelPreferences model into Request struct --- src/commands/request.hpp | 1 + src/commands/request_util.cpp | 32 ++++++++++++++++++++++++++++---- 2 files changed, 29 insertions(+), 4 deletions(-) (limited to 'src/commands') diff --git a/src/commands/request.hpp b/src/commands/request.hpp index 702fcb8..8538cdc 100644 --- a/src/commands/request.hpp +++ b/src/commands/request.hpp @@ -16,6 +16,7 @@ namespace bot::command { const irc::Message &irc_message; schemas::Channel channel; + schemas::ChannelPreferences channel_preferences; schemas::User user; pqxx::connection &conn; diff --git a/src/commands/request_util.cpp b/src/commands/request_util.cpp index 94e563d..9424082 100644 --- a/src/commands/request_util.cpp +++ b/src/commands/request_util.cpp @@ -63,6 +63,30 @@ namespace bot::command { schemas::Channel channel(channel_query[0]); + pqxx::result channel_pref_query = + work->exec("SELECT * FROM channel_preferences WHERE channel_id = " + + std::to_string(channel.get_id())); + + // Create new channel preference data in the database if it didn't exist b4 + if (channel_pref_query.empty()) { + work->exec( + "INSERT INTO channel_preferences (channel_id, prefix, locale) VALUES " + "(" + + std::to_string(channel.get_id()) + ", '" + DEFAULT_PREFIX + "', '" + + DEFAULT_LOCALE_ID + "')"); + + work->commit(); + + delete work; + work = new pqxx::work(conn); + + channel_pref_query = + work->exec("SELECT * FROM channel_preferences WHERE channel_id = " + + std::to_string(channel.get_id())); + } + + schemas::ChannelPreferences channel_preferences(channel_pref_query[0]); + pqxx::result user_query = work->exec("SELECT * FROM users WHERE alias_id = " + std::to_string(irc_message.sender.id)); @@ -87,8 +111,8 @@ namespace bot::command { delete work; if (parts.empty()) { - Request req{command_id, std::nullopt, std::nullopt, irc_message, - channel, user, conn}; + Request req{command_id, std::nullopt, std::nullopt, irc_message, + channel, channel_preferences, user, conn}; return req; } @@ -105,8 +129,8 @@ namespace bot::command { message = std::nullopt; } - Request req{command_id, subcommand_id, message, irc_message, - channel, user, conn}; + Request req{command_id, subcommand_id, message, irc_message, + channel, channel_preferences, user, conn}; return req; } } -- cgit v1.2.3