~aleteoryx/muditaos

6b45d3b71dc1e09a16a3bc66cc54ced1c9de8d43 — Maciej Gibowicz 9 months ago eda9260
[BH-2097] Adding and deleting a single custom quote

When deleting quotes, if the database is already empty, we switch to
predefined quotes.
M products/BellHybrid/services/db/agents/QuotesAgent.cpp => products/BellHybrid/services/db/agents/QuotesAgent.cpp +18 -11
@@ 42,6 42,15 @@ namespace Quotes
        return nullptr;
    }

    auto QuotesAgent::isCustomQuotesDatabaseEmpty() -> bool
    {
        const auto result = quotesDB->query(Queries::getCustomQuotesCount);
        if (result == nullptr || result->getRowCount() == 0) {
            return true;
        }
        return ((*result)[0].getUInt32() == 0);
    }

    auto QuotesAgent::handleReadRandomizedQuote(std::shared_ptr<db::Query> query) -> std::unique_ptr<db::QueryResult>
    {
        const auto request = std::dynamic_pointer_cast<Messages::ReadRandomizedQuoteRequest>(query);


@@ 121,6 130,10 @@ namespace Quotes
        const auto executeResult = quotesDB->execute(Queries::deleteCustomQuote, request->quoteId);

        if (executeResult && quotesGroup == customGroup) {
            if (isCustomQuotesDatabaseEmpty()) {
                // if the list of custom quotes is empty, we change the group to a predefined one
                settings->setValue(settings::Quotes::selectedGroup, predefinedGroup, settings::SettingsScope::Global);
            }
            shuffleQuoteModel.updateList(ListUpdateMode::Forced);
        }



@@ 135,17 148,11 @@ namespace Quotes
        if (request == nullptr) {
            return std::make_unique<Messages::NotificationResult>(false);
        }
        if (request->group == customGroup) {
            const auto result = quotesDB->query(Queries::getCustomQuotesCount);
            if (result == nullptr || result->getRowCount() == 0) {
                return std::make_unique<Messages::NotificationResult>(false);
            }
            if ((*result)[0].getUInt32() == 0) {
                // if the list of custom quotes is empty, we change the group to a predefined one
                settings->setValue(settings::Quotes::selectedGroup, predefinedGroup, settings::SettingsScope::Global);
                shuffleQuoteModel.updateList(ListUpdateMode::Forced);
                return std::make_unique<Messages::NotificationResult>(true);
            }
        if (request->group == customGroup && isCustomQuotesDatabaseEmpty()) {
            // if the list of custom quotes is empty, we change the group to a predefined one
            settings->setValue(settings::Quotes::selectedGroup, predefinedGroup, settings::SettingsScope::Global);
            shuffleQuoteModel.updateList(ListUpdateMode::Forced);
            return std::make_unique<Messages::NotificationResult>(true);
        }

        settings->setValue(settings::Quotes::selectedGroup, request->group, settings::SettingsScope::Global);

M products/BellHybrid/services/db/agents/QuotesAgent.hpp => products/BellHybrid/services/db/agents/QuotesAgent.hpp +1 -0
@@ 30,6 30,7 @@ namespace Quotes
        Database *quotesDB;
        ShuffleQuoteModel shuffleQuoteModel;

        auto isCustomQuotesDatabaseEmpty() -> bool;
        auto handleAddNewEntry(std::shared_ptr<db::Query> query) -> std::unique_ptr<db::QueryResult>;
        auto handleEditEntry(std::shared_ptr<db::Query> query) -> std::unique_ptr<db::QueryResult>;
        auto handleDeleteEntry(std::shared_ptr<db::Query> query) -> std::unique_ptr<db::QueryResult>;