Skip to content
This repository has been archived by the owner on Jan 3, 2025. It is now read-only.

Commit

Permalink
Merge pull request #37 from The-B1T-Foundation/added_syn_ant_funcs
Browse files Browse the repository at this point in the history
Added syn ant funcs
  • Loading branch information
b1tflyyyy authored May 6, 2024
2 parents 0ae882f + 6cf1819 commit c88c0e0
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,71 @@ AEnglish_Words_Info_API_Controller::~AEnglish_Words_Info_API_Controller()

// ---------------------------------------------------------------------------------------------------------------------
std::optional<std::string> AEnglish_Words_Info_API_Controller::Get_Definition(const std::string& word)
{
if (!Send_Request(word, "/definitions", "GET"))
{
return std::nullopt;
}

std::string response{};
response.reserve(500);

for (std::size_t i{}; i < Max_Response_Depth && !Json_Response["definitions"][i].empty(); ++i)
{
response += Json_Response["definitions"][i]["definition"];
response += "\n\n";
}

return response;
}

// ---------------------------------------------------------------------------------------------------------------------
std::optional<std::string> AEnglish_Words_Info_API_Controller::Get_Antonym(const std::string& word)
{
if (!Send_Request(word, "/antonyms", "GET"))
{
return std::nullopt;
}

std::string response{};
response.reserve(500);

for (std::size_t i{}; i < Max_Response_Depth && !Json_Response["antonyms"][i].empty(); ++i)
{
response += Json_Response["antonyms"][i];
response += "\n";
}

return response;
}

// ---------------------------------------------------------------------------------------------------------------------
std::optional<std::string> AEnglish_Words_Info_API_Controller::Get_Synonym(const std::string& word)
{
if (!Send_Request(word, "/synonyms", "GET"))
{
return std::nullopt;
}

std::string response{};
response.reserve(500);

for (std::size_t i{}; i < Max_Response_Depth && !Json_Response["synonyms"][i].empty(); ++i)
{
response += Json_Response["synonyms"][i];
response += "\n";
}

return response;
}

