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:
parent
4ca8b19adb
commit
847aec7bdd
9 changed files with 190 additions and 13 deletions
|
|
@ -15,13 +15,13 @@ TgBot::InlineKeyboardMarkup::Ptr KeyboardFactory::createMainMenu() {
|
|||
}
|
||||
|
||||
// TODO: Переписать с учетом констант на количество отображаемых тайтлов и нового callback'a
|
||||
TgBot::InlineKeyboardMarkup::Ptr KeyboardFactory::createMyTitles(std::vector<Title> titles) {
|
||||
TgBot::InlineKeyboardMarkup::Ptr KeyboardFactory::createMyTitles(std::vector<BotStructs::Title> titles) {
|
||||
auto keyboard = std::make_shared<TgBot::InlineKeyboardMarkup>();
|
||||
std::vector<TgBot::InlineKeyboardButton::Ptr> row;
|
||||
std::vector<std::vector<TgBot::InlineKeyboardButton::Ptr>> layout;
|
||||
|
||||
int counter = 0;
|
||||
for(Title& title : titles) {
|
||||
for(BotStructs::Title& title : titles) {
|
||||
if(counter >= 6) {
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ void BotHandlers::handleCallback(TgBot::CallbackQuery::Ptr query) {
|
|||
|
||||
HandlerResult BotHandlers::returnMyTitles(int64_t userId, int64_t payload) {
|
||||
// Здесь должен происходить запрос на сервер
|
||||
std::vector<Title> titles = {{123, "Школа мертвяков", "", 1}, {321, "KissXsis", "", 2}};
|
||||
std::vector<BotStructs::Title> titles = {{123, "Школа мертвяков", "", 1}, {321, "KissXsis", "", 2}};
|
||||
|
||||
struct HandlerResult result;
|
||||
result.keyboard = KeyboardFactory::createMyTitles(titles);
|
||||
|
|
@ -85,7 +85,8 @@ void BotHandlers::handleNavigation(TgBot::CallbackQuery::Ptr query, UserContext&
|
|||
|
||||
ctx.history.back().payload = newPayload;
|
||||
|
||||
auto result = renderCurrent(ctx);
|
||||
auto result = renderCurrent(query, ctx);
|
||||
if(result.message == "meow") return; // TODO: убрать
|
||||
editMessage(query, result);
|
||||
return;
|
||||
}
|
||||
|
|
@ -96,7 +97,8 @@ void BotHandlers::handleNavigation(TgBot::CallbackQuery::Ptr query, UserContext&
|
|||
sendError(query, BotConstants::Text::SAD_ERROR);
|
||||
return;
|
||||
}
|
||||
auto result = renderCurrent(ctx);
|
||||
auto result = renderCurrent(query, ctx);
|
||||
if(result.message == "meow") return; // TODO: убрать
|
||||
editMessage(query, result);
|
||||
return;
|
||||
}
|
||||
|
|
@ -109,7 +111,8 @@ void BotHandlers::handleNavigation(TgBot::CallbackQuery::Ptr query, UserContext&
|
|||
}
|
||||
|
||||
ctx.history.push_back(*newStepOpt);
|
||||
auto result = renderCurrent(ctx);
|
||||
auto result = renderCurrent(query, ctx);
|
||||
if(result.message == "meow") return; // TODO: убрать
|
||||
editMessage(query, result);
|
||||
}
|
||||
|
||||
|
|
@ -161,15 +164,32 @@ void BotHandlers::editMessage(TgBot::CallbackQuery::Ptr query, HandlerResult res
|
|||
);
|
||||
}
|
||||
|
||||
HandlerResult BotHandlers::renderCurrent(const UserContext& ctx) {
|
||||
HandlerResult BotHandlers::renderCurrent(TgBot::CallbackQuery::Ptr query, const UserContext& ctx) {
|
||||
const auto& step = ctx.history.back();
|
||||
int64_t userId = query->from->id;
|
||||
switch (step.state) {
|
||||
case UserState::MAIN_MENU:
|
||||
return showMainMenu();
|
||||
case UserState::VIEWING_MY_TITLES:
|
||||
return returnMyTitles(ctx.userId, step.payload); // payload = offset
|
||||
server_.fetchUserTitlesAsync(std::to_string(2)) // ALARM: тестовое значение вместо userId
|
||||
.then([this, query](pplx::task<std::vector<BotStructs::Title>> t) {
|
||||
try {
|
||||
auto titles = t.get();
|
||||
|
||||
std::string message = formatTitlesList(titles);
|
||||
auto keyboard = KeyboardFactory::createMyTitles(titles);
|
||||
|
||||
editMessage(query, {message, keyboard});
|
||||
|
||||
} catch (const std::exception& e) {
|
||||
sendError(query, BotConstants::Text::SERVER_ERROR);
|
||||
// Логирование ошибки (например, в cerr)
|
||||
}
|
||||
});
|
||||
|
||||
return {"meow", nullptr};
|
||||
/*
|
||||
case UserState::VIEWING_TITLE_PAGE:
|
||||
case UserState::VIEWING_TITLE_PAGE:
|
||||
return returnTitlePage(step.payload); // payload = titleId
|
||||
case UserState::VIEWING_REVIEW:
|
||||
return returnReview(step.payload); // payload = reviewId
|
||||
|
|
@ -256,4 +276,17 @@ void BotHandlers::handleError(TgBot::CallbackQuery::Ptr query, UserContext& ctx)
|
|||
HandlerResult result = {BotConstants::Text::AUTH_ERROR, nullptr};
|
||||
editMessage(query, result);
|
||||
}
|
||||
}
|
||||
|
||||
std::string BotHandlers::formatTitlesList(const std::vector<BotStructs::Title>& titles) {
|
||||
if (titles.empty()) {
|
||||
return "У вас пока нет тайтлов.";
|
||||
}
|
||||
|
||||
std::string msg;
|
||||
for (size_t i = 0; i < titles.size(); ++i) {
|
||||
// num — 0-based, но в сообщении показываем 1-based
|
||||
msg += std::to_string(i + 1) + ". " + titles[i].name + "\n";
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue