From 790c746ae1362439570525dfd81f85d4e46f0f2d Mon Sep 17 00:00:00 2001 From: Maciej Gibowicz Date: Mon, 27 Jan 2025 10:07:34 +0100 Subject: [PATCH] [BH-2096] Add quote group setting Adding a new endpoint for communication with Center; Modification of randomization algorithm from a selected group of quotes. --- module-services/service-db/DBServiceAPI.cpp | 8 +++- .../agents/settings/SystemSettings.hpp | 3 +- .../include/service-db/DBServiceAPI.hpp | 3 +- .../include/service-db/QuotesMessages.hpp | 28 ++++++++++- .../include/endpoints/EndpointType.hpp | 5 +- .../include/endpoints/JsonKeyNames.hpp | 19 +++++++- .../HomeScreenLayoutClassicWithQuotes.cpp | 6 ++- .../services/db/agents/QuotesAgent.cpp | 40 ++++++++++++++-- .../services/db/agents/QuotesAgent.hpp | 7 ++- .../services/db/agents/QuotesQueries.hpp | 14 ++++-- .../services/db/agents/ShuffleQuoteModel.cpp | 31 ++++++------ .../services/db/agents/ShuffleQuoteModel.hpp | 17 ++++--- .../91a948e7_Add_quote_group_setting/.meta | 6 +++ .../91a948e7_Add_quote_group_setting/down.sql | 10 ++++ .../91a948e7_Add_quote_group_setting/up.sql | 11 +++++ .../services/desktop/endpoints/CMakeLists.txt | 4 ++ .../desktop/endpoints/EndpointFactoryBell.cpp | 5 +- .../endpoints/quotes/QuotesEndpoint.hpp | 21 +++++++++ .../include/endpoints/quotes/QuotesHelper.hpp | 21 +++++++++ .../endpoints/quotes/QuotesEndpoint.cpp | 38 +++++++++++++++ .../desktop/endpoints/quotes/QuotesHelper.cpp | 47 +++++++++++++++++++ 21 files changed, 304 insertions(+), 40 deletions(-) create mode 100644 products/BellHybrid/services/db/databases/migration/settings_bell/current/91a948e7_Add_quote_group_setting/.meta create mode 100644 products/BellHybrid/services/db/databases/migration/settings_bell/current/91a948e7_Add_quote_group_setting/down.sql create mode 100644 products/BellHybrid/services/db/databases/migration/settings_bell/current/91a948e7_Add_quote_group_setting/up.sql create mode 100644 products/BellHybrid/services/desktop/endpoints/include/endpoints/quotes/QuotesEndpoint.hpp create mode 100644 products/BellHybrid/services/desktop/endpoints/include/endpoints/quotes/QuotesHelper.hpp create mode 100644 products/BellHybrid/services/desktop/endpoints/quotes/QuotesEndpoint.cpp create mode 100644 products/BellHybrid/services/desktop/endpoints/quotes/QuotesHelper.cpp diff --git a/module-services/service-db/DBServiceAPI.cpp b/module-services/service-db/DBServiceAPI.cpp index 993d77fc0095e2e3c93853781c0f05315c90fb64..5322994486768723a643913b477f55fcbecbc635 100644 --- a/module-services/service-db/DBServiceAPI.cpp +++ b/module-services/service-db/DBServiceAPI.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2025, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md #include "service-db/DBServiceAPI.hpp" @@ -317,6 +317,12 @@ void DBServiceAPI::InformDateChanged(sys::Service *serv) DBServiceAPI::GetQuery(serv, db::Interface::Name::Quotes, std::move(query)); } +void DBServiceAPI::QuotesGroupChanged(sys::Service *serv, const std::string &group) +{ + auto query = std::make_unique(group); + DBServiceAPI::GetQuery(serv, db::Interface::Name::Quotes, std::move(query)); +} + auto DBServiceAPI::hasContactSameNumbers(sys::Service *serv, const ContactRecord &rec) -> bool { std::shared_ptr msg = diff --git a/module-services/service-db/agents/settings/SystemSettings.hpp b/module-services/service-db/agents/settings/SystemSettings.hpp index 00a98e3aa0e108d59eb330f2052c46b17acb6706..d942c5ba9360ef75fea67115daf470ad7cf45c35 100644 --- a/module-services/service-db/agents/settings/SystemSettings.hpp +++ b/module-services/service-db/agents/settings/SystemSettings.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2025, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md #pragma once @@ -81,6 +81,7 @@ namespace settings { inline constexpr auto randomQuotesList = "quotes_random_list"; inline constexpr auto randomQuoteIDUpdateTime = "quotes_random_id_update_time"; + inline constexpr auto selectedGroup = "quotes_group"; } // namespace Quotes namespace Display diff --git a/module-services/service-db/include/service-db/DBServiceAPI.hpp b/module-services/service-db/include/service-db/DBServiceAPI.hpp index 3b57cc49ae0a55a7d9ee011078f628dfc3827b44..d9069b961e4fa27ebe11db8cf6f32cc6730fa7c8 100644 --- a/module-services/service-db/include/service-db/DBServiceAPI.hpp +++ b/module-services/service-db/include/service-db/DBServiceAPI.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2025, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md #pragma once @@ -128,4 +128,5 @@ class DBServiceAPI static void InformLanguageChanged(sys::Service *serv); static void InformDateChanged(sys::Service *serv); + static void QuotesGroupChanged(sys::Service *serv, const std::string &group); }; diff --git a/module-services/service-db/include/service-db/QuotesMessages.hpp b/module-services/service-db/include/service-db/QuotesMessages.hpp index aeb3a2ff0655985731ea1851ce51ef8e3e9bbbdb..f9fa8b02b6c5db70dc079d7a44f9dba19ddccd3a 100644 --- a/module-services/service-db/include/service-db/QuotesMessages.hpp +++ b/module-services/service-db/include/service-db/QuotesMessages.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2025, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md #pragma once @@ -337,6 +337,19 @@ namespace Quotes } }; + class NotificationResult : public db::QueryResult + { + public: + explicit NotificationResult(bool ret) + {} + bool ret; + + [[nodiscard]] auto debugInfo() const -> std::string + { + return "NotificationResult"; + } + }; + class InformLanguageChangeRequest : public db::Query { public: @@ -361,6 +374,19 @@ namespace Quotes } }; + class InformGroupChanged : public db::Query + { + public: + explicit InformGroupChanged(const std::string &group) : Query(Query::Type::Read), group(group) + {} + const std::string group; + + auto debugInfo() const -> std::string + { + return "InformGroupChanged"; + } + }; + class WriteQuoteRequest : public db::Query { public: diff --git a/module-services/service-desktop/endpoints/include/endpoints/EndpointType.hpp b/module-services/service-desktop/endpoints/include/endpoints/EndpointType.hpp index 6ecf072d94f5dcc916746b1ef65fc146f010c202..21a2dadd10309fdf4259a57b41d5feac395d274e 100644 --- a/module-services/service-desktop/endpoints/include/endpoints/EndpointType.hpp +++ b/module-services/service-desktop/endpoints/include/endpoints/EndpointType.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2025, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md #pragma once @@ -25,7 +25,8 @@ namespace sdesktop::endpoints UsbSecurity, Outbox, Reboot, - TimeSync + TimeSync, + Quotes }; inline constexpr auto lastEndpoint = magic_enum::enum_count() - 1; diff --git a/module-services/service-desktop/endpoints/include/endpoints/JsonKeyNames.hpp b/module-services/service-desktop/endpoints/include/endpoints/JsonKeyNames.hpp index 02aa071c285517affa03635a29f32d001ca989ef..e887094b6ce085ef34f3bd3bab3ca86ac34ac37c 100644 --- a/module-services/service-desktop/endpoints/include/endpoints/JsonKeyNames.hpp +++ b/module-services/service-desktop/endpoints/include/endpoints/JsonKeyNames.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2025, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md #pragma once @@ -123,4 +123,21 @@ namespace sdesktop::endpoints::json inline constexpr auto value = "value"; inline constexpr auto timestamp = "timestamp"; } + + namespace quotes + { + namespace settings + { + inline constexpr auto group = "group"; + inline constexpr auto interval = "interval"; + } // namespace settings + + inline constexpr auto quotation = "quotation"; + namespace quotations + { + inline constexpr auto text = "text"; + inline constexpr auto author = "author"; + } // namespace quotations + } // namespace quotes + } // namespace sdesktop::endpoints::json diff --git a/products/BellHybrid/apps/common/src/layouts/HomeScreenLayoutClassicWithQuotes.cpp b/products/BellHybrid/apps/common/src/layouts/HomeScreenLayoutClassicWithQuotes.cpp index 71778116c2543f6dfb91e0e368996d0983bebdcc..4d609fca532993a059e9858bd09f12dc21359fef 100644 --- a/products/BellHybrid/apps/common/src/layouts/HomeScreenLayoutClassicWithQuotes.cpp +++ b/products/BellHybrid/apps/common/src/layouts/HomeScreenLayoutClassicWithQuotes.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2025, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md #include "layouts/HomeScreenLayoutClassicWithQuotes.hpp" @@ -137,7 +137,9 @@ namespace gui { constexpr auto dash{'-'}; UTF8 authorWithDash = quoteAuthor; - authorWithDash.insert(&dash, 0); + if (!quoteAuthor.empty()) { + authorWithDash.insert(&dash, 0); + } author->setText(authorWithDash); quotes->setText(quoteContent); } diff --git a/products/BellHybrid/services/db/agents/QuotesAgent.cpp b/products/BellHybrid/services/db/agents/QuotesAgent.cpp index 9ff5233530e9394b10b65247987ea2a0462dfe05..af01b8b019ccd8156b3b4a376887b8cf120dd230 100644 --- a/products/BellHybrid/services/db/agents/QuotesAgent.cpp +++ b/products/BellHybrid/services/db/agents/QuotesAgent.cpp @@ -1,14 +1,15 @@ -// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2025, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md #include "QuotesAgent.hpp" #include "QuotesQueries.hpp" #include +#include namespace Quotes { QuotesAgent::QuotesAgent(Database *quotesDB, std::unique_ptr settings) - : quotesDB(quotesDB), shuffleQuoteModel(std::move(settings), quotesDB) + : settings(std::move(settings)), quotesDB(quotesDB), shuffleQuoteModel(*this->settings, quotesDB) { shuffleQuoteModel.updateList(ListUpdateMode::Normal); } @@ -21,6 +22,10 @@ namespace Quotes else if (typeid(*query) == typeid(Messages::InformLanguageChangeRequest) || (typeid(*query) == typeid(Messages::InformDateChanged))) { shuffleQuoteModel.updateList(ListUpdateMode::Forced); + return std::make_unique(true); + } + else if (typeid(*query) == typeid(Messages::InformGroupChanged)) { + return handleGroupChanged(query); } return nullptr; } @@ -30,9 +35,12 @@ namespace Quotes const auto request = std::dynamic_pointer_cast(query); if (request) { const auto id = shuffleQuoteModel.getId(); + const auto quotesGroup = + settings->getValue(settings::Quotes::selectedGroup, settings::SettingsScope::Global); LOG_DEBUG("Shuffle id: %d", id); - const auto result = quotesDB->query(Queries::readQuote, id); - if (result->getRowCount() > 0) { + const auto readQuery = quotesGroup == customGroup ? Queries::readCustomQuote : Queries::readPredefinedQuote; + const auto result = quotesDB->query(readQuery, id); + if (result && result->getRowCount() > 0) { const IdQuoteAuthorRecord record(result.get()); auto response = std::make_unique( record.quote_id, record.quote, record.author); @@ -42,4 +50,28 @@ namespace Quotes } return nullptr; } + + auto QuotesAgent::handleGroupChanged(std::shared_ptr query) -> std::unique_ptr + { + const auto request = std::dynamic_pointer_cast(query); + if (request == nullptr) { + return std::make_unique(false); + } + if (request->group == customGroup) { + const auto result = quotesDB->query(Queries::getCustomQuotesCount); + if (result == nullptr || result->getRowCount() == 0) { + return std::make_unique(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(true); + } + } + + settings->setValue(settings::Quotes::selectedGroup, request->group, settings::SettingsScope::Global); + shuffleQuoteModel.updateList(ListUpdateMode::Forced); + return std::make_unique(true); + } } // namespace Quotes diff --git a/products/BellHybrid/services/db/agents/QuotesAgent.hpp b/products/BellHybrid/services/db/agents/QuotesAgent.hpp index 267e87ed3fe3cdd41362b07b455424c8e972bae3..139c7bf66bcea5a667b6d1ec5b4913aaa6d57bae 100644 --- a/products/BellHybrid/services/db/agents/QuotesAgent.hpp +++ b/products/BellHybrid/services/db/agents/QuotesAgent.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2025, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md #pragma once @@ -17,7 +17,7 @@ namespace Quotes class QuotesAgent : public RecordInterface { public: - explicit QuotesAgent(Database *quotesDB, std::unique_ptr settings); + QuotesAgent(Database *quotesDB, std::unique_ptr settings); ~QuotesAgent() = default; auto runQuery(std::shared_ptr query) -> std::unique_ptr; @@ -26,7 +26,10 @@ namespace Quotes auto handleReadRandomizedQuote(std::shared_ptr query) -> std::unique_ptr; private: + std::unique_ptr settings; Database *quotesDB; ShuffleQuoteModel shuffleQuoteModel; + + auto handleGroupChanged(std::shared_ptr query) -> std::unique_ptr; }; } // namespace Quotes diff --git a/products/BellHybrid/services/db/agents/QuotesQueries.hpp b/products/BellHybrid/services/db/agents/QuotesQueries.hpp index b775765cf0610f69f366993fdf46f7b48cc384ea..d72171906e8d40411a577475140a9cf90e30332c 100644 --- a/products/BellHybrid/services/db/agents/QuotesQueries.hpp +++ b/products/BellHybrid/services/db/agents/QuotesQueries.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2025, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md #pragma once @@ -7,9 +7,17 @@ namespace Quotes::Queries { - inline constexpr auto getQuotes = + inline constexpr auto getPredefinedQuotes = "SELECT QT.quote_id FROM quote_table as QT, quote_languages as QL WHERE QL.lang_name=" str_ " and QT.quote_lang=QL.lang_id GROUP BY QT.quote_id;"; - inline constexpr auto readQuote = "SELECT quote_id, quote, author FROM quote_table WHERE quote_id=" u32_ ";"; + inline constexpr auto readPredefinedQuote = + "SELECT quote_id, quote, author FROM quote_table WHERE quote_id=" u32_ ";"; + + inline constexpr auto getCustomQuotes = "SELECT quote_id FROM custom_quote_table;"; + + inline constexpr auto readCustomQuote = + "SELECT quote_id, quote, author FROM custom_quote_table WHERE quote_id=" u32_ ";"; + + inline constexpr auto getCustomQuotesCount = "SELECT COUNT(quote_id) FROM custom_quote_table;"; } // namespace Quotes::Queries diff --git a/products/BellHybrid/services/db/agents/ShuffleQuoteModel.cpp b/products/BellHybrid/services/db/agents/ShuffleQuoteModel.cpp index dcb2a1092d0e251745d97f769ceb5b749d53b05d..badec75f57a922ef5215c8107df78e84ba372e0c 100644 --- a/products/BellHybrid/services/db/agents/ShuffleQuoteModel.cpp +++ b/products/BellHybrid/services/db/agents/ShuffleQuoteModel.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2025, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md #include "ShuffleQuoteModel.hpp" @@ -32,16 +32,19 @@ namespace namespace Quotes { - ShuffleQuoteModel::ShuffleQuoteModel(std::unique_ptr settings, Database *quotesDB) - : settings(std::move(settings)), quotesDB(quotesDB), + ShuffleQuoteModel::ShuffleQuoteModel(settings::Settings &settings, Database *quotesDB) + : settings(settings), quotesDB(quotesDB), serializer(std::make_unique()), rngEngine{std::make_unique( bsp::trng::getRandomValue())} {} void ShuffleQuoteModel::updateList(ListUpdateMode updateMode) { - auto queryResult = quotesDB->query(Queries::getQuotes, utils::getDisplayLanguage().c_str()); - auto quotesList = getList(zeroOffset, maxLimit, std::move(queryResult)); + const auto quotesGroup = settings.getValue(settings::Quotes::selectedGroup, settings::SettingsScope::Global); + auto queryResult = quotesGroup == customGroup + ? quotesDB->query(Queries::getCustomQuotes) + : quotesDB->query(Queries::getPredefinedQuotes, utils::getDisplayLanguage().c_str()); + auto quotesList = getList(zeroOffset, maxLimit, std::move(queryResult)); populateList(std::move(quotesList), updateMode); } @@ -54,12 +57,12 @@ namespace Quotes std::shuffle(std::begin(list), std::end(list), *rngEngine); if (updateMode == ListUpdateMode::Normal) { - auto settingsList = settings->getValue(settings::Quotes::randomQuotesList, settings::SettingsScope::Global); + auto settingsList = settings.getValue(settings::Quotes::randomQuotesList, settings::SettingsScope::Global); if (not settingsList.empty()) { return; } } - settings->setValue( + settings.setValue( settings::Quotes::randomQuotesList, serializer->serialize(list), settings::SettingsScope::Global); LOG_DEBUG("*** ID queue length: %zu", list.size()); } @@ -71,7 +74,7 @@ namespace Quotes } auto list = serializer->deserialize( - settings->getValue(settings::Quotes::randomQuotesList, settings::SettingsScope::Global)); + settings.getValue(settings::Quotes::randomQuotesList, settings::SettingsScope::Global)); const auto [_, id] = list.front(); return id; } @@ -79,7 +82,7 @@ namespace Quotes void ShuffleQuoteModel::shiftIdList() { auto list = serializer->deserialize( - settings->getValue(settings::Quotes::randomQuotesList, settings::SettingsScope::Global)); + settings.getValue(settings::Quotes::randomQuotesList, settings::SettingsScope::Global)); if (!list.empty()) { list.pop_front(); @@ -89,7 +92,7 @@ namespace Quotes updateList(ListUpdateMode::Forced); } else { - settings->setValue( + settings.setValue( settings::Quotes::randomQuotesList, serializer->serialize(list), settings::SettingsScope::Global); } } @@ -98,7 +101,7 @@ namespace Quotes { std::time_t lastTimestamp; const auto lastTimestampString = - settings->getValue(settings::Quotes::randomQuoteIDUpdateTime, settings::SettingsScope::Global); + settings.getValue(settings::Quotes::randomQuoteIDUpdateTime, settings::SettingsScope::Global); try { lastTimestamp = std::stoll(lastTimestampString); @@ -110,9 +113,9 @@ namespace Quotes const auto currentTimestamp = std::time(nullptr); if (hasCrossedMidnight(currentTimestamp, lastTimestamp)) { - settings->setValue(settings::Quotes::randomQuoteIDUpdateTime, - utils::to_string(currentTimestamp), - settings::SettingsScope::Global); + settings.setValue(settings::Quotes::randomQuoteIDUpdateTime, + utils::to_string(currentTimestamp), + settings::SettingsScope::Global); return true; } return false; diff --git a/products/BellHybrid/services/db/agents/ShuffleQuoteModel.hpp b/products/BellHybrid/services/db/agents/ShuffleQuoteModel.hpp index e440bd10376ebcc757f6d49d55a0cf3a7ca6e2be..f34bc6e7b39989e680423bb05a96a56e049453a7 100644 --- a/products/BellHybrid/services/db/agents/ShuffleQuoteModel.hpp +++ b/products/BellHybrid/services/db/agents/ShuffleQuoteModel.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2025, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md #pragma once @@ -10,6 +10,9 @@ namespace Quotes { + inline constexpr auto customGroup = "Custom"; + inline constexpr auto predefinedGroup = "Predefined"; + enum class ListUpdateMode { Normal, @@ -18,9 +21,14 @@ namespace Quotes class ShuffleQuoteModel { + public: + ShuffleQuoteModel(settings::Settings &settings, Database *quotesDB); + auto updateList(ListUpdateMode updateMode) -> void; + [[nodiscard]] auto getId() -> int; + private: app::ApplicationCommon *app{nullptr}; - std::unique_ptr settings; + settings::Settings &settings; Database *quotesDB{nullptr}; std::unique_ptr serializer; std::unique_ptr rngEngine; @@ -28,11 +36,6 @@ namespace Quotes auto populateList(std::unique_ptr idsList, ListUpdateMode updateMode) -> void; auto shiftIdList() -> void; auto isQuoteExpired() -> bool; - - public: - ShuffleQuoteModel(std::unique_ptr settings, Database *quotesDB); - auto updateList(ListUpdateMode updateMode) -> void; - [[nodiscard]] auto getId() -> int; }; } // namespace Quotes diff --git a/products/BellHybrid/services/db/databases/migration/settings_bell/current/91a948e7_Add_quote_group_setting/.meta b/products/BellHybrid/services/db/databases/migration/settings_bell/current/91a948e7_Add_quote_group_setting/.meta new file mode 100644 index 0000000000000000000000000000000000000000..a21fa27be2aebe4b89b71c0e06f3e753788f519e --- /dev/null +++ b/products/BellHybrid/services/db/databases/migration/settings_bell/current/91a948e7_Add_quote_group_setting/.meta @@ -0,0 +1,6 @@ +{ + "id": "91a948e7-4cff-4492-b25c-e219cfb4e8de", + "date": "2025-01-27 08:55:40", + "message": "Add quote group setting", + "parent": 0 +} \ No newline at end of file diff --git a/products/BellHybrid/services/db/databases/migration/settings_bell/current/91a948e7_Add_quote_group_setting/down.sql b/products/BellHybrid/services/db/databases/migration/settings_bell/current/91a948e7_Add_quote_group_setting/down.sql new file mode 100644 index 0000000000000000000000000000000000000000..ecef39d0b5ec476ef13c8e43a42baf7f961e2754 --- /dev/null +++ b/products/BellHybrid/services/db/databases/migration/settings_bell/current/91a948e7_Add_quote_group_setting/down.sql @@ -0,0 +1,10 @@ +-- Copyright (c) 2017-2025, Mudita Sp. z.o.o. All rights reserved. +-- For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md + +-- Message: Add quote group setting +-- Revision: 91a948e7-4cff-4492-b25c-e219cfb4e8de +-- Create Date: 2025-01-27 08:55:40 + +-- Insert SQL here + +DELETE FROM settings_tab WHERE path = 'quotes_group'; diff --git a/products/BellHybrid/services/db/databases/migration/settings_bell/current/91a948e7_Add_quote_group_setting/up.sql b/products/BellHybrid/services/db/databases/migration/settings_bell/current/91a948e7_Add_quote_group_setting/up.sql new file mode 100644 index 0000000000000000000000000000000000000000..0a9cd5952e0f0bd293804dc505edc8dbfdcdb163 --- /dev/null +++ b/products/BellHybrid/services/db/databases/migration/settings_bell/current/91a948e7_Add_quote_group_setting/up.sql @@ -0,0 +1,11 @@ +-- Copyright (c) 2017-2025, Mudita Sp. z.o.o. All rights reserved. +-- For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md + +-- Message: Add quote group setting +-- Revision: 91a948e7-4cff-4492-b25c-e219cfb4e8de +-- Create Date: 2025-01-27 08:55:40 + +-- Insert SQL here + +INSERT OR IGNORE INTO settings_tab (path, value) VALUES + ('quotes_group', 'Predefined'); diff --git a/products/BellHybrid/services/desktop/endpoints/CMakeLists.txt b/products/BellHybrid/services/desktop/endpoints/CMakeLists.txt index 15ff583cb09384f2baf5b7de6465e2dac1ab768d..f6f5dcd0cebacc64288c466fbf7d4a5aab47110f 100644 --- a/products/BellHybrid/services/desktop/endpoints/CMakeLists.txt +++ b/products/BellHybrid/services/desktop/endpoints/CMakeLists.txt @@ -6,8 +6,12 @@ target_sources( EndpointFactoryBell.hpp EndpointFactoryBell.cpp deviceInfo/DeviceInfoEndpoint.cpp + quotes/QuotesEndpoint.cpp + quotes/QuotesHelper.cpp PUBLIC include/endpoints/deviceInfo/DeviceInfoEndpoint.hpp + include/endpoints/quotes/QuotesEndpoint.hpp + include/endpoints/quotes/QuotesHelper.hpp ) target_include_directories( diff --git a/products/BellHybrid/services/desktop/endpoints/EndpointFactoryBell.cpp b/products/BellHybrid/services/desktop/endpoints/EndpointFactoryBell.cpp index 9e7b48d86d910f22c508df5b5f6fcd35373eeac9..83277829209f1c637e661faef3ef96774504f948 100644 --- a/products/BellHybrid/services/desktop/endpoints/EndpointFactoryBell.cpp +++ b/products/BellHybrid/services/desktop/endpoints/EndpointFactoryBell.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2025, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md #include "EndpointFactoryBell.hpp" @@ -12,6 +12,7 @@ #include #include #include +#include namespace sdesktop::endpoints { @@ -34,6 +35,8 @@ namespace sdesktop::endpoints return std::make_unique(ownerServicePtr); case EndpointType::TimeSync: return std::make_unique(ownerServicePtr); + case EndpointType::Quotes: + return std::make_unique(ownerServicePtr); default: return std::make_unique(ownerServicePtr); } diff --git a/products/BellHybrid/services/desktop/endpoints/include/endpoints/quotes/QuotesEndpoint.hpp b/products/BellHybrid/services/desktop/endpoints/include/endpoints/quotes/QuotesEndpoint.hpp new file mode 100644 index 0000000000000000000000000000000000000000..9e9a4e306b5a5aeb6a3d73cddb219d38f683cf0a --- /dev/null +++ b/products/BellHybrid/services/desktop/endpoints/include/endpoints/quotes/QuotesEndpoint.hpp @@ -0,0 +1,21 @@ +// Copyright (c) 2017-2025, Mudita Sp. z.o.o. All rights reserved. +// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md + +#pragma once + +#include "QuotesHelper.hpp" +#include + +namespace sdesktop::endpoints +{ + class QuotesEndpoint : public Endpoint + { + public: + explicit QuotesEndpoint(sys::Service *ownerServicePtr); + auto handle(Context &context) -> void override; + + private: + std::unique_ptr helper; + }; + +} // namespace sdesktop::endpoints diff --git a/products/BellHybrid/services/desktop/endpoints/include/endpoints/quotes/QuotesHelper.hpp b/products/BellHybrid/services/desktop/endpoints/include/endpoints/quotes/QuotesHelper.hpp new file mode 100644 index 0000000000000000000000000000000000000000..d11d4aa014ab492e2f59f2a27fc2cf958c93ce06 --- /dev/null +++ b/products/BellHybrid/services/desktop/endpoints/include/endpoints/quotes/QuotesHelper.hpp @@ -0,0 +1,21 @@ +// Copyright (c) 2017-2025, Mudita Sp. z.o.o. All rights reserved. +// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md + +#pragma once + +#include + +namespace sdesktop::endpoints +{ + class QuotesHelper : public BaseHelper + { + public: + explicit QuotesHelper(sys::Service *p) : BaseHelper(p) + {} + + auto processGet(Context &context) -> ProcessResult final; + auto processPost(Context &context) -> ProcessResult final; + auto processPut(Context &context) -> ProcessResult final; + auto processDelete(Context &context) -> ProcessResult final; + }; +} // namespace sdesktop::endpoints diff --git a/products/BellHybrid/services/desktop/endpoints/quotes/QuotesEndpoint.cpp b/products/BellHybrid/services/desktop/endpoints/quotes/QuotesEndpoint.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c108a3c2424a30f5d41f50c81e5b4dc952736f4a --- /dev/null +++ b/products/BellHybrid/services/desktop/endpoints/quotes/QuotesEndpoint.cpp @@ -0,0 +1,38 @@ +// Copyright (c) 2017-2025, Mudita Sp. z.o.o. All rights reserved. +// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md + +#include +#include +#include + +namespace sdesktop::endpoints +{ + QuotesEndpoint::QuotesEndpoint(sys::Service *ownerServicePtr) : Endpoint(ownerServicePtr) + { + debugName = "QuotesEndpoint"; + helper = std::make_unique(ownerServicePtr); + } + + auto QuotesEndpoint::handle(Context &context) -> void + { + const auto &[sent, response] = helper->process(context.getMethod(), context); + + if (sent == Sent::Delayed) { + LOG_DEBUG("There is no proper delayed serving mechanism - depend on invisible context caching"); + } + if (sent == Sent::No) { + if (not response.has_value()) { + LOG_ERROR("Response not sent & response not created : respond with error"); + context.setResponseStatus(http::Code::NotAcceptable); + } + else { + context.setResponse(response.value()); + } + + sender::putToSendQueue(context.createSimpleResponse()); + } + if (sent == Sent::Yes and response.has_value()) { + LOG_ERROR("Response set when we already handled response in handler"); + } + } +} // namespace sdesktop::endpoints diff --git a/products/BellHybrid/services/desktop/endpoints/quotes/QuotesHelper.cpp b/products/BellHybrid/services/desktop/endpoints/quotes/QuotesHelper.cpp new file mode 100644 index 0000000000000000000000000000000000000000..79e286d335f26b0fa83f9eb68bd9bc8d8da131c9 --- /dev/null +++ b/products/BellHybrid/services/desktop/endpoints/quotes/QuotesHelper.cpp @@ -0,0 +1,47 @@ +// Copyright (c) 2017-2025, Mudita Sp. z.o.o. All rights reserved. +// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md + +#include +#include +#include +#include +#include +#include + +namespace sdesktop::endpoints +{ + auto QuotesHelper::processGet(Context &context) -> ProcessResult + { + // TODO: https://appnroll.atlassian.net/browse/BH-2099 + return {Sent::No, ResponseContext{.status = http::Code::BadRequest}}; + } + + auto QuotesHelper::processPost(Context &context) -> ProcessResult + { + // TODO: https://appnroll.atlassian.net/browse/BH-2097 + return {Sent::No, ResponseContext{.status = http::Code::InternalServerError}}; + } + + auto QuotesHelper::processPut(Context &context) -> ProcessResult + { + const auto &body = context.getBody(); + if (const auto group = body[json::quotes::settings::group].string_value(); !group.empty()) { + DBServiceAPI::QuotesGroupChanged(owner, group.c_str()); + } + else if (const auto interval = body[json::quotes::settings::interval].string_value(); !interval.empty()) { + // TODO: https://appnroll.atlassian.net/browse/BH-2095 + } + else { + return {Sent::No, ResponseContext{.status = http::Code::BadRequest}}; + } + + return {Sent::No, ResponseContext{.status = http::Code::OK}}; + } + + auto QuotesHelper::processDelete(Context &context) -> ProcessResult + { + // TODO: https://appnroll.atlassian.net/browse/BH-2097 + return {Sent::No, ResponseContext{.status = http::Code::InternalServerError}}; + } + +} // namespace sdesktop::endpoints