// ---------------------------------------------------------------------------------------------------------------------
bool AEnglish_Words_Info_API_Controller::Send_Request(std::string_view word, std::string_view endpoint, std::string_view request_type)
{
if (Curl = curl_easy_init(); Curl != nullptr)
{
curl_easy_setopt(Curl, CURLOPT_URL, (Url + word).data());
curl_easy_setopt(Curl, CURLOPT_CUSTOMREQUEST, "GET");
curl_easy_setopt(Curl, CURLOPT_URL, (Url + word.data() + endpoint.data()).data());
curl_easy_setopt(Curl, CURLOPT_CUSTOMREQUEST, request_type.data());
curl_easy_setopt(Curl, CURLOPT_HTTPHEADER, Headers);
curl_easy_setopt(Curl, CURLOPT_WRITEFUNCTION, &ABase_API_Controller::Write_Callback);
curl_easy_setopt(Curl, CURLOPT_WRITEDATA, &AEnglish_Words_Info_API_Controller::Json_Response);
Expand All @@ -58,11 +118,11 @@ std::optional<std::string> AEnglish_Words_Info_API_Controller::Get_Definition(co

curl_easy_cleanup(Curl);

if (resp_code != EHTTP_STATUS::OK || Json_Response["results"][0]["definition"].empty())
if (resp_code != EHTTP_STATUS::OK)
{
return std::nullopt;
return false;
}
}

return Json_Response["results"][0]["definition"];
return Curl != nullptr;
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ class AEnglish_Words_Info_API_Controller : public ABase_API_Controller
~AEnglish_Words_Info_API_Controller() override;

std::optional<std::string> Get_Definition(const std::string& word);
std::optional<std::string> Get_Antonym(const std::string& word);
std::optional<std::string> Get_Synonym(const std::string& word);

private:
bool Send_Request(std::string_view word, std::string_view endpoint, std::string_view request_type);

private:
constexpr static std::size_t Max_Response_Depth{ 10 };
static nljson Json_Response;
curl_slist* Headers;
};
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ AMetrics_DB_Controller::AMetrics_DB_Controller(const ADB_Config& db_cfg) :
// ---------------------------------------------------------------------------------------------------------------------
void AMetrics_DB_Controller::Set_Metrics(const SMetrics& metrics)
{
Exec_Query(std::format(R"(INSERT INTO {} (start_req, profile_req, pr_game_req, math_game_req, help_req, about_project_req, definition_req, date, total_number_of_requests) VALUES ('{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}'))", Table_Name, metrics.Start_Request_Count, metrics.Profile_Request_Count, metrics.Pr_Game_Request_Count, metrics.Math_Game_Request_Count, metrics.Help_Request_Count, metrics.About_Project_Request_Count, metrics.Definition_Request_Count, metrics.Get_Current_Date(), metrics.Get_Total_Number_Of_Requests()));
Exec_Query(std::format(R"(INSERT INTO {} (start_req, profile_req, pr_game_req, math_game_req, help_req, about_project_req, definition_req, date, total_number_of_requests, antonym_req, synonym_req) VALUES ('{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}'))", Table_Name, metrics.Start_Request_Count, metrics.Profile_Request_Count, metrics.Pr_Game_Request_Count, metrics.Math_Game_Request_Count, metrics.Help_Request_Count, metrics.About_Project_Request_Count, metrics.Definition_Request_Count, metrics.Get_Current_Date(), metrics.Get_Total_Number_Of_Requests(), metrics.Antonym_Request_Count, metrics.Synonym_Request_Count));
}

// ---------------------------------------------------------------------------------------------------------------------
SMetrics AMetrics_DB_Controller::Get_Metrics(std::int64_t metrics_id)
{
auto response{ Exec_Query(std::format(R"(SELECT start_req, profile_req, pr_game_req, math_game_req, help_req, about_project_req, definition_req, date FROM {} WHERE id = '{}')", Table_Name, metrics_id)) };
return SMetrics{ response[0][0].as<std::int64_t>(), response[0][1].as<std::int64_t>(), response[0][2].as<std::int64_t>(), response[0][3].as<std::int64_t>(), response[0][4].as<std::int64_t>(), response[0][5].as<std::int64_t>(), response[0][6].as<std::int64_t>(), response[0][7].as<std::string>() };
auto response{ Exec_Query(std::format(R"(SELECT start_req, profile_req, pr_game_req, math_game_req, help_req, about_project_req, definition_req, antonym_req, synonym_req, date FROM {} WHERE id = '{}')", Table_Name, metrics_id)) };
return SMetrics{ response[0][0].as<std::int64_t>(), response[0][1].as<std::int64_t>(), response[0][2].as<std::int64_t>(), response[0][3].as<std::int64_t>(), response[0][4].as<std::int64_t>(), response[0][5].as<std::int64_t>(), response[0][6].as<std::int64_t>(), response[0][7].as<std::int64_t>(), response[0][8].as<std::int64_t>(), response[0][9].as<std::string>() };
}

// ---------------------------------------------------------------------------------------------------------------------
Expand All @@ -51,6 +51,6 @@ std::pair<std::int64_t, std::int64_t> AMetrics_DB_Controller::Get_Metrics_Range(
// ---------------------------------------------------------------------------------------------------------------------
SMetrics AMetrics_DB_Controller::Get_Best_Metrics()
{
auto response{ Exec_Query(std::format(R"(SELECT start_req, profile_req, pr_game_req, math_game_req, help_req, about_project_req, definition_req, date FROM {} WHERE total_number_of_requests = (SELECT MAX(total_number_of_requests) FROM {}))", Table_Name, Table_Name)) };
return SMetrics{ response[0][0].as<std::int64_t>(), response[0][1].as<std::int64_t>(), response[0][2].as<std::int64_t>(), response[0][3].as<std::int64_t>(), response[0][4].as<std::int64_t>(), response[0][5].as<std::int64_t>(), response[0][6].as<std::int64_t>(), response[0][7].as<std::string>() };
auto response{ Exec_Query(std::format(R"(SELECT start_req, profile_req, pr_game_req, math_game_req, help_req, about_project_req, definition_req, antonym_req, synonym_req, date FROM {} WHERE total_number_of_requests = (SELECT MAX(total_number_of_requests) FROM {}))", Table_Name, Table_Name)) };
return SMetrics{ response[0][0].as<std::int64_t>(), response[0][1].as<std::int64_t>(), response[0][2].as<std::int64_t>(), response[0][3].as<std::int64_t>(), response[0][4].as<std::int64_t>(), response[0][5].as<std::int64_t>(), response[0][6].as<std::int64_t>(), response[0][7].as<std::int64_t>(), response[0][8].as<std::int64_t>(),response[0][9].as<std::string>() };
}
10 changes: 6 additions & 4 deletions source/business_logic/model/metrics/metrics_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@

// ---------------------------------------------------------------------------------------------------------------------
SMetrics::SMetrics() :
Start_Request_Count{}, Profile_Request_Count{}, Pr_Game_Request_Count{}, Math_Game_Request_Count{}, Help_Request_Count{}, About_Project_Request_Count{}, Definition_Request_Count{}, Current_Date{}
Start_Request_Count{}, Profile_Request_Count{}, Pr_Game_Request_Count{}, Math_Game_Request_Count{}, Help_Request_Count{}, About_Project_Request_Count{}, Definition_Request_Count{}, Antonym_Request_Count{}, Synonym_Request_Count{}, Current_Date{}
{ }

// ---------------------------------------------------------------------------------------------------------------------
SMetrics::SMetrics(std::int64_t start_request_count, std::int64_t profile_request_count, std::int64_t pr_game_request_count, std::int64_t math_game_request_count, std::int64_t help_request_count, std::int64_t about_project_request_count, std::int64_t definition_request_count, std::string current_date) :
Start_Request_Count{ start_request_count }, Profile_Request_Count{ profile_request_count }, Pr_Game_Request_Count{ pr_game_request_count }, Math_Game_Request_Count{ math_game_request_count }, Help_Request_Count{ help_request_count }, About_Project_Request_Count{ about_project_request_count }, Definition_Request_Count{ definition_request_count }, Current_Date{ std::move(current_date) }
SMetrics::SMetrics(std::int64_t start_request_count, std::int64_t profile_request_count, std::int64_t pr_game_request_count, std::int64_t math_game_request_count, std::int64_t help_request_count, std::int64_t about_project_request_count, std::int64_t definition_request_count, std::int64_t antonym_request_count, std::int64_t synonym_request_count, std::string current_date) :
Start_Request_Count{ start_request_count }, Profile_Request_Count{ profile_request_count }, Pr_Game_Request_Count{ pr_game_request_count }, Math_Game_Request_Count{ math_game_request_count }, Help_Request_Count{ help_request_count }, About_Project_Request_Count{ about_project_request_count }, Definition_Request_Count{ definition_request_count }, Antonym_Request_Count{ antonym_request_count }, Synonym_Request_Count{ synonym_request_count }, Current_Date{ std::move(current_date) }
{ }

// ---------------------------------------------------------------------------------------------------------------------
Expand All @@ -49,7 +49,7 @@ const std::string& SMetrics::Get_Current_Date() const
// ---------------------------------------------------------------------------------------------------------------------
std::int64_t SMetrics::Get_Total_Number_Of_Requests() const
{
return Start_Request_Count + Profile_Request_Count + Pr_Game_Request_Count + Math_Game_Request_Count + Help_Request_Count + About_Project_Request_Count + Definition_Request_Count;
return Start_Request_Count + Profile_Request_Count + Pr_Game_Request_Count + Math_Game_Request_Count + Help_Request_Count + About_Project_Request_Count + Definition_Request_Count + Antonym_Request_Count + Synonym_Request_Count;
}

// ---------------------------------------------------------------------------------------------------------------------
Expand All @@ -62,5 +62,7 @@ void SMetrics::Clear()
Help_Request_Count = 0;
About_Project_Request_Count = 0;
Definition_Request_Count = 0;
Antonym_Request_Count = 0;
Synonym_Request_Count = 0;
Current_Date = " ";
}
5 changes: 4 additions & 1 deletion source/business_logic/model/metrics/metrics_model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct SMetrics
{
public:
explicit SMetrics();
[[maybe_unused]] explicit SMetrics(std::int64_t start_request_count, std::int64_t profile_request_count, std::int64_t pr_game_request_count, std::int64_t math_game_request_count, std::int64_t help_request_count, std::int64_t about_project_request_count, std::int64_t definition_request_count, std::string current_date);
[[maybe_unused]] explicit SMetrics(std::int64_t start_request_count, std::int64_t profile_request_count, std::int64_t pr_game_request_count, std::int64_t math_game_request_count, std::int64_t help_request_count, std::int64_t about_project_request_count, std::int64_t definition_request_count, std::int64_t antonym_request_count, std::int64_t synonym_request_count, std::string current_date);

~SMetrics() = default;

Expand All @@ -42,6 +42,7 @@ struct SMetrics
[[nodiscard]] std::int64_t Get_Total_Number_Of_Requests() const;
void Clear();

// antonym - synonym
public:
std::int64_t Start_Request_Count;
std::int64_t Profile_Request_Count;
Expand All @@ -50,6 +51,8 @@ struct SMetrics
std::int64_t Help_Request_Count;
std::int64_t About_Project_Request_Count;
std::int64_t Definition_Request_Count;
std::int64_t Antonym_Request_Count;
std::int64_t Synonym_Request_Count;

private:
std::string Current_Date;
Expand Down
42 changes: 42 additions & 0 deletions source/view/handlers/message_handler/message_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,48 @@ void AMessage_Handler::Bind_Commands()

TG_Bot.getApi().sendMessage(message->chat->id, AMessage_Reply::Get_Not_Found_Word_Definition_Msg());
});
TG_Bot.getEvents().onCommand(SMessage_Commands::Get_Antonym.data(), [this](TgBot::Message::Ptr message) -> void
{
if (!Words_Api_Requests_Limiter.Can_Send_Request())
{
TG_Bot.getApi().sendMessage(message->chat->id, AMessage_Reply::Get_Limit_Api_Requests_Msg());
return;
}

std::lock_guard<std::mutex> locker(Mutex);
++Metrics.Antonym_Request_Count;

Cut_User_Input(message->text, SMessage_Commands::Get_Antonym.size());
if (auto response{ English_Words_API_Controller.Get_Antonym(message->text) }; response != std::nullopt)
{
TG_Bot.getApi().sendMessage(message->chat->id,
AMessage_Reply::Get_Antonym_Msg(message->text, response.value()));
return;
}

TG_Bot.getApi().sendMessage(message->chat->id, AMessage_Reply::Get_Not_Found_Antonym_Msg());
});
TG_Bot.getEvents().onCommand(SMessage_Commands::Get_Synonym.data(), [this](TgBot::Message::Ptr message) -> void
{
if (!Words_Api_Requests_Limiter.Can_Send_Request())
{
TG_Bot.getApi().sendMessage(message->chat->id, AMessage_Reply::Get_Limit_Api_Requests_Msg());
return;
}

std::lock_guard<std::mutex> locker(Mutex);
++Metrics.Synonym_Request_Count;

Cut_User_Input(message->text, SMessage_Commands::Get_Synonym.size());
if (auto response{ English_Words_API_Controller.Get_Synonym(message->text) }; response != std::nullopt)
{
TG_Bot.getApi().sendMessage(message->chat->id,
AMessage_Reply::Get_Synonym_Msg(message->text, response.value()));
return;
}

TG_Bot.getApi().sendMessage(message->chat->id, AMessage_Reply::Get_Not_Found_Synonym_Msg());
});
TG_Bot.getEvents().onCommand(SMessage_Commands::Metrics_Range.data(), [this](TgBot::Message::Ptr message) -> void
{
if (!Is_Root_User(message->from->id))
Expand Down
6 changes: 4 additions & 2 deletions source/view/texts/message_commands/message_commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ constinit std::string_view SMessage_Commands::Start{ "start" };
constinit std::string_view SMessage_Commands::Profile{ "profile" };
constinit std::string_view SMessage_Commands::Programmer_Game{ "pr_game" };
constinit std::string_view SMessage_Commands::Math_Game{ "math_game" };
constinit std::string_view SMessage_Commands::Answer{ "answer" };
constinit std::string_view SMessage_Commands::Answer{ "ans" };
constinit std::string_view SMessage_Commands::Help{ "help" };
constinit std::string_view SMessage_Commands::About_Project{ "about_project" };
constinit std::string_view SMessage_Commands::Definiton{ "def" };
constinit std::string_view SMessage_Commands::Metrics_Range{"metrics_range" };
constinit std::string_view SMessage_Commands::Get_Metrics{ "get_metrics" };
constinit std::string_view SMessage_Commands::Get_Best_Metric{ "get_best_metric" };
constinit std::string_view SMessage_Commands::Get_Best_Metric{ "get_best_metric" };
constinit std::string_view SMessage_Commands::Get_Antonym{ "ant" };
constinit std::string_view SMessage_Commands::Get_Synonym{ "syn" };
2 changes: 2 additions & 0 deletions source/view/texts/message_commands/message_commands.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ struct SMessage_Commands
constinit static std::string_view Metrics_Range;
constinit static std::string_view Get_Metrics;
constinit static std::string_view Get_Best_Metric;
constinit static std::string_view Get_Antonym;
constinit static std::string_view Get_Synonym;
};
Loading

0 comments on commit c88c0e0

Please sign in to comment.