CourseWork_CompShop/ComputerShopProvider/ComputerShopBusinessLogic/BusinessLogics/OrderLogic.cs

170 lines
5.9 KiB
C#
Raw Normal View History

2023-05-17 14:20:02 +04:00
using ComputerShopContracts.BindingModels;
using ComputerShopContracts.BusinessLogicContracts;
using ComputerShopContracts.SearchModels;
2023-05-20 00:08:47 +04:00
using ComputerShopContracts.StorageContracts;
2023-05-17 14:20:02 +04:00
using ComputerShopContracts.ViewModels;
2023-05-20 00:08:47 +04:00
using ComputerShopDataModels.Enums;
using Microsoft.Extensions.Logging;
2023-05-17 14:20:02 +04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerShopBusinessLogic.BusinessLogics
{
public class OrderLogic : IOrderLogic
{
2023-05-20 00:08:47 +04:00
private readonly ILogger _logger;
private readonly IOrderStorage _orderStorage;
private readonly IAssemblyStorage _assemblyStorage;
2023-05-20 08:34:33 +04:00
private readonly ISupplyStorage _supplyStorage;
public OrderLogic(ILogger<ComponentLogic> logger, IOrderStorage orderStorage, IAssemblyStorage assemblyStorage, ISupplyStorage supplyStorage)
2023-05-20 00:08:47 +04:00
{
_logger = logger;
_orderStorage = orderStorage;
_assemblyStorage = assemblyStorage;
2023-05-20 08:34:33 +04:00
_supplyStorage = supplyStorage;
2023-05-20 00:08:47 +04:00
}
2023-05-17 14:20:02 +04:00
public bool CreateOrder(OrderBindingModel model)
{
2023-05-20 00:08:47 +04:00
CheckModel(model);
if (_orderStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
{
_logger.LogInformation("ReadList. Orderid:{ 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 bool Update(OrderBindingModel model)
{
CheckModel(model);
if (_orderStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(OrderBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_orderStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
2023-05-17 14:20:02 +04:00
}
2023-05-20 00:08:47 +04:00
public bool TakeOrderInWork(OrderBindingModel model)
2023-05-17 14:20:02 +04:00
{
2023-05-20 00:08:47 +04:00
return StatusUpdate(model, OrderStatus.Выполняется);
2023-05-17 14:20:02 +04:00
}
public bool DeliveryOrder(OrderBindingModel model)
{
2023-05-20 00:08:47 +04:00
return StatusUpdate(model, OrderStatus.Выдан);
2023-05-17 14:20:02 +04:00
}
public bool FinishOrder(OrderBindingModel model)
{
2023-05-20 00:08:47 +04:00
return StatusUpdate(model, OrderStatus.Готов);
2023-05-17 14:20:02 +04:00
}
2023-05-20 00:08:47 +04:00
public bool StatusUpdate(OrderBindingModel model, OrderStatus _newStatus)
2023-05-17 14:20:02 +04:00
{
2023-05-20 00:08:47 +04:00
var viewModel = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
if (viewModel == null)
{
throw new ArgumentNullException(nameof(model));
}
if (viewModel.Status + 1 != _newStatus)
{
_logger.LogWarning("Status update to " + _newStatus.ToString() + " operation failed. Order status incorrect.");
return false;
}
model.Status = _newStatus;
if (model.Status == OrderStatus.Выдан) model.DateImplement = DateTime.Now;
else
{
model.DateImplement = viewModel.DateImplement;
}
CheckModel(model, false);
if (_orderStorage.Update(model) == null)
{
model.Status--;
_logger.LogWarning("Update operation failed");
return false;
}
return true;
2023-05-17 14:20:02 +04:00
}
2023-05-20 00:08:47 +04:00
private void CheckModel(OrderBindingModel model, bool withParams = true)
2023-05-17 14:20:02 +04:00
{
2023-05-20 00:08:47 +04:00
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.ClientId < 0)
{
throw new ArgumentNullException("Некорректный идентификатор у клиента", nameof(model.ClientId));
}
if (model.Sum <= 0)
{
throw new ArgumentNullException("Сумма заказа должна быть больше 0", nameof(model.Sum));
}
_logger.LogInformation("Order. OrderID:{Id}. Sum:{ Sum}. ClientId: { ClientId}", model.Id, model.Sum, model.ClientId);
2023-05-17 14:20:02 +04:00
}
2023-05-20 00:08:47 +04:00
public bool AddAssembly(OrderSearchModel model, AssemblySearchModel assemblymodel, int amount)
2023-05-17 14:20:02 +04:00
{
2023-05-20 00:08:47 +04:00
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
2023-05-17 14:20:02 +04:00
2023-05-20 00:08:47 +04:00
_logger.LogInformation("AddAssemblyToOrder. AssemblyName:{AssemblyName}.Id:{ Id}", assemblymodel.AssemblyName, model.Id);
var element = _orderStorage.GetElement(model);
var assembly = _assemblyStorage.GetElement(assemblymodel);
if (element == null || assembly == null)
{
return false;
}
_logger.LogInformation("AddAssemblyToOrder find. Id:{Id}", element.Id);
element.AssemblyOrders[assembly.Id] = (assembly, amount);
_orderStorage.Update(new()
{
Id = element.Id,
Status = element.Status,
Sum = element.Sum + assembly.Price * amount,
ClientId = element.ClientId,
AssemblyOrders = element.AssemblyOrders
});
return true;
2023-05-17 14:20:02 +04:00
}
2023-05-20 08:34:33 +04:00
2023-05-17 14:20:02 +04:00
}
}