Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created Core controller baseclass #191

Merged
merged 4 commits into from
Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/domain/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
project (domain)

# Domains
add_subdirectory (common)
add_subdirectory (customermgmt)
add_subdirectory (dashboard)
add_subdirectory (employeemgmt)
Expand Down
12 changes: 12 additions & 0 deletions core/domain/common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
project (common)

# domain lib
add_library (
domaincommon
STATIC
cachecontroller.hpp
basecontroller.hpp
librarycommon.hpp
)

set_target_properties(domaincommon PROPERTIES LINKER_LANGUAGE CXX)
60 changes: 60 additions & 0 deletions core/domain/common/basecontroller.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**************************************************************************************************
* 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 <https://www.gnu.org/licenses/>. *
* *
* Ben Ziv <[email protected]> *
* *
**************************************************************************************************/
#ifndef CORE_DOMAIN_COMMON_BASECONTROLLER_HPP_
#define CORE_DOMAIN_COMMON_BASECONTROLLER_HPP_
#include <functional>
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include <logger/loghelper.hpp>
#include "cachecontroller.hpp"

namespace domain {

template <typename DpType, typename ViewType, typename EntityType>
class BaseController {
public:
BaseController() = default;
virtual ~BaseController() = default;

protected:
/*!
* Logs the validation results for debugging purposes
*/
typedef std::map<std::string, std::string> ValidationErrors;
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());
}
}

typedef std::pair<std::string, std::function<std::string(const EntityType&)>> Keys;
std::shared_ptr<DpType> mDataProvider;
std::shared_ptr<ViewType> mView;
CacheController<EntityType> mCachedList;
};

} // namespace domain

#endif // CORE_DOMAIN_COMMON_BASECONTROLLER_HPP_
110 changes: 110 additions & 0 deletions core/domain/common/cachecontroller.hpp
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>. *
* *
* Ben Ziv <[email protected]> *
* *
**************************************************************************************************/
#ifndef CORE_DOMAIN_COMMON_CACHECONTROLLER_HPP_
#define CORE_DOMAIN_COMMON_CACHECONTROLLER_HPP_
#include <functional>
#include <string>
#include <vector>

namespace domain {

/*!
* Cache list wrapper
*/

template <typename EntityType>
class CacheController {
public:
CacheController() = default;
virtual ~CacheController() = default;

/*!
* Sets cached data
*/
void fill(const std::vector<EntityType>& list) {
mCachedList = list;
}

/*!
* Returns a copy of cached data
*/
const std::vector<EntityType>& 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<EntityType>::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<EntityType>::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<EntityType>::iterator find(const std::string& key,
std::function<std::string(const EntityType&)> 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<std::string(const EntityType&)> fn) {
return find(key, fn) != mCachedList.end();
}

private:
std::vector<EntityType> mCachedList;
};

} // namespace domain
#endif // CORE_DOMAIN_COMMON_CACHECONTROLLER_HPP_
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
* Ben Ziv <[email protected]> *
* *
**************************************************************************************************/
#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
Expand All @@ -33,4 +33,4 @@
#endif
#endif

#endif // CORE_DOMAIN_LIBRARYCOMMON_HPP_
#endif // CORE_DOMAIN_COMMON_LIBRARYCOMMON_HPP_
48 changes: 16 additions & 32 deletions core/domain/customermgmt/customermgmtcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,30 @@ CustomerManagementController::CustomerManagementController(const CustomerMgmtDat

std::vector<entity::Customer> 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<entity::Customer>::iterator& iter = find(id);
if (iter != mCachedList.end()) {
LOG_INFO("Found customer %s", id.c_str());
return *iter;
} else {
const std::vector<entity::Customer>::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");
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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());
}
Expand All @@ -139,15 +139,16 @@ void CustomerManagementController::update(const entity::Customer& customer) {
// Update actual data
mDataProvider->update(customer);
// Update cache list
const std::vector<entity::Customer>::iterator it = find(customer.ID());
const std::vector<entity::Customer>::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<entity::Customer>::iterator it = find(id);
if (it == mCachedList.end()) {
const std::vector<entity::Customer>::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;
Expand All @@ -163,23 +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<entity::Customer>::iterator CustomerManagementController::find(const std::string& id) {
return std::find_if(mCachedList.begin(), mCachedList.end(),
[&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) {
Expand Down
14 changes: 5 additions & 9 deletions core/domain/customermgmt/customermgmtcontroller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include <map>
#include <vector>
#include "interface/customermgmtiface.hpp"

#include <domain/common/basecontroller.hpp>
// Entity
#include <entity/customer.hpp>

Expand All @@ -34,7 +34,10 @@ namespace customermgmt {

typedef std::map<std::string, std::string> ValidationErrors;

class CustomerManagementController : public CustomerManagementControlInterface {
class CustomerManagementController : public CustomerManagementControlInterface,
public BaseController<CustomerManagementDataInterface,
CustomerManagementViewInterface,
entity::Customer> {
public:
explicit CustomerManagementController(const CustomerMgmtDataPtr& data,
const CustomerMgmtViewPtr& view);
Expand All @@ -49,13 +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<entity::Customer>::iterator find(const std::string& id);

CustomerMgmtDataPtr mDataProvider;
CustomerMgmtViewPtr mView;
std::vector<entity::Customer> mCachedList; // List of customers
};

} // namespace customermgmt
Expand Down
2 changes: 1 addition & 1 deletion core/domain/customermgmt/interface/customermgmtiface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include <vector>
#include "customermgmtdataif.hpp"
#include "customermgmtviewif.hpp"
#include <domain/librarycommon.hpp>
#include <domain/common/librarycommon.hpp>
#include <entity/customer.hpp>

namespace domain {
Expand Down
8 changes: 5 additions & 3 deletions core/domain/dashboard/dashboardcontroller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <memory>
#include <string>
#include "interface/dashboardiface.hpp"
#include <domain/common/basecontroller.hpp>
#include <entity/user.hpp>

namespace domain {
Expand All @@ -34,7 +35,10 @@ enum class DASHSTATUS {
UNINITIALIZED = 2
};

class DashboardController : public DashboardControlInterface {
class DashboardController : public DashboardControlInterface,
public BaseController<DashboardDataInterface,
DashboardViewInterface,
entity::User> {
public:
explicit DashboardController(const DashboardDataPtr& data,
const DashboardViewPtr& view);
Expand All @@ -45,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;
Expand Down
2 changes: 1 addition & 1 deletion core/domain/dashboard/interface/dashboardiface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include <string>
#include "dashboarddataif.hpp"
#include "dashboardviewif.hpp"
#include <domain/librarycommon.hpp>
#include <domain/common/librarycommon.hpp>
#include <entity/user.hpp>
#include <entity/employee.hpp>

Expand Down
Loading