feat(tgbot-back): start to develop back

Implemented fetchUserTitlesAsync func and embedded it in the code of the front in the trial mode. It needs to be restructured
This commit is contained in:
Kirill 2025-12-05 12:38:34 +03:00
parent 4ca8b19adb
commit 847aec7bdd
9 changed files with 190 additions and 13 deletions

View file

@ -0,0 +1,31 @@
#pragma once
#include "CppRestOpenAPIClient/ApiClient.h"
#include "CppRestOpenAPIClient/ApiConfiguration.h"
#include "CppRestOpenAPIClient/api/DefaultApi.h"
#include "CppRestOpenAPIClient/model/User.h"
#include "CppRestOpenAPIClient/model/GetUserTitles_200_response.h"
#include "CppRestOpenAPIClient/model/UserTitle.h"
#include "CppRestOpenAPIClient/model/Title.h"
#include "constants.hpp"
#include "structs.hpp"
#include <iostream>
#include <thread>
#include <functional>
#include <memory>
#include <cpprest/asyncrt_utils.h>
#include <boost/optional.hpp>
using namespace org::openapitools::client::api;
class BotToServer {
public:
BotToServer();
// Асинхронный метод: получить список тайтлов пользователя
pplx::task<std::vector<BotStructs::Title>> fetchUserTitlesAsync(const std::string& userId);
private:
std::shared_ptr<org::openapitools::client::api::ApiConfiguration> apiconfiguration;
std::shared_ptr<org::openapitools::client::api::ApiClient> apiclient;
std::shared_ptr<org::openapitools::client::api::DefaultApi> api;
};

View file

@ -0,0 +1,93 @@
#include "BotToServer.hpp"
BotToServer::BotToServer() {
apiconfiguration = std::make_shared<ApiConfiguration>();
const char* envUrl = getenv("NYANIMEDBBASEURL");
if (!envUrl) {
std::runtime_error("Environment variable NYANIMEDBBASEURL is not set");
}
apiconfiguration->setBaseUrl(utility::conversions::to_string_t(envUrl));
apiconfiguration->setUserAgent(utility::conversions::to_string_t("OpenAPI Client"));
apiclient = std::make_shared<ApiClient>(apiconfiguration);
api = std::make_shared<DefaultApi>(apiclient);
}
// Вспомогательная функция: преобразует UserTitle → BotStructs::Title
static BotStructs::Title mapUserTitleToBotTitle(
const std::shared_ptr<org::openapitools::client::model::UserTitle>& userTitle
) {
if (!userTitle || !userTitle->titleIsSet()) {
return BotStructs::Title{0, "Invalid", "", -1};
}
auto apiTitle = userTitle->getTitle();
if (!apiTitle) {
return BotStructs::Title{0, "No Title", "", -1};
}
int64_t id = apiTitle->getId();
std::string name = "Untitled";
auto titleNames = apiTitle->getTitleNames();
utility::string_t ru = U("ru");
if (titleNames.find(ru) != titleNames.end() && !titleNames.at(ru).empty()) {
name = utility::conversions::to_utf8string(titleNames.at(ru).front());
} else if (!titleNames.empty()) {
const auto& firstLang = *titleNames.begin();
if (!firstLang.second.empty()) {
name = utility::conversions::to_utf8string(firstLang.second.front());
}
}
std::string description = ""; // описание пока не поддерживается в OpenAPI-модели
return BotStructs::Title{id, name, description, -1};
}
pplx::task<std::vector<BotStructs::Title>> BotToServer::fetchUserTitlesAsync(const std::string& userId) {
utility::string_t userIdW = utility::conversions::to_string_t(userId);
int32_t limit = static_cast<int32_t>(BotConstants::DISP_TITLES_NUM);
auto responseTask = api->getUserTitles(
userIdW,
boost::none, // cursor
boost::none, // sort
boost::none, // sortForward
boost::none, // word
boost::none, // status
boost::none, // watchStatus
boost::none, // rating
boost::none, // myRate
boost::none, // releaseYear
boost::none, // releaseSeason
limit,
boost::none // fields
);
return responseTask.then([=](pplx::task<std::shared_ptr<org::openapitools::client::model::GetUserTitles_200_response>> task) {
try {
auto response = task.get();
if (!response) {
throw std::runtime_error("Null response from getUserTitles");
}
const auto& userTitles = response->getData();
std::vector<BotStructs::Title> result;
result.reserve(userTitles.size());
for (size_t i = 0; i < userTitles.size(); ++i) {
BotStructs::Title botTitle = mapUserTitleToBotTitle(userTitles[i]);
botTitle.num = static_cast<int64_t>(i); // 0-based индекс
result.push_back(botTitle);
}
return result;
} catch (const web::http::http_exception& e) {
std::cerr << "HTTP error in fetchUserTitlesAsync: " << e.what() << std::endl;
throw;
} catch (const std::exception& e) {
std::cerr << "Error in fetchUserTitlesAsync: " << e.what() << std::endl;
throw;
}
});
}