Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/backend/api/ingredients.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ class IngredientsApi : ApiBase {
void putToStorage(UserId user, StorageId storage, IngredientId ingredient) const;
void deleteFromStorage(UserId user, StorageId storage, IngredientId ingredient) const;

void
deleteMultipleFromStorage(UserId user, StorageId storage, const std::vector<IngredientId>& ingredients = {}) const;
void deleteMultipleFromStorage(UserId user, StorageId storage, const std::vector<IngredientId>& ingredients) const;

[[nodiscard]] models::ingredient::IngredientSearchForStorageResponse
searchForStorage(UserId user,
Expand Down
6 changes: 4 additions & 2 deletions src/handlers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ target_sources(main PRIVATE
src/handlers/personal_account/view.cpp
src/handlers/personal_account/publication_history.cpp

src/handlers/suggested_recipe/add_storage.cpp
src/handlers/suggested_recipe/view.cpp
src/handlers/cooking_planning/add_storage.cpp
src/handlers/cooking_planning/view.cpp

src/handlers/recipes_suggestions/view.cpp

Expand All @@ -50,4 +50,6 @@ target_sources(main PRIVATE
src/handlers/recipes_search/view.cpp

src/handlers/recipe/view.cpp

src/handlers/cooking/ingredients_spending.cpp
)
2 changes: 1 addition & 1 deletion src/handlers/commands/start.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void handleStartCmd(MessageRef m, BotRef bot, SMRef stateManager, api::ApiClient
auto recipe = api.getRecipesApi().get(userId, *mRecipeId);
message::deleteMessageId(userId);
renderRecipeView(recipe, *mRecipeId, userId, chatId, bot);
stateManager.put(RecipeView{.prevState = std::nullopt, .recipe = std::move(recipe)});
stateManager.put(RecipeView{.prevState = std::nullopt, .recipe = std::move(recipe), .recipeId = *mRecipeId});
return;
}
};
Expand Down
12 changes: 6 additions & 6 deletions src/handlers/commands/wanna_eat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
#include "utils/utils.hpp"

#include <optional>
#include <utility>
#include <vector>

namespace cookcookhnya::handlers::commands {

using namespace render::select_storages;
using namespace render::main_menu;
using namespace render::recipes_suggestions;
using namespace std::views;

void handleWannaEatCmd(MessageRef m, BotRef bot, SMRef stateManager, api::ApiClientRef api) {
auto storages = api.getStoragesApi().getStoragesList(m.from->id);
Expand All @@ -25,15 +28,12 @@ void handleWannaEatCmd(MessageRef m, BotRef bot, SMRef stateManager, api::ApiCli
stateManager.put(MainMenu{});
} else if (storages.size() == 1) {
message::deleteMessageId(m.from->id);
renderRecipesSuggestion({storages}, 0, m.from->id, m.chat->id, bot, api);
renderRecipesSuggestion({storages[0].id}, 0, m.from->id, m.chat->id, bot, api);
stateManager.put(SuggestedRecipesList{
.selectedStorages = storages,
.pageNo = 0,
.fromStorage = false,
});
.prevState = SuggestedRecipesList::FromMainMenuData{{}, std::move(storages[0])}, .pageNo = 0});
} else {
message::deleteMessageId(m.from->id);
auto newState = StoragesSelection{};
auto newState = StoragesSelection{.prevState = MainMenu{}, .selectedStorages = {}};
renderStorageSelection(newState, m.from->id, m.chat->id, bot, api);
stateManager.put(newState);
}
Expand Down
6 changes: 4 additions & 2 deletions src/handlers/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ using states::StorageList;

using states::TotalPublicationHistory;

using states::RecipeStorageAddition;
using states::SuggestedRecipeView;
using states::CookingPlanning;
using states::CookingPlanningStorageAddition;

using states::StorageCreationEnterName;
using states::StorageDeletion;
Expand Down Expand Up @@ -57,6 +57,8 @@ using states::RecipesSearch;

using states::RecipeView;

using states::CookingIngredientsSpending;

// Type aliases
using BotRef = const TgBot::Api&;
using SMRef = const states::StateManager&;
Expand Down
70 changes: 70 additions & 0 deletions src/handlers/cooking/ingredients_spending.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "ingredients_spending.hpp"

#include "backend/api/api.hpp"
#include "backend/id_types.hpp"
#include "handlers/common.hpp"
#include "render/cooking/ingredients_spending.hpp"
#include "render/cooking_planning/view.hpp"
#include "states.hpp"
#include "utils/parsing.hpp"
#include "utils/utils.hpp"

#include <algorithm>
#include <ranges>
#include <string_view>
#include <utility>

namespace cookcookhnya::handlers::cooking {

using namespace render::cooking_planning;
using namespace render::cooking;
using states::helpers::SelectableIngredient;
using namespace std::literals;
using namespace std::views;
using std::ranges::to;

void handleCookingIngredientsSpendingCQ(
CookingIngredientsSpending& state, CallbackQueryRef cq, BotRef bot, SMRef stateManager, api::ApiClientRef api) {
auto chatId = cq.message->chat->id;
auto userId = cq.from->id;

if (cq.data == "back") {
bot.answerCallbackQuery(cq.id);
renderCookingPlanning(state.prevState.availability, state.prevState.recipeId, userId, chatId, bot, api);
stateManager.put(auto{std::move(state.prevState)});
return;
}

if (cq.data == "remove") {
if (!state.storageId)
return;
auto selected = state.ingredients | filter(&SelectableIngredient::selected) |
transform(&SelectableIngredient::id) | to<std::vector>();
api.getIngredientsApi().deleteMultipleFromStorage(userId, *state.storageId, selected);
bot.answerCallbackQuery(cq.id, utils::utf8str(u8"Успешно удалено из выбранного хранилища"));
return;
}

if (cq.data == "to_shopping_list") {
auto selected = state.ingredients | filter(&SelectableIngredient::selected) |
transform(&SelectableIngredient::id) | to<std::vector>();
api.getShoppingListApi().put(userId, selected);
bot.answerCallbackQuery(cq.id, utils::utf8str(u8"Успешно добавлено"));
return;
}

if (cq.data.starts_with("ingredient_")) {
auto mIngredientId =
utils::parseSafe<api::IngredientId>(std::string_view{cq.data}.substr("ingredient_"sv.size()));
if (!mIngredientId)
return;
auto ingredientIter = std::ranges::find(state.ingredients, *mIngredientId, &SelectableIngredient::id);
if (ingredientIter == state.ingredients.end())
return;
ingredientIter->selected = !ingredientIter->selected;
renderIngredientsSpending(state.ingredients, state.storageId.has_value(), userId, chatId, bot);
return;
}
}

} // namespace cookcookhnya::handlers::cooking
11 changes: 11 additions & 0 deletions src/handlers/cooking/ingredients_spending.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include "backend/api/api.hpp"
#include "handlers/common.hpp"

namespace cookcookhnya::handlers::cooking {

void handleCookingIngredientsSpendingCQ(
CookingIngredientsSpending& state, CallbackQueryRef cq, BotRef bot, SMRef stateManager, api::ApiClientRef api);

} // namespace cookcookhnya::handlers::cooking
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include "backend/id_types.hpp"
#include "backend/models/storage.hpp"
#include "handlers/common.hpp"
#include "render/suggested_recipe/add_storage.hpp"
#include "render/suggested_recipe/view.hpp"
#include "render/cooking_planning/add_storage.hpp"
#include "render/cooking_planning/view.hpp"
#include "states.hpp"
#include "utils/ingredients_availability.hpp"
#include "utils/parsing.hpp"
Expand All @@ -15,20 +15,20 @@
#include <string_view>
#include <utility>

namespace cookcookhnya::handlers::suggested_recipe {
namespace cookcookhnya::handlers::cooking_planning {

using namespace render::suggested_recipe;
using namespace render::cooking_planning;
using namespace api::models::storage;

void handleRecipeStorageAdditionCQ(
RecipeStorageAddition& state, CallbackQueryRef cq, BotRef bot, SMRef stateManager, api::ApiClientRef api) {
void handleCookingPlanningStorageAdditionCQ(
CookingPlanningStorageAddition& state, CallbackQueryRef cq, BotRef bot, SMRef stateManager, api::ApiClientRef api) {
bot.answerCallbackQuery(cq.id);
const std::string& data = cq.data;
auto chatId = cq.message->chat->id;
auto userId = cq.from->id;

if (data == "back") {
renderRecipeView(state.prevState.availability, state.prevState.recipeId, userId, chatId, bot, api);
renderCookingPlanning(state.prevState.availability, state.prevState.recipeId, userId, chatId, bot, api);
stateManager.put(auto{std::move(state.prevState)});
return;
}
Expand All @@ -40,14 +40,27 @@ void handleRecipeStorageAdditionCQ(
const StorageSummary newStorage = {.id = *newStorageId, .name = newStorageDetails.name};
state.prevState.addedStorages.push_back(newStorage);
utils::addStorage(state.prevState.availability, newStorage);

using StoragesList = std::vector<StorageSummary>;
auto selectedStorages = state.prevState.getStorages();
const StoragesList* selectedStoragesPtr = nullptr;
// very optimized decision! (no)
if (auto* storagesVal = std::get_if<StoragesList>(&selectedStorages))
selectedStoragesPtr = storagesVal;
else if (auto* storagesRef = std::get_if<std::reference_wrapper<StoragesList>>(&selectedStorages))
selectedStoragesPtr = &storagesRef->get();
else
return;

renderStoragesSuggestion(state.prevState.availability,
state.prevState.prevState.selectedStorages,
*selectedStoragesPtr,
state.prevState.addedStorages,
state.prevState.recipeId,
userId,
chatId,
bot,
api);
return;
}
}

Expand All @@ -59,16 +72,29 @@ void handleRecipeStorageAdditionCQ(
state.prevState.addedStorages.erase(std::ranges::find(
state.prevState.addedStorages, newStorageId, &api::models::storage::StorageSummary::id));
utils::deleteStorage(state.prevState.availability, newStorage);

using StoragesList = std::vector<StorageSummary>;
auto selectedStorages = state.prevState.getStorages();
const StoragesList* selectedStoragesPtr = nullptr;
// very optimized decision! (no)
if (auto* storagesVal = std::get_if<StoragesList>(&selectedStorages))
selectedStoragesPtr = storagesVal;
else if (auto* storagesRef = std::get_if<std::reference_wrapper<StoragesList>>(&selectedStorages))
selectedStoragesPtr = &storagesRef->get();
else
return;

renderStoragesSuggestion(state.prevState.availability,
state.prevState.prevState.selectedStorages,
*selectedStoragesPtr,
state.prevState.addedStorages,
state.prevState.recipeId,
userId,
chatId,
bot,
api);
return;
}
}
}

} // namespace cookcookhnya::handlers::suggested_recipe
} // namespace cookcookhnya::handlers::cooking_planning
11 changes: 11 additions & 0 deletions src/handlers/cooking_planning/add_storage.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include "backend/api/api.hpp"
#include "handlers/common.hpp"

namespace cookcookhnya::handlers::cooking_planning {

void handleCookingPlanningStorageAdditionCQ(
CookingPlanningStorageAddition& state, CallbackQueryRef cq, BotRef bot, SMRef stateManager, api::ApiClientRef api);

} // namespace cookcookhnya::handlers::cooking_planning
Loading
Loading