From 31b24ed4d3b6fb8e21b870a111eb982d4787dddd Mon Sep 17 00:00:00 2001 From: Mateusz Grzegorzek Date: Wed, 10 Mar 2021 17:37:31 +0100 Subject: [PATCH] [EGD-5932] Add more queries to Quotes agent Added queries: - list quotes from selected category, - enable/disable category, - enable/disable single quote, - list enabled quotes, Other fixes: - move `msgHandled` and `msgNotHandled` to `Message.hpp`, - use `typeid` in `QuotesAgent::registerMessages`, - change `Quotes::Queries` style, - make `quotesList` private Queries to add in next PR: - add/read/write quote --- image/user/db/quotes_001.sql | 4 +- module-apps/Application.cpp | 44 +++---- module-apps/Application.hpp | 10 -- .../ApplicationAlarmClock.cpp | 6 +- .../ApplicationCallLog.cpp | 6 +- .../ApplicationDesktop.cpp | 2 +- .../application-notes/ApplicationNotes.cpp | 6 +- .../ApplicationOnBoarding.cpp | 6 +- .../model/ApplicationManager.cpp | 14 +-- .../agents/file_indexer/FileIndexerAgent.cpp | 16 +-- .../service-db/agents/quotes/QuotesAgent.cpp | 55 ++++++++- .../service-db/agents/quotes/QuotesAgent.hpp | 6 +- .../agents/quotes/QuotesQueries.hpp | 44 +++++++ .../service-db/service-db/QuotesMessages.hpp | 114 ++++++++++++++++-- .../test/test-service-db-quotes.cpp | 52 ++++++++ .../test/test-service-db-quotes.hpp | 60 +++++++++ .../service-evtmgr/EventManager.cpp | 2 +- module-sys/Service/Message.hpp | 10 ++ 18 files changed, 379 insertions(+), 78 deletions(-) diff --git a/image/user/db/quotes_001.sql b/image/user/db/quotes_001.sql index 4c5a10cf960bb33be47ce39242718b4dec98578a..4e37f1f9d5e023132063eac199b1c25d07f5bb23 100644 --- a/image/user/db/quotes_001.sql +++ b/image/user/db/quotes_001.sql @@ -10,7 +10,7 @@ CREATE TABLE IF NOT EXISTS quote_languages ( CREATE TABLE IF NOT EXISTS category_table ( category_id INTEGER NOT NULL, category_name TEXT NOT NULL UNIQUE, - enabled BOOLEAN DEFAULT TRUE, + enabled BOOLEAN NOT NULL DEFAULT TRUE, PRIMARY KEY (category_id) ); @@ -19,7 +19,7 @@ CREATE TABLE IF NOT EXISTS quote_table ( lang_id INTEGER NOT NULL, quote TEXT NOT NULL, author TEXT, - enabled BOOLEAN DEFAULT TRUE, + enabled BOOLEAN NOT NULL DEFAULT TRUE, PRIMARY KEY (quote_id), FOREIGN KEY (lang_id) REFERENCES quote_languages(lang_id) ); diff --git a/module-apps/Application.cpp b/module-apps/Application.cpp index c6768cd3f694296ea8fa8ddfd9ebe3a2ead10ec7..84451dbd6235de08c6c897890e6d43039e51ebd8 100644 --- a/module-apps/Application.cpp +++ b/module-apps/Application.cpp @@ -275,10 +275,10 @@ namespace app else if (auto msg = dynamic_cast(msgl)) { if (!msg->muted) { handleVolumePopup(); - return msgHandled(); + return sys::msgHandled(); } } - return msgNotHandled(); + return sys::msgNotHandled(); } sys::MessagePointer Application::handleSignalStrengthUpdate(sys::Message *msgl) @@ -286,7 +286,7 @@ namespace app if ((state == State::ACTIVE_FORGROUND) && getCurrentWindow()->updateSignalStrength()) { refreshWindow(gui::RefreshModes::GUI_REFRESH_FAST); } - return msgHandled(); + return sys::msgHandled(); } sys::MessagePointer Application::handleNetworkAccessTechnologyUpdate(sys::Message *msgl) @@ -294,7 +294,7 @@ namespace app if ((state == State::ACTIVE_FORGROUND) && getCurrentWindow()->updateNetworkAccessTechnology()) { refreshWindow(gui::RefreshModes::GUI_REFRESH_FAST); } - return msgHandled(); + return sys::msgHandled(); } sys::MessagePointer Application::handleInputEvent(sys::Message *msgl) @@ -309,7 +309,7 @@ namespace app if (not windowsStack.isEmpty() && getCurrentWindow()->onInput(msg->getEvent())) { refreshWindow(gui::RefreshModes::GUI_REFRESH_FAST); } - return msgHandled(); + return sys::msgHandled(); } sys::MessagePointer Application::handleKBDKeyEvent(sys::Message *msgl) @@ -322,7 +322,7 @@ namespace app if (iev.keyCode != gui::KeyCode::KEY_UNDEFINED) { messageInputEventApplication(this, this->GetName(), iev); } - return msgHandled(); + return sys::msgHandled(); } sys::MessagePointer Application::handleBatteryStatusChange() @@ -330,7 +330,7 @@ namespace app if ((state == State::ACTIVE_FORGROUND) && getCurrentWindow()->updateBatteryStatus()) { refreshWindow(gui::RefreshModes::GUI_REFRESH_FAST); } - return msgHandled(); + return sys::msgHandled(); } sys::MessagePointer Application::handleMinuteUpdated(sys::Message *msgl) @@ -338,7 +338,7 @@ namespace app if (state == State::ACTIVE_FORGROUND && getCurrentWindow()->updateTime()) { refreshWindow(gui::RefreshModes::GUI_REFRESH_FAST); } - return msgHandled(); + return sys::msgHandled(); } sys::MessagePointer Application::handleAction(sys::Message *msgl) @@ -398,9 +398,9 @@ namespace app stateStr(state)); } if (handled) { - return msgHandled(); + return sys::msgHandled(); } - return msgNotHandled(); + return sys::msgNotHandled(); } sys::MessagePointer Application::handleSwitchWindow(sys::Message *msgl) @@ -428,7 +428,7 @@ namespace app if (text != nullptr) { text->addText(ret->getDescription()); refreshWindow(gui::RefreshModes::GUI_REFRESH_FAST); - return msgHandled(); + return sys::msgHandled(); } } getCurrentWindow()->onBeforeShow(msg->getCommand(), switchData.get()); @@ -437,7 +437,7 @@ namespace app else { LOG_ERROR("No such window: %s", msg->getWindowName().c_str()); } - return msgHandled(); + return sys::msgHandled(); } sys::MessagePointer Application::handleUpdateWindow(sys::Message *msgl) @@ -453,14 +453,14 @@ namespace app else { LOG_ERROR("No such window: %s", msg->getWindowName().c_str()); } - return msgHandled(); + return sys::msgHandled(); } sys::MessagePointer Application::handleAppClose(sys::Message *msgl) { setState(State::DEACTIVATING); app::manager::Controller::confirmClose(this); - return msgHandled(); + return sys::msgHandled(); } sys::MessagePointer Application::handleAppRebuild(sys::Message *msgl) @@ -475,7 +475,7 @@ namespace app refreshWindow(gui::RefreshModes::GUI_REFRESH_DEEP); } LOG_INFO("App rebuild done"); - return msgHandled(); + return sys::msgHandled(); } sys::MessagePointer Application::handleAppRefresh(sys::Message *msgl) @@ -486,29 +486,29 @@ namespace app LOG_DEBUG("Ignore request for window %s we are on window %s", msg->getWindowName().c_str(), windowsStack.isEmpty() ? "none" : getCurrentWindow()->getName().c_str()); - return msgNotHandled(); + return sys::msgNotHandled(); } render(msg->getMode()); - return msgHandled(); + return sys::msgHandled(); } sys::MessagePointer Application::handleGetDOM(sys::Message *msgl) { if (windowsStack.isEmpty()) { LOG_ERROR("Current window is not defined - can't dump DOM"); - return msgNotHandled(); + return sys::msgNotHandled(); } auto window = getCurrentWindow(); if (window == nullptr) { LOG_ERROR("No window - can't dump DOM"); - return msgNotHandled(); + return sys::msgNotHandled(); } auto request = static_cast(msgl); LOG_DEBUG("Get DOM for: %s", request->getSenderName().c_str()); bus.sendUnicast(DOMResponder(GetName(), *window, std::move(request->event)).build(), request->getSenderName()); - return msgHandled(); + return sys::msgHandled(); } sys::MessagePointer Application::handleAppFocusLost(sys::Message *msgl) @@ -517,7 +517,7 @@ namespace app setState(State::ACTIVE_BACKGROUND); app::manager::Controller::confirmSwitch(this); } - return msgHandled(); + return sys::msgHandled(); } sys::MessagePointer Application::handleSIMMessage(sys::Message *msgl) @@ -525,7 +525,7 @@ namespace app if (getCurrentWindow()->updateSim()) { refreshWindow(gui::RefreshModes::GUI_REFRESH_FAST); } - return msgHandled(); + return sys::msgHandled(); } sys::ReturnCodes Application::InitHandler() diff --git a/module-apps/Application.hpp b/module-apps/Application.hpp index df278cfe166540dbf49b5dde5664074ee56d9067..34a10a317ff10165409ee01e2be9da7bdf403762 100644 --- a/module-apps/Application.hpp +++ b/module-apps/Application.hpp @@ -65,16 +65,6 @@ namespace app class Application; class GuiTimer; - inline auto msgHandled() -> sys::MessagePointer - { - return std::make_shared(); - } - - inline auto msgNotHandled() -> sys::MessagePointer - { - return std::make_shared(sys::ReturnCodes::Unresolved); - } - class ActionHandledResponse; // Forward declaration using ActionResult = std::shared_ptr; auto actionHandled() -> ActionResult; diff --git a/module-apps/application-alarm-clock/ApplicationAlarmClock.cpp b/module-apps/application-alarm-clock/ApplicationAlarmClock.cpp index bf6e82d4d402b40f8ca85e02ccf854207dbf1ab0..ce317a062199e44af568ffeda523bcaf74332560 100644 --- a/module-apps/application-alarm-clock/ApplicationAlarmClock.cpp +++ b/module-apps/application-alarm-clock/ApplicationAlarmClock.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md #include "ApplicationAlarmClock.hpp" @@ -56,9 +56,9 @@ namespace app } } if (handled) { - return msgHandled(); + return sys::msgHandled(); } - return msgNotHandled(); + return sys::msgNotHandled(); } sys::ReturnCodes ApplicationAlarmClock::InitHandler() diff --git a/module-apps/application-calllog/ApplicationCallLog.cpp b/module-apps/application-calllog/ApplicationCallLog.cpp index 6685f1bd21f9071a79a869cc643f71ae656b67e7..95e3aef6412d10d24ed2f8634cb56758bacb3232 100644 --- a/module-apps/application-calllog/ApplicationCallLog.cpp +++ b/module-apps/application-calllog/ApplicationCallLog.cpp @@ -51,17 +51,17 @@ namespace app for (auto &[name, window] : windowsStack.windows) { window->onDatabaseMessage(msgl); } - return msgHandled(); + return sys::msgHandled(); } if (resp != nullptr) { if (auto command = callbackStorage->getCallback(resp); command->execute()) { refreshWindow(gui::RefreshModes::GUI_REFRESH_FAST); - return msgHandled(); + return sys::msgHandled(); } } - return msgNotHandled(); + return sys::msgNotHandled(); } // Invoked during initialization diff --git a/module-apps/application-desktop/ApplicationDesktop.cpp b/module-apps/application-desktop/ApplicationDesktop.cpp index 3eafae4aab819e5604fe8d91c15ce73cc21eaf35..c172d9fe7b618eda74092d6a0d0c589344a3a856 100644 --- a/module-apps/application-desktop/ApplicationDesktop.cpp +++ b/module-apps/application-desktop/ApplicationDesktop.cpp @@ -361,7 +361,7 @@ namespace app getWindow(app::window::name::desktop_update)->handleSwitchData(data.get()); } } - return msgHandled(); + return sys::msgHandled(); }); auto msgToSend = diff --git a/module-apps/application-notes/ApplicationNotes.cpp b/module-apps/application-notes/ApplicationNotes.cpp index b7eb97d6dfe487087afc861948ab993dfbd52efe..17635f3ab3ef856d9ba6bea3099e377c8c9575b4 100644 --- a/module-apps/application-notes/ApplicationNotes.cpp +++ b/module-apps/application-notes/ApplicationNotes.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md #include "ApplicationNotes.hpp" @@ -59,9 +59,9 @@ namespace app if (auto command = callbackStorage->getCallback(resp); command->execute()) { refreshWindow(gui::RefreshModes::GUI_REFRESH_FAST); } - return msgHandled(); + return sys::msgHandled(); } - return msgNotHandled(); + return sys::msgNotHandled(); } sys::ReturnCodes ApplicationNotes::InitHandler() diff --git a/module-apps/application-onboarding/ApplicationOnBoarding.cpp b/module-apps/application-onboarding/ApplicationOnBoarding.cpp index a040532bb7ce1d65238a89af4633b1d438933a54..110f76214d82e89addee604ea480447fa0cb9245 100644 --- a/module-apps/application-onboarding/ApplicationOnBoarding.cpp +++ b/module-apps/application-onboarding/ApplicationOnBoarding.cpp @@ -51,7 +51,7 @@ namespace app return retMsg; } - return msgNotHandled(); + return sys::msgNotHandled(); } sys::ReturnCodes ApplicationOnBoarding::InitHandler() @@ -67,10 +67,10 @@ namespace app connect(typeid(manager::GetCurrentDisplayLanguageResponse), [&](sys::Message *msg) { if (gui::window::name::onBoarding_languages == getCurrentWindow()->getName()) { switchWindow(gui::window::name::onBoarding_eula, nullptr); - return msgHandled(); + return sys::msgHandled(); } else { - return msgNotHandled(); + return sys::msgNotHandled(); } }); diff --git a/module-services/service-appmgr/model/ApplicationManager.cpp b/module-services/service-appmgr/model/ApplicationManager.cpp index 80a9ac3715fbfc38510950562ae000546b618c6f..4a28b57094b54342b361080d4efebf1018ae2ce0 100644 --- a/module-services/service-appmgr/model/ApplicationManager.cpp +++ b/module-services/service-appmgr/model/ApplicationManager.cpp @@ -273,27 +273,27 @@ namespace app::manager connect(typeid(InputLanguageChangeRequest), [this](sys::Message *request) { auto msg = static_cast(request); handleInputLanguageChange(msg); - return msgHandled(); + return sys::msgHandled(); }); connect(typeid(AutomaticDateAndTimeIsOnChangeRequest), [this](sys::Message *request) { auto msg = static_cast(request); handleAutomaticDateAndTimeChange(msg); - return msgHandled(); + return sys::msgHandled(); }); connect(typeid(AutomaticTimeZoneIsOnChangeRequest), [this](sys::Message *request) { auto msg = static_cast(request); handleAutomaticTimeZoneChange(msg); - return msgHandled(); + return sys::msgHandled(); }); connect(typeid(TimeFormatChangeRequest), [this](sys::Message *request) { auto msg = static_cast(request); handleTimeFormatChange(msg); - return msgHandled(); + return sys::msgHandled(); }); connect(typeid(DateFormatChangeRequest), [this](sys::Message *request) { auto msg = static_cast(request); handleDateFormatChange(msg); - return msgHandled(); + return sys::msgHandled(); }); connect(typeid(ShutdownRequest), [this](sys::Message *) { closeApplications(); @@ -313,12 +313,12 @@ namespace app::manager }); connect(typeid(UpdateInProgress), [this](sys::Message *) { closeApplicationsOnUpdate(); - return msgHandled(); + return sys::msgHandled(); }); connect(typeid(SetOsUpdateVersion), [this](sys::Message *request) { auto msg = static_cast(request); handleSetOsUpdateVersionChange(msg); - return msgHandled(); + return sys::msgHandled(); }); connect(typeid(app::manager::DOMRequest), [&](sys::Message *request) { return handleDOMRequest(request); }); diff --git a/module-services/service-db/agents/file_indexer/FileIndexerAgent.cpp b/module-services/service-db/agents/file_indexer/FileIndexerAgent.cpp index 3cd3f9feaba1311ebe0abf8cb1541047c28bc16a..df7597d71c6e630247fe7a2587890ebdc696b569 100644 --- a/module-services/service-db/agents/file_indexer/FileIndexerAgent.cpp +++ b/module-services/service-db/agents/file_indexer/FileIndexerAgent.cpp @@ -279,7 +279,7 @@ auto FileIndexerAgent::handleGetRecord(sys::Message *req) -> sys::MessagePointer return std::make_shared( std::make_unique(msg->dbRecord)); } - return app::msgHandled(); + return sys::msgHandled(); } auto FileIndexerAgent::handleSetRecord(sys::Message *req) -> sys::MessagePointer @@ -303,7 +303,7 @@ auto FileIndexerAgent::handleSetRecord(sys::Message *req) -> sys::MessagePointer } } } - return app::msgHandled(); + return sys::msgHandled(); } auto FileIndexerAgent::handleDeleteFile(sys::Message *req) -> sys::MessagePointer @@ -324,7 +324,7 @@ auto FileIndexerAgent::handleDeleteFile(sys::Message *req) -> sys::MessagePointe parentService->bus.sendUnicast(std::move(notifyMsg), recipient); } } - return app::msgHandled(); + return sys::msgHandled(); } auto FileIndexerAgent::handleDeleteAllFilesInDir(sys::Message *req) -> sys::MessagePointer @@ -346,7 +346,7 @@ auto FileIndexerAgent::handleDeleteAllFilesInDir(sys::Message *req) -> sys::Mess parentService->bus.sendUnicast(std::move(notifyMsg), recipient); } } - return app::msgHandled(); + return sys::msgHandled(); } auto FileIndexerAgent::dbGetProperty(std::unique_ptr metaData) -> FileIndexer::FileMetadata @@ -474,7 +474,7 @@ auto FileIndexerAgent::handleGetProperty(sys::Message *req) -> sys::MessagePoint return std::make_shared( std::make_unique(msg->dbMetaData)); } - return app::msgHandled(); + return sys::msgHandled(); } auto FileIndexerAgent::handleGetAllProperties(sys::Message *req) -> sys::MessagePointer @@ -486,7 +486,7 @@ auto FileIndexerAgent::handleGetAllProperties(sys::Message *req) -> sys::Message return std::make_shared( std::make_unique(msg->dbMetaData)); } - return app::msgHandled(); + return sys::msgHandled(); } auto FileIndexerAgent::handleSetProperty(sys::Message *req) -> sys::MessagePointer @@ -509,7 +509,7 @@ auto FileIndexerAgent::handleSetProperty(sys::Message *req) -> sys::MessagePoint dbSetProperty(std::make_unique(metaData)); } } - return app::msgHandled(); + return sys::msgHandled(); } auto FileIndexerAgent::handleSetProperties(sys::Message *req) -> sys::MessagePointer @@ -529,5 +529,5 @@ auto FileIndexerAgent::handleSetProperties(sys::Message *req) -> sys::MessagePoi parentService->bus.sendUnicast(std::move(notifyMsg), recipient); } } - return app::msgHandled(); + return sys::msgHandled(); } diff --git a/module-services/service-db/agents/quotes/QuotesAgent.cpp b/module-services/service-db/agents/quotes/QuotesAgent.cpp index e17de4b8cc82cf2876e16ea68825dc3f19f0469e..68a0cec2d20691468a928157a9255e40594a5c32 100644 --- a/module-services/service-db/agents/quotes/QuotesAgent.cpp +++ b/module-services/service-db/agents/quotes/QuotesAgent.cpp @@ -18,9 +18,18 @@ namespace Quotes { using std::placeholders::_1; - parentService->connect(Messages::GetCategoryListRequest(), + parentService->connect(typeid(Messages::GetCategoryListRequest), std::bind(&QuotesAgent::handleCategoryList, this, _1)); - parentService->connect(Messages::GetQuotesListRequest(), std::bind(&QuotesAgent::handleQuotesList, this, _1)); + parentService->connect(typeid(Messages::GetQuotesListRequest), + std::bind(&QuotesAgent::handleQuotesList, this, _1)); + parentService->connect(typeid(Messages::GetQuotesListByCategoryIdRequest), + std::bind(&QuotesAgent::handleQuotesListByCategoryId, this, _1)); + parentService->connect(typeid(Messages::EnableCategoryByIdRequest), + std::bind(&QuotesAgent::handleEnableCategoryById, this, _1)); + parentService->connect(typeid(Messages::EnableQuoteByIdRequest), + std::bind(&QuotesAgent::handleEnableQuoteById, this, _1)); + parentService->connect(typeid(Messages::GetEnabledQuotesListRequest), + std::bind(&QuotesAgent::handleEnabledQuotesList, this, _1)); } auto QuotesAgent::getDbFilePath() -> const std::string @@ -45,7 +54,7 @@ namespace Quotes auto categoryList = getList(std::move(msg->categoryList), std::move(query)); return std::make_shared(std::move(categoryList)); } - return app::msgNotHandled(); + return sys::msgNotHandled(); } auto QuotesAgent::handleQuotesList(sys::Message *req) -> sys::MessagePointer @@ -55,7 +64,45 @@ namespace Quotes auto quotesList = getList(std::move(msg->quotesList), std::move(query)); return std::make_shared(std::move(quotesList)); } - return app::msgNotHandled(); + return sys::msgNotHandled(); + } + + auto QuotesAgent::handleQuotesListByCategoryId(sys::Message *req) -> sys::MessagePointer + { + if (auto msg = dynamic_cast(req)) { + auto query = database->query(Queries::getQuotesByCategoryId, msg->categoryId); + auto quotesList = getList(std::move(msg->quotesList), std::move(query)); + return std::make_shared(std::move(quotesList)); + } + return sys::msgNotHandled(); + } + + auto QuotesAgent::handleEnableCategoryById(sys::Message *req) -> sys::MessagePointer + { + if (auto msg = dynamic_cast(req)) { + auto result = database->execute(Queries::enableCategory, msg->enable, msg->categoryId); + return std::make_shared(result); + } + return sys::msgNotHandled(); + } + + auto QuotesAgent::handleEnableQuoteById(sys::Message *req) -> sys::MessagePointer + { + if (auto msg = dynamic_cast(req)) { + auto result = database->execute(Queries::enableQuote, msg->enable, msg->quoteId); + return std::make_shared(result); + } + return sys::msgNotHandled(); + } + + auto QuotesAgent::handleEnabledQuotesList(sys::Message *req) -> sys::MessagePointer + { + if (auto msg = dynamic_cast(req)) { + auto query = database->query(Queries::getEnabledQuotes); + auto quotesList = getList(std::move(msg->quotesList), std::move(query)); + return std::make_shared(std::move(quotesList)); + } + return sys::msgNotHandled(); } } // namespace Quotes diff --git a/module-services/service-db/agents/quotes/QuotesAgent.hpp b/module-services/service-db/agents/quotes/QuotesAgent.hpp index 30f8f5c3162cf639bf8465dbb416bc5eed8c8855..93a2bb210b25f666e6aabeb95cb5fb8cb8985b74 100644 --- a/module-services/service-db/agents/quotes/QuotesAgent.hpp +++ b/module-services/service-db/agents/quotes/QuotesAgent.hpp @@ -11,7 +11,7 @@ namespace Quotes class QuotesAgent : public DatabaseAgent { public: - QuotesAgent(sys::Service *parentService); + explicit QuotesAgent(sys::Service *parentService); ~QuotesAgent() = default; void initDb() override @@ -24,6 +24,10 @@ namespace Quotes protected: auto handleCategoryList(sys::Message *req) -> sys::MessagePointer; auto handleQuotesList(sys::Message *req) -> sys::MessagePointer; + auto handleQuotesListByCategoryId(sys::Message *req) -> sys::MessagePointer; + auto handleEnableCategoryById(sys::Message *req) -> sys::MessagePointer; + auto handleEnableQuoteById(sys::Message *req) -> sys::MessagePointer; + auto handleEnabledQuotesList(sys::Message *req) -> sys::MessagePointer; private: auto getDbInitString() -> const std::string override; diff --git a/module-services/service-db/agents/quotes/QuotesQueries.hpp b/module-services/service-db/agents/quotes/QuotesQueries.hpp index 6d953aac7f164224a01bc932352dd71d44f8f3bd..ef106fb6a9f91aad7aec298632eea0f2559dffd6 100644 --- a/module-services/service-db/agents/quotes/QuotesQueries.hpp +++ b/module-services/service-db/agents/quotes/QuotesQueries.hpp @@ -14,4 +14,48 @@ namespace Quotes::Queries SELECT quote_id, lang_id, quote, author, enabled FROM quote_table; )sql"; + + constexpr auto getQuotesByCategoryId = R"sql( + SELECT QT.quote_id, QT.lang_id, QT.quote, QT.author, QT.enabled + FROM + quote_table as QT, + quote_category_map as QCM, + category_table as CT + WHERE + QCM.quote_id = QT.quote_id + and + QCM.category_id = CT.category_id + and + QCM.category_id = '%lu' + and + QT.enabled = TRUE + and + CT.enabled = TRUE + )sql"; + + constexpr auto enableCategory = R"sql( + UPDATE category_table SET enabled = '%d' + WHERE category_id = '%lu'; + )sql"; + + constexpr auto enableQuote = R"sql( + UPDATE quote_table SET enabled = '%d' + WHERE quote_id = '%lu'; + )sql"; + + constexpr auto getEnabledQuotes = R"sql( + SELECT QT.quote_id, QT.lang_id, QT.quote, QT.author, QT.enabled + FROM + quote_table as QT, + quote_category_map as QCM, + category_table as CT + WHERE + QCM.quote_id = QT.quote_id + and + QCM.category_id = CT.category_id + and + QT.enabled = TRUE + and + CT.enabled = TRUE + )sql"; } // namespace Quotes::Queries diff --git a/module-services/service-db/service-db/QuotesMessages.hpp b/module-services/service-db/service-db/QuotesMessages.hpp index 12b7e4b1f94d371cc0500bcd0a49c9b0ead393eb..f29cdc28d91022ed7264822386022cd36027d985 100644 --- a/module-services/service-db/service-db/QuotesMessages.hpp +++ b/module-services/service-db/service-db/QuotesMessages.hpp @@ -45,11 +45,6 @@ namespace Quotes std::string author; bool enabled; - [[nodiscard]] auto RecordEqual(QuoteRecord qr) const noexcept -> bool - { - return qr.lang_id == lang_id && qr.quote == quote; - } - QuoteRecord() = default; explicit QuoteRecord(QueryResult *query) @@ -77,7 +72,6 @@ namespace Quotes class GetCategoryListRequest : public QuotesMessage { public: - GetCategoryListRequest() = default; explicit GetCategoryListRequest(std::unique_ptr categoryList) : QuotesMessage(), categoryList(std::move(categoryList)) {} @@ -91,7 +85,6 @@ namespace Quotes sys::ReturnCodes code = sys::ReturnCodes::Success) : sys::ResponseMessage(code), categoryList(std::move(categoryList)) {} - std::unique_ptr categoryList; [[nodiscard]] unsigned int getCount() const noexcept { @@ -102,14 +95,15 @@ namespace Quotes { return categoryList->data; } + + private: + std::unique_ptr categoryList; }; class GetQuotesListRequest : public QuotesMessage { public: - GetQuotesListRequest() = default; - explicit GetQuotesListRequest(std::unique_ptr quotesList) - : QuotesMessage(), quotesList(std::move(quotesList)) + explicit GetQuotesListRequest(std::unique_ptr quotesList) : quotesList(std::move(quotesList)) {} std::unique_ptr quotesList; }; @@ -121,7 +115,38 @@ namespace Quotes sys::ReturnCodes code = sys::ReturnCodes::Success) : sys::ResponseMessage(code), quotesList(std::move(quotesList)) {} + + [[nodiscard]] unsigned int getCount() const noexcept + { + return quotesList->count; + } + + [[nodiscard]] auto getResults() const -> std::vector + { + return quotesList->data; + } + + private: + std::unique_ptr quotesList; + }; + + class GetQuotesListByCategoryIdRequest : public QuotesMessage + { + public: + explicit GetQuotesListByCategoryIdRequest(std::unique_ptr quotesList, unsigned int categoryId) + : quotesList(std::move(quotesList)), categoryId(categoryId) + {} std::unique_ptr quotesList; + const unsigned int categoryId; + }; + + class GetQuotesListByCategoryIdResponse : public sys::ResponseMessage + { + public: + explicit GetQuotesListByCategoryIdResponse(std::unique_ptr quotesList, + sys::ReturnCodes code = sys::ReturnCodes::Success) + : sys::ResponseMessage(code), quotesList(std::move(quotesList)) + {} [[nodiscard]] unsigned int getCount() const noexcept { @@ -132,6 +157,75 @@ namespace Quotes { return quotesList->data; } + + private: + std::unique_ptr quotesList; + }; + + class EnableCategoryByIdRequest : public QuotesMessage + { + public: + explicit EnableCategoryByIdRequest(unsigned int categoryId, bool enable) + : categoryId(categoryId), enable(enable) + {} + const unsigned int categoryId; + const bool enable; + }; + + class EnableCategoryByIdResponse : public QuotesMessage + { + public: + explicit EnableCategoryByIdResponse(bool success) : success(success) + {} + const bool success; + }; + + class EnableQuoteByIdRequest : public QuotesMessage + { + public: + explicit EnableQuoteByIdRequest(unsigned int quoteId, bool enable) : quoteId(quoteId), enable(enable) + {} + const unsigned int quoteId; + const bool enable; + }; + + class EnableQuoteByIdResponse : public QuotesMessage + { + public: + explicit EnableQuoteByIdResponse(bool success) : success(success) + {} + const bool success; + }; + + class GetEnabledQuotesListRequest : public QuotesMessage + { + public: + explicit GetEnabledQuotesListRequest(std::unique_ptr quotesList) + : quotesList(std::move(quotesList)) + {} + std::unique_ptr quotesList; + }; + + class GetEnabledQuotesListResponse : public sys::ResponseMessage + { + public: + explicit GetEnabledQuotesListResponse(std::unique_ptr quotesList, + sys::ReturnCodes code = sys::ReturnCodes::Success) + : sys::ResponseMessage(code), quotesList(std::move(quotesList)) + {} + + [[nodiscard]] unsigned int getCount() const noexcept + { + return quotesList->count; + } + + [[nodiscard]] auto getResults() const -> std::vector + { + return quotesList->data; + } + + private: + std::unique_ptr quotesList; }; } // namespace Messages } // namespace Quotes diff --git a/module-services/service-db/test/test-service-db-quotes.cpp b/module-services/service-db/test/test-service-db-quotes.cpp index e4760f4d319eeac4cab721fd2af27c20fb594d76..8786bf27bbc724fe10f5f5a8926a5e85a6575029 100644 --- a/module-services/service-db/test/test-service-db-quotes.cpp +++ b/module-services/service-db/test/test-service-db-quotes.cpp @@ -9,6 +9,7 @@ using namespace Quotes; constexpr auto totalNumOfCategoriesInDb = 6; constexpr auto totalNumOfQuotesInDb = 48; +constexpr auto numOfQuotesWithCategoryIdEqualToOne = 9; TEST_CASE("Quotes") { @@ -74,5 +75,56 @@ TEST_CASE("Quotes") REQUIRE(response->getResults().size() == totalNumOfQuotesInDb); } + SECTION("Get quotes by category id") + { + const unsigned int categoryId = 1; + auto quotes = tester->getQuotesByCategoryId(categoryId); + REQUIRE(quotes.size() == numOfQuotesWithCategoryIdEqualToOne); + } + + SECTION("Enable category by id") + { + bool enable = false; + const unsigned int categoryId = 1; + + auto response = tester->enableCategory(categoryId, enable); + REQUIRE(response->success); + + // Quotes in category one should be disabled + auto quotes = tester->getQuotesByCategoryId(categoryId); + REQUIRE(quotes.size() == 0); + + enable = true; + + response = tester->enableCategory(categoryId, enable); + REQUIRE(response->success); + + // Quotes in category one should be enabled + quotes = tester->getQuotesByCategoryId(categoryId); + REQUIRE(quotes.size() == numOfQuotesWithCategoryIdEqualToOne); + } + + SECTION("Enable quote by id") + { + bool enable = false; + const unsigned int quoteId = 1; + + auto response = tester->enableQuote(quoteId, enable); + REQUIRE(response->success); + + // All quotes except quote with id=1 should be enabled + auto quotes = tester->getEnabledQuotes(); + REQUIRE(quotes.size() == totalNumOfQuotesInDb - 1); + + enable = true; + + response = tester->enableQuote(quoteId, enable); + REQUIRE(response->success); + + // All quotes should be enabled + quotes = tester->getEnabledQuotes(); + REQUIRE(quotes.size() == totalNumOfQuotesInDb); + } + Database::deinitialize(); } diff --git a/module-services/service-db/test/test-service-db-quotes.hpp b/module-services/service-db/test/test-service-db-quotes.hpp index cb9cffe6a3489e66fc2fec4925295bbbca3483f1..7688fcccd1bdf4f431ad8cce6ab3755dcbd77b7e 100644 --- a/module-services/service-db/test/test-service-db-quotes.hpp +++ b/module-services/service-db/test/test-service-db-quotes.hpp @@ -23,5 +23,65 @@ namespace Quotes { return QuotesAgent::handleQuotesList(req); } + + auto handleQuotesListByCategoryId(Messages::GetQuotesListByCategoryIdRequest *req) -> sys::MessagePointer + { + return QuotesAgent::handleQuotesListByCategoryId(req); + } + + auto handleEnableCategoryById(Messages::EnableCategoryByIdRequest *req) -> sys::MessagePointer + { + return QuotesAgent::handleEnableCategoryById(req); + } + + auto handleEnableQuoteById(Messages::EnableQuoteByIdRequest *req) -> sys::MessagePointer + { + return QuotesAgent::handleEnableQuoteById(req); + } + + auto handleEnabledQuotesList(Messages::GetEnabledQuotesListRequest *req) -> sys::MessagePointer + { + return QuotesAgent::handleEnabledQuotesList(req); + } + + auto getQuotesByCategoryId(unsigned int categoryId) -> std::vector + { + QuotesList quotesList; + quotesList.limit = 0; + + auto record = std::make_unique(quotesList); + auto request = std::make_shared(std::move(record), categoryId); + auto response = std::dynamic_pointer_cast( + handleQuotesListByCategoryId(request.get())); + + return response->getResults(); + } + + auto getEnabledQuotes() -> std::vector + { + QuotesList quotesList; + quotesList.limit = 0; + + auto record = std::make_unique(quotesList); + auto request = std::make_shared(std::move(record)); + auto response = std::dynamic_pointer_cast( + handleEnabledQuotesList(request.get())); + + return response->getResults(); + } + + auto enableCategory(unsigned int categoryId, bool enable) + -> std::shared_ptr + { + auto request = std::make_shared(categoryId, enable); + return std::dynamic_pointer_cast( + handleEnableCategoryById(request.get())); + } + + auto enableQuote(unsigned int quoteId, bool enable) -> std::shared_ptr + { + auto request = std::make_shared(quoteId, enable); + return std::dynamic_pointer_cast(handleEnableQuoteById(request.get())); + } }; } // namespace Quotes diff --git a/module-services/service-evtmgr/EventManager.cpp b/module-services/service-evtmgr/EventManager.cpp index 9b70a7579b4b028cfad21d13976c0d9c8cccf04d..2a8204aa4b41e1c583b68cda9f96d39ee5226850 100644 --- a/module-services/service-evtmgr/EventManager.cpp +++ b/module-services/service-evtmgr/EventManager.cpp @@ -269,7 +269,7 @@ sys::ReturnCodes EventManager::InitHandler() bsp::rtc_SetDateTimeFromTimestamp(msg->getTime()); bsp::rtc_SetMinuteAlarm(msg->getTime()); handleMinuteUpdate(msg->getTime()); - return app::msgHandled(); + return sys::msgHandled(); }); connect(sevm::BatteryStatusChangeMessage(), [&](sys::Message *msgl) { diff --git a/module-sys/Service/Message.hpp b/module-sys/Service/Message.hpp index 1176c9c37f90c85cf2f3d2ef12cc40fee3d0d3c5..e1875cb7312eec340a6162f0fc6a0ad0bfc0de6b 100644 --- a/module-sys/Service/Message.hpp +++ b/module-sys/Service/Message.hpp @@ -108,4 +108,14 @@ namespace sys ReturnCodes retCode; MessageType responseTo; }; + + inline auto msgHandled() -> MessagePointer + { + return std::make_shared(); + } + + inline auto msgNotHandled() -> MessagePointer + { + return std::make_shared(ReturnCodes::Unresolved); + } } // namespace sys