From 62d472e38d5630b2312319a6a894971d2ff6b2e9 Mon Sep 17 00:00:00 2001 From: benzgar Date: Mon, 11 Jan 2021 20:54:23 -0500 Subject: [PATCH 1/4] Created Core controller baseclass Closes #177 - Moved dumpValidationResult --- core/domain/CMakeLists.txt | 1 + core/domain/common/CMakeLists.txt | 15 +++++ core/domain/common/basecontroller.hpp | 58 +++++++++++++++++++ core/domain/{ => common}/librarycommon.hpp | 6 +- .../customermgmt/customermgmtcontroller.cpp | 8 --- .../customermgmt/customermgmtcontroller.hpp | 8 ++- .../interface/customermgmtiface.hpp | 2 +- core/domain/dashboard/dashboardcontroller.hpp | 6 +- .../dashboard/interface/dashboardiface.hpp | 2 +- .../employeemgmt/employeecontroller.cpp | 7 --- .../employeemgmt/employeecontroller.hpp | 8 ++- .../interface/employeemgmtiface.hpp | 2 +- .../inventory/interface/inventoryiface.hpp | 2 +- core/domain/inventory/inventorycontroller.cpp | 7 --- core/domain/inventory/inventorycontroller.hpp | 8 ++- core/domain/unittest/CMakeLists.txt | 1 + .../domain/userlogin/interface/loginiface.hpp | 2 +- core/domain/userlogin/logincontroller.hpp | 7 ++- 18 files changed, 108 insertions(+), 42 deletions(-) create mode 100644 core/domain/common/CMakeLists.txt create mode 100644 core/domain/common/basecontroller.hpp rename core/domain/{ => common}/librarycommon.hpp (94%) diff --git a/core/domain/CMakeLists.txt b/core/domain/CMakeLists.txt index c7ad7d76..0c8c9a07 100644 --- a/core/domain/CMakeLists.txt +++ b/core/domain/CMakeLists.txt @@ -1,6 +1,7 @@ project (domain) # Domains +add_subdirectory (common) add_subdirectory (customermgmt) add_subdirectory (dashboard) add_subdirectory (employeemgmt) diff --git a/core/domain/common/CMakeLists.txt b/core/domain/common/CMakeLists.txt new file mode 100644 index 00000000..7199d9ed --- /dev/null +++ b/core/domain/common/CMakeLists.txt @@ -0,0 +1,15 @@ +project (common) + +# domain lib +add_library ( + domaincommon + STATIC + librarycommon.hpp + basecontroller.hpp +) + +target_link_libraries ( + domaincommon + entity + ${MINGW_DEPENDENCY} +) diff --git a/core/domain/common/basecontroller.hpp b/core/domain/common/basecontroller.hpp new file mode 100644 index 00000000..b666755a --- /dev/null +++ b/core/domain/common/basecontroller.hpp @@ -0,0 +1,58 @@ +/************************************************************************************************** +* PSCORE * +* Copyright (C) 2021 Pointon Software * +* * +* This program is free software: you can redistribute it and/or modify * +* it under the terms of the GNU Affero General Public License as published * +* by the Free Software Foundation, either version 3 of the License, or * +* (at your option) any later version. * +* * +* This program is distributed in the hope that it will be useful, * +* but WITHOUT ANY WARRANTY; without even the implied warranty of * +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +* GNU Affero General Public License for more details. * +* * +* You should have received a copy of the GNU Affero General Public License * +* along with this program. If not, see . * +* * +* Ben Ziv * +* * +**************************************************************************************************/ +#ifndef CORE_DOMAIN_COMMON_BASECONTROLLER_HPP_ +#define CORE_DOMAIN_COMMON_BASECONTROLLER_HPP_ +#include +#include +#include +#include +#include + +namespace domain { + +typedef std::map ValidationErrors; + +template +class BaseController { + public: + BaseController() = default; + virtual ~BaseController() = default; + + protected: + void dumpValidationResult(const ValidationErrors& errors) const { + LOG_DEBUG("Dumping validation result"); + for (auto const &result : errors) { + LOG_DEBUG(std::string(result.first + " -> " + result.second).c_str()); + } + } + + std::shared_ptr mDataProvider; + std::shared_ptr mView; + + private: + std::vector mCachedList; + bool isExists(const std::string& barcode); + typename std::vector::iterator find(const std::string& barcode); +}; + +} // namespace domain + +#endif // CORE_DOMAIN_COMMON_BASECONTROLLER_HPP_ \ No newline at end of file diff --git a/core/domain/librarycommon.hpp b/core/domain/common/librarycommon.hpp similarity index 94% rename from core/domain/librarycommon.hpp rename to core/domain/common/librarycommon.hpp index 9411fc28..c322a3d5 100644 --- a/core/domain/librarycommon.hpp +++ b/core/domain/common/librarycommon.hpp @@ -18,8 +18,8 @@ * Ben Ziv * * * **************************************************************************************************/ -#ifndef CORE_DOMAIN_LIBRARYCOMMON_HPP_ -#define CORE_DOMAIN_LIBRARYCOMMON_HPP_ +#ifndef CORE_DOMAIN_COMMON_LIBRARYCOMMON_HPP_ +#define CORE_DOMAIN_COMMON_LIBRARYCOMMON_HPP_ #if defined(__GNUC__) // Linux @@ -33,4 +33,4 @@ #endif #endif -#endif // CORE_DOMAIN_LIBRARYCOMMON_HPP_ \ No newline at end of file +#endif // CORE_DOMAIN_COMMON_LIBRARYCOMMON_HPP_ \ No newline at end of file diff --git a/core/domain/customermgmt/customermgmtcontroller.cpp b/core/domain/customermgmt/customermgmtcontroller.cpp index 7bd91d82..6c1cece1 100644 --- a/core/domain/customermgmt/customermgmtcontroller.cpp +++ b/core/domain/customermgmt/customermgmtcontroller.cpp @@ -172,14 +172,6 @@ std::vector::iterator CustomerManagementController::find(const [&id](const entity::Customer& e) { return e.ID() == id; }); } -void CustomerManagementController::dumpValidationResult( - const ValidationErrors& validationErrors) const { - LOG_DEBUG("Dumping validation result"); - for (auto const &result : validationErrors) { - LOG_DEBUG(std::string(result.first + " -> " + result.second).c_str()); - } -} - CustomerMgmtControllerPtr createCustomerMgmtModule( const CustomerMgmtDataPtr& data, const CustomerMgmtViewPtr& view) { diff --git a/core/domain/customermgmt/customermgmtcontroller.hpp b/core/domain/customermgmt/customermgmtcontroller.hpp index 87a27b0e..fd07f036 100644 --- a/core/domain/customermgmt/customermgmtcontroller.hpp +++ b/core/domain/customermgmt/customermgmtcontroller.hpp @@ -25,7 +25,7 @@ #include #include #include "interface/customermgmtiface.hpp" - +#include // Entity #include @@ -34,7 +34,10 @@ namespace customermgmt { typedef std::map ValidationErrors; -class CustomerManagementController : public CustomerManagementControlInterface { +class CustomerManagementController : public CustomerManagementControlInterface, + public BaseController { public: explicit CustomerManagementController(const CustomerMgmtDataPtr& data, const CustomerMgmtViewPtr& view); @@ -49,7 +52,6 @@ class CustomerManagementController : public CustomerManagementControlInterface { private: void create(const entity::Customer& customer); void update(const entity::Customer& customer); - void dumpValidationResult(const ValidationErrors& validationErrors) const; bool isExists(const std::string& id); std::vector::iterator find(const std::string& id); diff --git a/core/domain/customermgmt/interface/customermgmtiface.hpp b/core/domain/customermgmt/interface/customermgmtiface.hpp index de8759fd..7976cd92 100644 --- a/core/domain/customermgmt/interface/customermgmtiface.hpp +++ b/core/domain/customermgmt/interface/customermgmtiface.hpp @@ -26,7 +26,7 @@ #include #include "customermgmtdataif.hpp" #include "customermgmtviewif.hpp" -#include +#include #include namespace domain { diff --git a/core/domain/dashboard/dashboardcontroller.hpp b/core/domain/dashboard/dashboardcontroller.hpp index 65bf9c22..b18c0ae3 100644 --- a/core/domain/dashboard/dashboardcontroller.hpp +++ b/core/domain/dashboard/dashboardcontroller.hpp @@ -23,6 +23,7 @@ #include #include #include "interface/dashboardiface.hpp" +#include #include namespace domain { @@ -34,7 +35,10 @@ enum class DASHSTATUS { UNINITIALIZED = 2 }; -class DashboardController : public DashboardControlInterface { +class DashboardController : public DashboardControlInterface, + public BaseController { public: explicit DashboardController(const DashboardDataPtr& data, const DashboardViewPtr& view); diff --git a/core/domain/dashboard/interface/dashboardiface.hpp b/core/domain/dashboard/interface/dashboardiface.hpp index 624088de..71e710ee 100644 --- a/core/domain/dashboard/interface/dashboardiface.hpp +++ b/core/domain/dashboard/interface/dashboardiface.hpp @@ -24,7 +24,7 @@ #include #include "dashboarddataif.hpp" #include "dashboardviewif.hpp" -#include +#include #include #include diff --git a/core/domain/employeemgmt/employeecontroller.cpp b/core/domain/employeemgmt/employeecontroller.cpp index f0a1b322..2d523ccc 100644 --- a/core/domain/employeemgmt/employeecontroller.cpp +++ b/core/domain/employeemgmt/employeecontroller.cpp @@ -234,13 +234,6 @@ ValidationErrors EmployeeMgmtController::validateDetails(const entity::Employee& return validationErrors; } -void EmployeeMgmtController::dumpValidationResult(const ValidationErrors& validationErrors) const { - LOG_DEBUG("Dumping validation result"); - for (auto const &result : validationErrors) { - LOG_DEBUG(std::string(result.first + " -> " + result.second).c_str()); - } -} - EmpMgmtControllerPtr createEmployeeMgmtModule(const EmpMgmtDataPtr& data, const EmpMgmtViewPtr& view) { return std::make_unique(data, view); diff --git a/core/domain/employeemgmt/employeecontroller.hpp b/core/domain/employeemgmt/employeecontroller.hpp index 3f45b58d..769038c7 100644 --- a/core/domain/employeemgmt/employeecontroller.hpp +++ b/core/domain/employeemgmt/employeecontroller.hpp @@ -25,7 +25,7 @@ #include #include #include "interface/employeemgmtiface.hpp" - +#include // Entity #include #include @@ -35,7 +35,10 @@ namespace empmgmt { typedef std::map ValidationErrors; -class EmployeeMgmtController : public EmployeeMgmtControlInterface { +class EmployeeMgmtController : public EmployeeMgmtControlInterface, + public BaseController { public: explicit EmployeeMgmtController(const EmpMgmtDataPtr& data, const EmpMgmtViewPtr& view); @@ -57,7 +60,6 @@ class EmployeeMgmtController : public EmployeeMgmtControlInterface { bool isExists(const std::string& id); std::vector::iterator find(const std::string& id); ValidationErrors validateDetails(const entity::Employee& employee) const; - void dumpValidationResult(const ValidationErrors& validationErrors) const; void create(const SaveEmployeeData& data); void createUser(const entity::Employee& employee, const std::string& pin) const; diff --git a/core/domain/employeemgmt/interface/employeemgmtiface.hpp b/core/domain/employeemgmt/interface/employeemgmtiface.hpp index 46e2f8cf..58b69327 100644 --- a/core/domain/employeemgmt/interface/employeemgmtiface.hpp +++ b/core/domain/employeemgmt/interface/employeemgmtiface.hpp @@ -26,7 +26,7 @@ #include #include "employeemgmtdataif.hpp" #include "employeemgmtviewif.hpp" -#include +#include #include namespace domain { diff --git a/core/domain/inventory/interface/inventoryiface.hpp b/core/domain/inventory/interface/inventoryiface.hpp index bc414e41..b0fef6c8 100644 --- a/core/domain/inventory/interface/inventoryiface.hpp +++ b/core/domain/inventory/interface/inventoryiface.hpp @@ -26,7 +26,7 @@ #include #include "inventorydataif.hpp" #include "inventoryviewif.hpp" -#include +#include #include namespace domain { diff --git a/core/domain/inventory/inventorycontroller.cpp b/core/domain/inventory/inventorycontroller.cpp index 034b5a57..6bd62c18 100644 --- a/core/domain/inventory/inventorycontroller.cpp +++ b/core/domain/inventory/inventorycontroller.cpp @@ -116,13 +116,6 @@ void InventoryController::update(const entity::Product& product) { LOG_INFO("Product %s information updated", product.name().c_str()); } -void InventoryController::dumpValidationResult(const ValidationErrors& validationErrors) const { - LOG_DEBUG("Dumping validation result"); - for (auto const &result : validationErrors) { - LOG_DEBUG(std::string(result.first + " -> " + result.second).c_str()); - } -} - INVENTORYAPISTATUS InventoryController::remove(const std::string& barcode) { LOG_DEBUG("Removing product %s", barcode.c_str()); const std::vector::iterator it = find(barcode); diff --git a/core/domain/inventory/inventorycontroller.hpp b/core/domain/inventory/inventorycontroller.hpp index a970485a..8225eef0 100644 --- a/core/domain/inventory/inventorycontroller.hpp +++ b/core/domain/inventory/inventorycontroller.hpp @@ -25,7 +25,7 @@ #include #include #include "interface/inventoryiface.hpp" - +#include // Entity #include @@ -34,7 +34,10 @@ namespace inventory { typedef std::map ValidationErrors; -class InventoryController : public InventoryControlInterface { +class InventoryController : public InventoryControlInterface, + public BaseController { public: explicit InventoryController(const InventoryDataPtr& data, const InventoryViewPtr& view); @@ -51,7 +54,6 @@ class InventoryController : public InventoryControlInterface { std::vector::iterator find(const std::string& barcode); void create(const entity::Product& product); void update(const entity::Product& product); - void dumpValidationResult(const ValidationErrors& validationErrors) const; InventoryDataPtr mDataProvider; InventoryViewPtr mView; diff --git a/core/domain/unittest/CMakeLists.txt b/core/domain/unittest/CMakeLists.txt index ec617c52..9bf4d6a1 100644 --- a/core/domain/unittest/CMakeLists.txt +++ b/core/domain/unittest/CMakeLists.txt @@ -43,6 +43,7 @@ endif () target_link_libraries ( domain_unittest + domaincommon entity validator gtest diff --git a/core/domain/userlogin/interface/loginiface.hpp b/core/domain/userlogin/interface/loginiface.hpp index 07f3e884..a6008740 100644 --- a/core/domain/userlogin/interface/loginiface.hpp +++ b/core/domain/userlogin/interface/loginiface.hpp @@ -22,7 +22,7 @@ #define CORE_DOMAIN_USERLOGIN_INTERFACE_LOGINIFACE_HPP_ #include #include -#include +#include #include "logindataif.hpp" #include "loginviewif.hpp" diff --git a/core/domain/userlogin/logincontroller.hpp b/core/domain/userlogin/logincontroller.hpp index fc7433f7..96e748a4 100644 --- a/core/domain/userlogin/logincontroller.hpp +++ b/core/domain/userlogin/logincontroller.hpp @@ -26,7 +26,7 @@ #include "interface/loginiface.hpp" #include "interface/logindataif.hpp" #include "interface/loginviewif.hpp" - +#include // Entity #include @@ -39,7 +39,10 @@ enum class AUTHSTATUS { UNINITIALIZED = 2 }; -class LoginController : public LoginControlInterface { +class LoginController : public LoginControlInterface, + public BaseController { public: explicit LoginController(const LoginDataPtr& dataprovider, const LoginViewPtr& view); From 12aa1b30fc3584a55f596f0954b9cecb9d9a88e3 Mon Sep 17 00:00:00 2001 From: benzgar Date: Mon, 11 Jan 2021 21:00:21 -0500 Subject: [PATCH 2/4] - Removed dataprovider and view members --- core/domain/customermgmt/customermgmtcontroller.hpp | 2 -- core/domain/dashboard/dashboardcontroller.hpp | 2 -- core/domain/employeemgmt/employeecontroller.hpp | 2 -- core/domain/inventory/inventorycontroller.hpp | 2 -- core/domain/userlogin/logincontroller.hpp | 3 +-- 5 files changed, 1 insertion(+), 10 deletions(-) diff --git a/core/domain/customermgmt/customermgmtcontroller.hpp b/core/domain/customermgmt/customermgmtcontroller.hpp index fd07f036..7c0a847a 100644 --- a/core/domain/customermgmt/customermgmtcontroller.hpp +++ b/core/domain/customermgmt/customermgmtcontroller.hpp @@ -55,8 +55,6 @@ class CustomerManagementController : public CustomerManagementControlInterface, bool isExists(const std::string& id); std::vector::iterator find(const std::string& id); - CustomerMgmtDataPtr mDataProvider; - CustomerMgmtViewPtr mView; std::vector mCachedList; // List of customers }; diff --git a/core/domain/dashboard/dashboardcontroller.hpp b/core/domain/dashboard/dashboardcontroller.hpp index b18c0ae3..dcd5895f 100644 --- a/core/domain/dashboard/dashboardcontroller.hpp +++ b/core/domain/dashboard/dashboardcontroller.hpp @@ -49,8 +49,6 @@ class DashboardController : public DashboardControlInterface, entity::Employee getUserDetails(const entity::User& user) override; private: - DashboardDataPtr mDataProvider; - DashboardViewPtr mView; std::string mCurrentUserID; DASHSTATUS getUserData(entity::User* container) const; DASHSTATUS getEmployeeData(const std::string& employeeID, entity::Employee* container) const; diff --git a/core/domain/employeemgmt/employeecontroller.hpp b/core/domain/employeemgmt/employeecontroller.hpp index 769038c7..272224cd 100644 --- a/core/domain/employeemgmt/employeecontroller.hpp +++ b/core/domain/employeemgmt/employeecontroller.hpp @@ -53,8 +53,6 @@ class EmployeeMgmtController : public EmployeeMgmtControlInterface, const std::string& lname) override; private: - EmpMgmtDataPtr mDataProvider; - EmpMgmtViewPtr mView; std::vector mCachedList; // List of employees bool isExists(const std::string& id); diff --git a/core/domain/inventory/inventorycontroller.hpp b/core/domain/inventory/inventorycontroller.hpp index 8225eef0..2f0f9007 100644 --- a/core/domain/inventory/inventorycontroller.hpp +++ b/core/domain/inventory/inventorycontroller.hpp @@ -55,8 +55,6 @@ class InventoryController : public InventoryControlInterface, void create(const entity::Product& product); void update(const entity::Product& product); - InventoryDataPtr mDataProvider; - InventoryViewPtr mView; std::vector mCachedList; // List of products }; diff --git a/core/domain/userlogin/logincontroller.hpp b/core/domain/userlogin/logincontroller.hpp index 96e748a4..1899d1f2 100644 --- a/core/domain/userlogin/logincontroller.hpp +++ b/core/domain/userlogin/logincontroller.hpp @@ -47,9 +47,8 @@ class LoginController : public LoginControlInterface, explicit LoginController(const LoginDataPtr& dataprovider, const LoginViewPtr& view); bool authenticate(const std::string& id, const std::string& pin) override; + private: - LoginDataPtr mDataProvider; - LoginViewPtr mView; AUTHSTATUS getUser(const std::string& id, entity::User* user); bool isPinValid(const std::string& pin) const; bool isUserValid(const entity::User& userInfo) const; From 579f310056b76cba0462e5e7a1dfc82f79d02132 Mon Sep 17 00:00:00 2001 From: benzgar Date: Mon, 11 Jan 2021 22:16:33 -0500 Subject: [PATCH 3/4] - Created cachelist controller --- core/domain/common/CMakeLists.txt | 3 +- core/domain/common/basecontroller.hpp | 11 +- core/domain/common/cachecontroller.hpp | 110 ++++++++++++++++++ .../customermgmt/customermgmtcontroller.cpp | 40 +++---- .../customermgmt/customermgmtcontroller.hpp | 4 - .../employeemgmt/employeecontroller.cpp | 37 +++--- .../employeemgmt/employeecontroller.hpp | 4 - core/domain/inventory/inventorycontroller.cpp | 37 +++--- core/domain/inventory/inventorycontroller.hpp | 4 - 9 files changed, 164 insertions(+), 86 deletions(-) create mode 100644 core/domain/common/cachecontroller.hpp diff --git a/core/domain/common/CMakeLists.txt b/core/domain/common/CMakeLists.txt index 7199d9ed..03a7a194 100644 --- a/core/domain/common/CMakeLists.txt +++ b/core/domain/common/CMakeLists.txt @@ -4,8 +4,9 @@ project (common) add_library ( domaincommon STATIC - librarycommon.hpp + cachecontroller.hpp basecontroller.hpp + librarycommon.hpp ) target_link_libraries ( diff --git a/core/domain/common/basecontroller.hpp b/core/domain/common/basecontroller.hpp index b666755a..44f9c8e9 100644 --- a/core/domain/common/basecontroller.hpp +++ b/core/domain/common/basecontroller.hpp @@ -20,11 +20,13 @@ **************************************************************************************************/ #ifndef CORE_DOMAIN_COMMON_BASECONTROLLER_HPP_ #define CORE_DOMAIN_COMMON_BASECONTROLLER_HPP_ +#include #include #include #include #include #include +#include "cachecontroller.hpp" namespace domain { @@ -37,6 +39,9 @@ class BaseController { virtual ~BaseController() = default; protected: + /*! + * Logs the validation results for debugging purposes + */ void dumpValidationResult(const ValidationErrors& errors) const { LOG_DEBUG("Dumping validation result"); for (auto const &result : errors) { @@ -46,11 +51,7 @@ class BaseController { std::shared_ptr mDataProvider; std::shared_ptr mView; - - private: - std::vector mCachedList; - bool isExists(const std::string& barcode); - typename std::vector::iterator find(const std::string& barcode); + CacheController mCachedList; }; } // namespace domain diff --git a/core/domain/common/cachecontroller.hpp b/core/domain/common/cachecontroller.hpp new file mode 100644 index 00000000..c578910a --- /dev/null +++ b/core/domain/common/cachecontroller.hpp @@ -0,0 +1,110 @@ +/************************************************************************************************** +* PSCORE * +* Copyright (C) 2021 Pointon Software * +* * +* This program is free software: you can redistribute it and/or modify * +* it under the terms of the GNU Affero General Public License as published * +* by the Free Software Foundation, either version 3 of the License, or * +* (at your option) any later version. * +* * +* This program is distributed in the hope that it will be useful, * +* but WITHOUT ANY WARRANTY; without even the implied warranty of * +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +* GNU Affero General Public License for more details. * +* * +* You should have received a copy of the GNU Affero General Public License * +* along with this program. If not, see . * +* * +* Ben Ziv * +* * +**************************************************************************************************/ +#ifndef CORE_DOMAIN_COMMON_CACHECONTROLLER_HPP_ +#define CORE_DOMAIN_COMMON_CACHECONTROLLER_HPP_ +#include +#include +#include + +namespace domain { + +/*! + * Cache list wrapper +*/ + +template +class CacheController { + public: + CacheController() = default; + virtual ~CacheController() = default; + + /*! + * Sets cached data + */ + void fill(const std::vector& list) { + mCachedList = list; + } + + /*! + * Returns a copy of cached data + */ + const std::vector& get() { + return mCachedList; + } + + /*! + * Adds data to the list + */ + void insert(const EntityType& data) { + mCachedList.emplace_back(data); + } + + /*! + * Removes data to the list + */ + void erase(const typename std::vector::iterator it) { + mCachedList.erase(it); + } + + /*! + * Checks if cache list is not empty + */ + bool hasData() { + return !mCachedList.empty(); + } + + /*! + * Returns cache size + */ + size_t dataCount() { + return mCachedList.size(); + } + + /*! + * Returns an iterator referring to the past-the-end element in the cache list + */ + typename std::vector::iterator endOfData() { + return mCachedList.end(); + } + + /*! + * @param [in] 1 - the key or id string that you want to find + * @param [in] 2 - the entity function that returns their key or id (e.g. ID() or barcode()) + */ + typename std::vector::iterator find(const std::string& key, + std::function fn) { + return std::find_if(mCachedList.begin(), mCachedList.end(), + [&key, &fn](const EntityType& e) { return fn(e) == key; }); + } + + /*! + * See BaseController::find() for paramater details + */ + bool isExists(const std::string& key, std::function fn) { + return find(key, fn) != mCachedList.end(); + } + + private: + std::vector mCachedList; +}; + +} // namespace domain +#endif // CORE_DOMAIN_COMMON_CACHECONTROLLER_HPP_ \ No newline at end of file diff --git a/core/domain/customermgmt/customermgmtcontroller.cpp b/core/domain/customermgmt/customermgmtcontroller.cpp index 6c1cece1..55decb5d 100644 --- a/core/domain/customermgmt/customermgmtcontroller.cpp +++ b/core/domain/customermgmt/customermgmtcontroller.cpp @@ -41,30 +41,30 @@ CustomerManagementController::CustomerManagementController(const CustomerMgmtDat std::vector CustomerManagementController::list() { LOG_DEBUG("Getting the list of customers"); - mCachedList = mDataProvider->getCustomers(); - if (mCachedList.empty()) { + mCachedList.fill(mDataProvider->getCustomers()); + if (!mCachedList.hasData()) { LOG_WARN("There are no customers on record"); mView->showListIsEmptyPopup(); return {}; } - LOG_INFO("Successfully retrieved customers list. Size: %d", mCachedList.size()); - return mCachedList; + LOG_INFO("Successfully retrieved customers list. Size: %d", mCachedList.dataCount()); + return mCachedList.get(); } entity::Customer CustomerManagementController::get(const std::string& id) { LOG_DEBUG("Getting customer %s data", id.c_str()); - const std::vector::iterator& iter = find(id); - if (iter != mCachedList.end()) { - LOG_INFO("Found customer %s", id.c_str()); - return *iter; - } else { + const std::vector::iterator& iter = + mCachedList.find(id, &entity::Customer::ID); + if (iter == mCachedList.endOfData()) { LOG_ERROR("Requested customer was not found"); return entity::Customer{}; } + LOG_INFO("Found customer %s", id.c_str()); + return *iter; } CUSTOMERMGMTAPISTATUS CustomerManagementController::save(const entity::Customer& customer, - ValidationErrors* validationResult) { + ValidationErrors* validationResult) { LOG_DEBUG("Saving customer information"); if (!validationResult) { LOG_ERROR("Validation-message container is not initialized"); @@ -101,7 +101,7 @@ CUSTOMERMGMTAPISTATUS CustomerManagementController::save(const entity::Customer& return CUSTOMERMGMTAPISTATUS::FAILED; } // Decide if it's a create or update request - if (isExists(customer.ID())) { + if (mCachedList.isExists(customer.ID(), &entity::Customer::ID)) { update(customer); } else { create(customer); @@ -129,7 +129,7 @@ void CustomerManagementController::create(const entity::Customer& data) { * Todo (code) - add checking if create is successful from dataprovider * before updating the cache */ - mCachedList.emplace_back(newCustomer); + mCachedList.insert(newCustomer); LOG_INFO("Customer %s %s added", newCustomer.firstName().c_str(), newCustomer.lastName().c_str()); } @@ -139,15 +139,16 @@ void CustomerManagementController::update(const entity::Customer& customer) { // Update actual data mDataProvider->update(customer); // Update cache list - const std::vector::iterator it = find(customer.ID()); + const std::vector::iterator it = + mCachedList.find(customer.ID(), &entity::Customer::ID); *it = customer; LOG_INFO("Customer %s information updated", customer.ID().c_str()); } CUSTOMERMGMTAPISTATUS CustomerManagementController::remove(const std::string& id) { LOG_DEBUG("Removing customer %s", id.c_str()); - const std::vector::iterator it = find(id); - if (it == mCachedList.end()) { + const std::vector::iterator it = mCachedList.find(id, &entity::Customer::ID); + if (it == mCachedList.endOfData()) { LOG_ERROR("Customer with ID %s was not found in the cache list", id.c_str()); mView->showDataNotReadyScreen(); return CUSTOMERMGMTAPISTATUS::NOT_FOUND; @@ -163,15 +164,6 @@ CUSTOMERMGMTAPISTATUS CustomerManagementController::remove(const std::string& id return CUSTOMERMGMTAPISTATUS::SUCCESS; } -bool CustomerManagementController::isExists(const std::string& id) { - return find(id) != mCachedList.end(); -} - -std::vector::iterator CustomerManagementController::find(const std::string& id) { - return std::find_if(mCachedList.begin(), mCachedList.end(), - [&id](const entity::Customer& e) { return e.ID() == id; }); -} - CustomerMgmtControllerPtr createCustomerMgmtModule( const CustomerMgmtDataPtr& data, const CustomerMgmtViewPtr& view) { diff --git a/core/domain/customermgmt/customermgmtcontroller.hpp b/core/domain/customermgmt/customermgmtcontroller.hpp index 7c0a847a..c0a4b0a0 100644 --- a/core/domain/customermgmt/customermgmtcontroller.hpp +++ b/core/domain/customermgmt/customermgmtcontroller.hpp @@ -52,10 +52,6 @@ class CustomerManagementController : public CustomerManagementControlInterface, private: void create(const entity::Customer& customer); void update(const entity::Customer& customer); - bool isExists(const std::string& id); - std::vector::iterator find(const std::string& id); - - std::vector mCachedList; // List of customers }; } // namespace customermgmt diff --git a/core/domain/employeemgmt/employeecontroller.cpp b/core/domain/employeemgmt/employeecontroller.cpp index 2d523ccc..f1406bdd 100644 --- a/core/domain/employeemgmt/employeecontroller.cpp +++ b/core/domain/employeemgmt/employeecontroller.cpp @@ -46,20 +46,21 @@ EmployeeMgmtController::EmployeeMgmtController(const EmpMgmtDataPtr& data, std::vector EmployeeMgmtController::list() { LOG_DEBUG("Getting the list of employees"); - mCachedList = mDataProvider->getEmployees(); - if (mCachedList.empty()) { + mCachedList.fill(mDataProvider->getEmployees()); + if (!mCachedList.hasData()) { LOG_WARN("There are no employees on record"); mView->showEmployeesEmptyPopup(); return {}; } - LOG_INFO("Successfully retrieved employees list. Size: %d", mCachedList.size()); - return mCachedList; + LOG_INFO("Successfully retrieved employees list. Size: %d", mCachedList.dataCount()); + return mCachedList.get(); } entity::Employee EmployeeMgmtController::getEmployee(const std::string& employeeID) { LOG_DEBUG("Getting employee %s", employeeID.c_str()); - const std::vector::iterator& iter = find(employeeID); - if (iter == mCachedList.end()) { + const std::vector::iterator& iter = + mCachedList.find(employeeID, &entity::Employee::ID); + if (iter == mCachedList.endOfData()) { LOG_ERROR("Employee was not found"); return entity::Employee{}; } @@ -88,7 +89,7 @@ void EmployeeMgmtController::create(const SaveEmployeeData& data) { * Todo (code) - add checking if create is successful from dataprovider * before updating the cache */ - mCachedList.emplace_back(newEmployee); + mCachedList.insert(newEmployee); LOG_INFO("Employee %s %s added", newEmployee.firstName().c_str(), newEmployee.lastName().c_str()); if (newEmployee.isSystemUser()) { @@ -121,7 +122,8 @@ void EmployeeMgmtController::update(const SaveEmployeeData& data) { LOG_INFO("User role updated to %s", employee.position().c_str()); } // Update cache list - const std::vector::iterator it = find(employee.ID()); + const std::vector::iterator it = + mCachedList.find(employee.ID(), &entity::Employee::ID); *it = employee; LOG_INFO("Employee %s information updated", employee.ID().c_str()); } @@ -142,7 +144,8 @@ EMPLMGMTSTATUS EmployeeMgmtController::save(const SaveEmployeeData& employeeData * if we're updating, we don't need to validate PIN * until we support User Information update */ - if (employee.isSystemUser() && !isExists(employee.ID())) { + if (employee.isSystemUser() && + !mCachedList.isExists(employee.ID(), &entity::Employee::ID)) { // Validate PIN only entity::validator::UserValidator validator( entity::User("Proxy", "Proxy", employeeData.PIN, @@ -155,7 +158,7 @@ EMPLMGMTSTATUS EmployeeMgmtController::save(const SaveEmployeeData& employeeData return EMPLMGMTSTATUS::FAILED; } // Decide if it's a create or update request - if (isExists(employee.ID())) { + if (mCachedList.isExists(employee.ID(), &entity::Employee::ID)) { update(employeeData); } else { create(employeeData); @@ -165,8 +168,9 @@ EMPLMGMTSTATUS EmployeeMgmtController::save(const SaveEmployeeData& employeeData EMPLMGMTSTATUS EmployeeMgmtController::remove(const std::string& employeeID) { LOG_DEBUG("Removing employee with ID %s", employeeID.c_str()); - const std::vector::iterator it = find(employeeID); - if (it == mCachedList.end()) { + const std::vector::iterator it = + mCachedList.find(employeeID, &entity::Employee::ID); + if (it == mCachedList.endOfData()) { LOG_ERROR("Employee with ID %s was not found in the cache list", employeeID.c_str()); mView->showDataNotReadyScreen(); return EMPLMGMTSTATUS::NOT_FOUND; @@ -193,15 +197,6 @@ std::vector EmployeeMgmtController::findByName(const std::stri return {}; } -bool EmployeeMgmtController::isExists(const std::string& id) { - return find(id) != mCachedList.end(); -} - -std::vector::iterator EmployeeMgmtController::find(const std::string& id) { - return std::find_if(mCachedList.begin(), mCachedList.end(), [&id](const entity::Employee& e) { - return e.ID() == id; }); -} - ValidationErrors EmployeeMgmtController::validateDetails(const entity::Employee& employee) const { ValidationErrors validationErrors; // validate key employee data diff --git a/core/domain/employeemgmt/employeecontroller.hpp b/core/domain/employeemgmt/employeecontroller.hpp index 272224cd..7365a517 100644 --- a/core/domain/employeemgmt/employeecontroller.hpp +++ b/core/domain/employeemgmt/employeecontroller.hpp @@ -53,10 +53,6 @@ class EmployeeMgmtController : public EmployeeMgmtControlInterface, const std::string& lname) override; private: - std::vector mCachedList; // List of employees - - bool isExists(const std::string& id); - std::vector::iterator find(const std::string& id); ValidationErrors validateDetails(const entity::Employee& employee) const; void create(const SaveEmployeeData& data); diff --git a/core/domain/inventory/inventorycontroller.cpp b/core/domain/inventory/inventorycontroller.cpp index 6bd62c18..8671076c 100644 --- a/core/domain/inventory/inventorycontroller.cpp +++ b/core/domain/inventory/inventorycontroller.cpp @@ -38,20 +38,21 @@ InventoryController::InventoryController(const InventoryDataPtr& data, std::vector InventoryController::list() { LOG_DEBUG("Getting the list of products"); - mCachedList = mDataProvider->getProducts(); - if (mCachedList.empty()) { + mCachedList.fill(mDataProvider->getProducts()); + if (!mCachedList.hasData()) { LOG_WARN("There are no products on record"); mView->showProductsEmptyPopup(); return {}; } - LOG_INFO("Successfully retrieved products list. Size: %d", mCachedList.size()); - return mCachedList; + LOG_INFO("Successfully retrieved products list. Size: %d", mCachedList.dataCount()); + return mCachedList.get(); } entity::Product InventoryController::getProduct(const std::string& barcode) { LOG_DEBUG("Getting product %s", barcode.c_str()); - const std::vector::iterator& iter = find(barcode); - if (iter != mCachedList.end()) { + const std::vector::iterator& iter = + mCachedList.find(barcode, &entity::Product::barcode); + if (iter != mCachedList.endOfData()) { LOG_INFO("Found product %s", barcode.c_str()); return *iter; } else { @@ -81,7 +82,7 @@ INVENTORYAPISTATUS InventoryController::save(const entity::Product& product, return INVENTORYAPISTATUS::FAILED; } // Decide if it's a create or update request - if (isExists(product.barcode())) { + if (mCachedList.isExists(product.barcode(), &entity::Product::barcode)) { update(product); } else { create(product); @@ -97,7 +98,7 @@ void InventoryController::create(const entity::Product& product) { * Todo (code) - add checking if create is successful from dataprovider * before updating the cache */ - mCachedList.emplace_back(product); + mCachedList.insert(product); LOG_INFO("%s created with code %s", product.name().c_str(), product.barcode().c_str()); } @@ -110,16 +111,17 @@ void InventoryController::update(const entity::Product& product) { * before updating the cache */ // Update cache list - const std::vector::iterator it = find(product.barcode()); + const std::vector::iterator it = + mCachedList.find(product.barcode(), &entity::Product::barcode); *it = product; - mCachedList.emplace_back(product); LOG_INFO("Product %s information updated", product.name().c_str()); } INVENTORYAPISTATUS InventoryController::remove(const std::string& barcode) { LOG_DEBUG("Removing product %s", barcode.c_str()); - const std::vector::iterator it = find(barcode); - if (it == mCachedList.end()) { + const std::vector::iterator it = + mCachedList.find(barcode, &entity::Product::barcode); + if (it == mCachedList.endOfData()) { LOG_ERROR("Product %s was not found in the cache list", barcode.c_str()); mView->showDataNotReadyScreen(); return INVENTORYAPISTATUS::NOT_FOUND; @@ -136,17 +138,6 @@ INVENTORYAPISTATUS InventoryController::remove(const std::string& barcode) { return INVENTORYAPISTATUS::SUCCESS; } -bool InventoryController::isExists(const std::string& barcode) { - return find(barcode) != mCachedList.end(); -} - -std::vector::iterator InventoryController::find(const std::string& barcode) { - return std::find_if(mCachedList.begin(), mCachedList.end(), - [&barcode](const entity::Product& e) { - return e.barcode() == barcode; - }); -} - InventoryControllerPtr createInventoryModule(const InventoryDataPtr& data, const InventoryViewPtr& view) { return std::make_unique(data, view); diff --git a/core/domain/inventory/inventorycontroller.hpp b/core/domain/inventory/inventorycontroller.hpp index 2f0f9007..f72a715a 100644 --- a/core/domain/inventory/inventorycontroller.hpp +++ b/core/domain/inventory/inventorycontroller.hpp @@ -50,12 +50,8 @@ class InventoryController : public InventoryControlInterface, INVENTORYAPISTATUS remove(const std::string& barcode) override; private: - bool isExists(const std::string& barcode); - std::vector::iterator find(const std::string& barcode); void create(const entity::Product& product); void update(const entity::Product& product); - - std::vector mCachedList; // List of products }; } // namespace inventory From d1db21d4292f3f5ea16b76a66ab2df1aa9dcd610 Mon Sep 17 00:00:00 2001 From: benzgar Date: Wed, 13 Jan 2021 15:15:50 -0500 Subject: [PATCH 4/4] - Updated CMake and typedefs --- core/domain/common/CMakeLists.txt | 6 +----- core/domain/common/basecontroller.hpp | 5 +++-- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/core/domain/common/CMakeLists.txt b/core/domain/common/CMakeLists.txt index 03a7a194..ddf57fd5 100644 --- a/core/domain/common/CMakeLists.txt +++ b/core/domain/common/CMakeLists.txt @@ -9,8 +9,4 @@ add_library ( librarycommon.hpp ) -target_link_libraries ( - domaincommon - entity - ${MINGW_DEPENDENCY} -) +set_target_properties(domaincommon PROPERTIES LINKER_LANGUAGE CXX) \ No newline at end of file diff --git a/core/domain/common/basecontroller.hpp b/core/domain/common/basecontroller.hpp index 44f9c8e9..184e9a2b 100644 --- a/core/domain/common/basecontroller.hpp +++ b/core/domain/common/basecontroller.hpp @@ -24,14 +24,13 @@ #include #include #include +#include #include #include #include "cachecontroller.hpp" namespace domain { -typedef std::map ValidationErrors; - template class BaseController { public: @@ -42,6 +41,7 @@ class BaseController { /*! * Logs the validation results for debugging purposes */ + typedef std::map ValidationErrors; void dumpValidationResult(const ValidationErrors& errors) const { LOG_DEBUG("Dumping validation result"); for (auto const &result : errors) { @@ -49,6 +49,7 @@ class BaseController { } } + typedef std::pair> Keys; std::shared_ptr mDataProvider; std::shared_ptr mView; CacheController mCachedList;