PIbd-22_Gerimovich.I.M._Fur.../FurnitureAssembly/FurnitureAssemblyBusinessLogic/BussinessLogic/OrderLogic.cs

160 lines
5.0 KiB
C#
Raw Normal View History

2024-02-28 01:13:41 +04:00
using FurnitureAssemblyContracts.BindingModels;
using FurnitureAssemblyContracts.BusinessLogicsContracts;
using FurnitureAssemblyContracts.SearchModels;
using FurnitureAssemblyContracts.StoragesContracts;
using FurnitureAssemblyContracts.ViewModels;
using FurnitureAssemblyDataModels.Enums;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FurnitureAssemblyBusinessLogic.BussinessLogic
{
public class OrderLogic : IOrderLogic
{
2024-02-28 11:03:17 +04:00
private readonly ILogger _logger;
private readonly IOrderStorage _orderStorage;
2024-02-28 01:13:41 +04:00
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
{
_logger = logger;
_orderStorage = orderStorage;
}
2024-02-28 01:13:41 +04:00
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
{
_logger.LogInformation("ReadList. Id:{Id}", model?.Id);
2024-02-28 01:13:41 +04:00
var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model);
if (list == null)
2024-02-28 01:13:41 +04:00
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
2024-02-28 01:13:41 +04:00
public bool CreateOrder(OrderBindingModel model)
{
CheckModel(model);
if (model.Status != OrderStatus.Неизвестен)
2024-02-28 01:13:41 +04:00
{
_logger.LogWarning("Insert operation failed, incorrect order status");
return false;
}
model.Status = OrderStatus.Принят;
if (_orderStorage.Insert(model) == null)
2024-02-28 01:13:41 +04:00
{
model.Status = OrderStatus.Неизвестен;
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool TakeOrderInWork(OrderBindingModel model)
{
return StatusUpdate(model, OrderStatus.Выполняется);
}
public bool FinishOrder(OrderBindingModel model)
{
return StatusUpdate(model, OrderStatus.Готов);
}
public bool DeliveryOrder(OrderBindingModel model)
{
return StatusUpdate(model, OrderStatus.Выдан);
}
2024-02-28 01:13:41 +04:00
private void CheckModel(OrderBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
2024-02-28 01:13:41 +04:00
if (!withParams)
{
return;
}
if (model.Count <= 0)
2024-02-28 01:13:41 +04:00
{
throw new ArgumentNullException("В заказе не может быть 0 изделий", nameof(model.Count));
}
if (model.Sum <= 0)
2024-02-28 01:13:41 +04:00
{
throw new ArgumentNullException("Суммарная стоимость заказа должна быть больше 0", nameof(model.Sum));
}
if (model.FurnitureId < 0)
2024-02-28 01:13:41 +04:00
{
throw new ArgumentNullException("Некорректный id у изделия", nameof(model.FurnitureId));
}
if (model.DateCreate > model.DateImplement)
2024-02-28 01:13:41 +04:00
{
throw new InvalidOperationException("Дата создания должна быть более ранней, нежели дата завершения");
}
if (model.ClientId < 0)
{
throw new ArgumentNullException("Некорректный идентификатор у клиента", nameof(model.ClientId));
}
2024-02-28 01:13:41 +04:00
_logger.LogInformation("Order. OrderId:{Id}. Sum:{Sum}. ClientId:{ClientId}. FurnitureId:{Id}", model.Id, model.Sum, model.ClientId, model.FurnitureId);
2024-02-28 01:13:41 +04:00
}
public bool StatusUpdate(OrderBindingModel model, OrderStatus newOrderStatus)
{
var viewModel = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
if (viewModel == null)
2024-02-28 01:13:41 +04:00
{
throw new ArgumentNullException(nameof(model));
}
if (viewModel.Status + 1 != newOrderStatus)
2024-02-28 01:13:41 +04:00
{
_logger.LogWarning("Status update operation failed. New status " + newOrderStatus.ToString() + "incorrect");
2024-02-28 01:13:41 +04:00
return false;
}
model.Status = newOrderStatus;
if (model.Status == OrderStatus.Выдан)
2024-02-28 01:13:41 +04:00
{
model.DateImplement = DateTime.Now;
}
else
{
model.DateImplement = viewModel.DateImplement;
}
CheckModel(model, false);
if (_orderStorage.Update(model) == null)
2024-02-28 01:13:41 +04:00
{
model.Status--;
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
}
}