PIbd-21_Musoev_D.T._FlowerShop/FlowerShop/FlowerShopBusinessLogic/BusinessLogics/OrderLogic.cs

148 lines
4.5 KiB
C#
Raw Normal View History

2023-04-14 01:56:19 +04:00
using FlowerShopContracts.BindingModels;
using FlowerShopContracts.BusinessLogicsContracts;
using FlowerShopContracts.SearchModels;
using FlowerShopContracts.StoragesContracts;
using FlowerShopContracts.ViewModels;
using FlowerShopDataModels.Enums;
using Microsoft.Extensions.Logging;
namespace FlowerShopBusinessLogic.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. 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;
}
2023-06-13 23:37:46 +04:00
2023-04-14 01:56:19 +04:00
public bool CreateOrder(OrderBindingModel model)
{
CheckModel(model);
if (model.Status != OrderStatus.Неизвестен)
{
_logger.LogWarning("Invalid order status");
return false;
}
model.Status = OrderStatus.Принят;
if (_orderStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
2023-06-13 23:37:46 +04:00
2023-04-14 01:56:19 +04:00
public bool TakeOrderInWork(OrderBindingModel model)
{
2023-06-13 23:37:46 +04:00
CheckModel(model, false);
if (_orderStorage.GetElement(new OrderSearchModel { Id=model.Id })?.Status != OrderStatus.Принят)
{
_logger.LogWarning("Invalid order status");
return false;
}
model.Status = OrderStatus.Выполняется;
if (_orderStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
}
return true;
2023-04-14 01:56:19 +04:00
}
public bool FinishOrder(OrderBindingModel model)
{
2023-06-13 23:37:46 +04:00
CheckModel(model, false);
if (_orderStorage.GetElement(new OrderSearchModel { Id = model.Id })?.Status != OrderStatus.Выполняется)
2023-04-14 01:56:19 +04:00
{
2023-06-13 23:37:46 +04:00
_logger.LogWarning("Invalid order status");
2023-04-14 01:56:19 +04:00
return false;
}
2023-06-13 23:37:46 +04:00
model.Status = OrderStatus.Готов;
model.DateImplement = DateTime.Now;
if (_orderStorage.Update(model) == null)
2023-04-14 01:56:19 +04:00
{
2023-06-13 23:37:46 +04:00
_logger.LogWarning("Update operation failed");
2023-04-14 01:56:19 +04:00
}
2023-06-13 23:37:46 +04:00
return true;
}
public bool DeliveryOrder(OrderBindingModel model)
{
CheckModel(model, false);
var order = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
if (order?.Status != OrderStatus.Готов)
2023-04-14 01:56:19 +04:00
{
2023-06-13 23:37:46 +04:00
_logger.LogWarning("Invalid order status");
return false;
2023-04-14 01:56:19 +04:00
}
2023-06-13 23:37:46 +04:00
model.Status = OrderStatus.Выдан;
model.DateImplement = order.DateImplement;
2023-04-14 01:56:19 +04:00
if (_orderStorage.Update(model) == null)
{
2023-06-13 23:37:46 +04:00
_logger.LogWarning("Update operation failed");
2023-04-14 01:56:19 +04:00
}
2023-06-13 23:37:46 +04:00
2023-04-14 01:56:19 +04:00
return true;
}
2023-06-13 23:37:46 +04:00
2023-04-14 01:56:19 +04:00
private void CheckModel(OrderBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
2023-06-13 23:37:46 +04:00
if (model.Sum <= 0)
2023-04-14 01:56:19 +04:00
{
2023-06-13 23:37:46 +04:00
throw new ArgumentNullException("Стоимость должна быть больше 0", nameof(model.Sum));
2023-04-14 01:56:19 +04:00
}
2023-06-13 23:37:46 +04:00
_logger.LogInformation("Order. Id: {Id}. Sum: {Sum}. BouquetId: {BouquetId}", model.Id, model.Sum, model.BouquetId);
2023-04-14 01:56:19 +04:00
2023-06-13 23:37:46 +04:00
var element = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
if (element != null && element.Id == model.Id)
2023-04-14 01:56:19 +04:00
{
2023-06-13 23:37:46 +04:00
throw new InvalidOperationException("Заказ с таким идентификатором уже есть");
2023-04-14 01:56:19 +04:00
}
}
}
}