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);
2023-12-01 19:59:06 +04:00
var order = _orderStorage.GetElement(model);
2023-05-20 00:08:47 +04:00
var assembly = _assemblyStorage.GetElement(assemblymodel);
2023-12-01 19:59:06 +04:00
if (order == null || assembly == null)
2023-05-20 00:08:47 +04:00
{
return false;
}
2023-12-01 19:59:06 +04:00
_logger.LogInformation("AddAssemblyToOrder find. Id:{Id}", order.Id);
2023-05-20 00:08:47 +04:00
2023-12-01 19:59:06 +04:00
order.OrderAssemblies[assembly.Id] = (assembly, amount);
2023-05-20 00:08:47 +04:00
_orderStorage.Update(new()
{
2023-12-01 19:59:06 +04:00
Id = order.Id,
Status = order.Status,
Sum = order.Sum + assembly.Price * amount,
ClientId = order.ClientId,
AssemblyOrders = order.OrderAssemblies
2023-05-20 00:08:47 +04:00
});
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
}
}