Implemented fetchUserTitlesAsync func and embedded it in the code of the front in the trial mode. It needs to be restructured
93 lines
No EOL
3.5 KiB
C++
93 lines
No EOL
3.5 KiB
C++
#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;
|
|
}
|
|
});
|
|
} |