Business logics for all entities.

This commit is contained in:
abazov73 2023-04-09 17:55:42 +04:00
parent 49325d73b2
commit 736d689c6c
7 changed files with 525 additions and 2 deletions

View File

@ -0,0 +1,106 @@
using ConstructionCompanyContracts.BindingModels;
using ConstructionCompanyContracts.BusinessLogicContracts;
using ConstructionCompanyContracts.SearchModels;
using ConstructionCompanyContracts.StorageContracts;
using ConstructionCompanyContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConstructionCompanyBusinessLogic.BusinessLogics
{
public class EmployeeLogic : IEmployeeLogic
{
private readonly ILogger _logger;
private readonly IEmployeeStorage _employeeStorage;
public EmployeeLogic(ILogger<EmployeeLogic> logger, IEmployeeStorage employeeStorage)
{
_logger = logger;
_employeeStorage = employeeStorage;
}
public List<EmployeeViewModel>? ReadList(EmployeeSearchModel? model)
{
_logger.LogInformation("ReadList. EmployeeName:{EmployeeName}. Id:{Id}", model?.EmployeeName, model?.Id);
var list = model == null ? _employeeStorage.GetFullList() : _employeeStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public EmployeeViewModel? ReadElement(EmployeeSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. EmployeeName:{EmployeeName}. Id:{Id}", model.EmployeeName, model.Id);
var element = _employeeStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public bool Create(EmployeeBindingModel model)
{
CheckModel(model);
if (_employeeStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(EmployeeBindingModel model)
{
CheckModel(model);
if (_employeeStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(EmployeeBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_employeeStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(EmployeeBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.EmployeeName))
{
throw new ArgumentNullException("Нет названия материала", nameof(model.EmployeeName));
}
_logger.LogInformation("Employee. EmployeeName:{EmployeeName}. PositionID:{PositionID}. Id:{Id}", model.EmployeeName, model.PositionID, model.Id);
}
}
}

View File

@ -0,0 +1,92 @@
using ConstructionCompanyContracts.BindingModels;
using ConstructionCompanyContracts.BusinessLogicContracts;
using ConstructionCompanyContracts.SearchModels;
using ConstructionCompanyContracts.StorageContracts;
using ConstructionCompanyContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConstructionCompanyBusinessLogic.BusinessLogics
{
public class EmployeeOrderLogic : IEmployeeOrderLogic
{
private readonly ILogger _logger;
private readonly IEmployeeOrderStorage _employeeOrderStorage;
public EmployeeOrderLogic(ILogger<EmployeeOrderLogic> logger, IEmployeeOrderStorage employeeOrderStorage)
{
_logger = logger;
_employeeOrderStorage = employeeOrderStorage;
}
public List<EmployeeOrderViewModel>? ReadList(EmployeeOrderSearchModel? model)
{
_logger.LogInformation("ReadList. EmployeeId:{EmployeeId}. OrderId:{OrderId}", model?.EmployeeId, model?.OrderId);
var list = model == null ? _employeeOrderStorage.GetFullList() : _employeeOrderStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public EmployeeOrderViewModel? ReadElement(EmployeeOrderSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadList. EmployeeId:{EmployeeId}. OrderId:{OrderId}", model?.EmployeeId, model?.OrderId);
var element = _employeeOrderStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. EmployeeId:{EmployeeId}. OrderId:{OrderId}", model?.EmployeeId, model?.OrderId);
return element;
}
public bool Create(EmployeeOrderBindingModel model)
{
CheckModel(model);
if (_employeeOrderStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(EmployeeOrderBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. EmployeeId:{EmployeeId}. OrderId:{OrderId}", model?.EmployeeId, model?.OrderId);
if (_employeeOrderStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(EmployeeOrderBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
_logger.LogInformation("EmployeeOrder. EmployeeId:{EmployeeId}. OrderId:{OrderId}", model?.EmployeeId, model?.OrderId);
}
}
}

View File

@ -0,0 +1,96 @@
using ConstructionCompanyContracts.BindingModels;
using ConstructionCompanyContracts.BusinessLogicContracts;
using ConstructionCompanyContracts.SearchModels;
using ConstructionCompanyContracts.StorageContracts;
using ConstructionCompanyContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConstructionCompanyBusinessLogic.BusinessLogics
{
public class MaterialOrderLogic : IMaterialOrderLogic
{
private readonly ILogger _logger;
private readonly IMaterialOrderStorage _employeeOrderStorage;
public MaterialOrderLogic(ILogger<MaterialOrderLogic> logger, IMaterialOrderStorage employeeOrderStorage)
{
_logger = logger;
_employeeOrderStorage = employeeOrderStorage;
}
public List<MaterialOrderViewModel>? ReadList(MaterialOrderSearchModel? model)
{
_logger.LogInformation("ReadList. MaterialId:{MaterialId}. OrderId:{OrderId}", model?.MaterialId, model?.OrderId);
var list = model == null ? _employeeOrderStorage.GetFullList() : _employeeOrderStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public MaterialOrderViewModel? ReadElement(MaterialOrderSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadList. MaterialId:{MaterialId}. OrderId:{OrderId}", model?.MaterialId, model?.OrderId);
var element = _employeeOrderStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. MaterialId:{MaterialId}. OrderId:{OrderId}", model?.MaterialId, model?.OrderId);
return element;
}
public bool Create(MaterialOrderBindingModel model)
{
CheckModel(model);
if (_employeeOrderStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(MaterialOrderBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. MaterialId:{MaterialId}. OrderId:{OrderId}", model?.MaterialId, model?.OrderId);
if (_employeeOrderStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(MaterialOrderBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.Quantity <= 0)
{
throw new InvalidOperationException("Количество должна быть больше 0!");
}
_logger.LogInformation("MaterialOrder. MaterialId:{MaterialId}. OrderId:{OrderId}", model?.MaterialId, model?.OrderId);
}
}
}

View File

@ -0,0 +1,121 @@
using ConstructionCompanyContracts.BindingModels;
using ConstructionCompanyContracts.BusinessLogicContracts;
using ConstructionCompanyContracts.SearchModels;
using ConstructionCompanyContracts.StorageContracts;
using ConstructionCompanyContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConstructionCompanyBusinessLogic.BusinessLogics
{
public class OrderLogic : IOrderLogic
{
private readonly ILogger _logger;
private readonly IOrderStorage _orderStorage;
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
{
_logger = logger;
_orderStorage = orderStorage;
}
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
{
_logger.LogInformation("ReadList. Adress:{Adress}. Id:{Id}", model?.Id);
var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public OrderViewModel? ReadElement(OrderSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Id:{Id}", model.Id);
var element = _orderStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public bool CreateOrder(OrderBindingModel model)
{
CheckModel(model);
model.Status = ConstructionCompanyDataModels.Enums.OrderStatus.Принят;
if (_orderStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool TakeOrderInWork(OrderBindingModel model)
{
CheckModel(model);
model.Status = ConstructionCompanyDataModels.Enums.OrderStatus.Выполняется;
if (_orderStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool FinishOrder(OrderBindingModel model)
{
CheckModel(model);
model.Status = ConstructionCompanyDataModels.Enums.OrderStatus.Завершён;
model.DateEnd = DateTime.Now;
if (_orderStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(OrderBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Description))
{
throw new ArgumentNullException("Нет описания заказа", nameof(model.Description));
}
if (string.IsNullOrEmpty(model.Adress))
{
throw new ArgumentNullException("Нет адреса заказа", nameof(model.Adress));
}
if (model.Price <= 0)
{
throw new InvalidOperationException("Цена должна быть больше 0!");
}
if (string.IsNullOrEmpty(model.CustomerNumber))
{
throw new ArgumentNullException("Нет телефона", nameof(model.CustomerNumber));
}
_logger.LogInformation("Order. Description:{Description}. Adress:{Adress}. Price:{Price} Id:{Id}", model.Description, model.Adress, model.Price, model.Id);
}
}
}

View File

@ -0,0 +1,110 @@
using ConstructionCompanyContracts.BindingModels;
using ConstructionCompanyContracts.BusinessLogicContracts;
using ConstructionCompanyContracts.SearchModels;
using ConstructionCompanyContracts.StorageContracts;
using ConstructionCompanyContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConstructionCompanyBusinessLogic.BusinessLogics
{
public class PositionLogic : IPositionLogic
{
private readonly ILogger _logger;
private readonly IPositionStorage _positionStorage;
public PositionLogic(ILogger<PositionLogic> logger, IPositionStorage positionStorage)
{
_logger = logger;
_positionStorage = positionStorage;
}
public List<PositionViewModel>? ReadList(PositionSearchModel? model)
{
_logger.LogInformation("ReadList. PositionName:{PositionName}. Id:{Id}", model?.PositionName, model?.Id);
var list = model == null ? _positionStorage.GetFullList() : _positionStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public PositionViewModel? ReadElement(PositionSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. PositionName:{PositionName}. Id:{Id}", model.PositionName, model.Id);
var element = _positionStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public bool Create(PositionBindingModel model)
{
CheckModel(model);
if (_positionStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(PositionBindingModel model)
{
CheckModel(model);
if (_positionStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(PositionBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_positionStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(PositionBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.PositionName))
{
throw new ArgumentNullException("Нет названия материала", nameof(model.PositionName));
}
if (model.Salary < 0)
{
throw new ArgumentNullException("Зарплата должна быть не меньше 0!", nameof(model.Salary));
}
_logger.LogInformation("Position. IngredietnName:{PositionName}. Salary:{Salary}. Id:{Id}", model.PositionName, model.Salary, model.Id);
}
}
}

View File

@ -14,7 +14,6 @@ namespace ConstructionCompanyContracts.BusinessLogicContracts
List<EmployeeOrderViewModel>? ReadList(EmployeeOrderSearchModel? model);
EmployeeOrderViewModel? ReadElement(EmployeeOrderSearchModel model);
bool Create(EmployeeOrderBindingModel model);
bool Update(EmployeeOrderBindingModel model);
bool Delete(EmployeeOrderBindingModel model);
}
}

View File

@ -14,7 +14,6 @@ namespace ConstructionCompanyContracts.BusinessLogicContracts
List<MaterialOrderViewModel>? ReadList(MaterialOrderSearchModel? model);
MaterialOrderViewModel? ReadElement(MaterialOrderSearchModel model);
bool Create(MaterialOrderBindingModel model);
bool Update(MaterialOrderBindingModel model);
bool Delete(MaterialOrderBindingModel model);
}
}