PIbd-23_Polevoy_S.V._Flower.../FlowerShop/FlowerShopBusinessLogic/BusinessLogics/OrderLogic.cs

144 lines
4.1 KiB
C#
Raw Normal View History

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;
}
public bool CreateOrder(OrderBindingModel model)
{
CheckModel(model);
if (model.Status != OrderStatus.Unknown)
{
_logger.LogWarning("Invalid order status");
return false;
}
model.Status = OrderStatus.Accepted;
if (_orderStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool TakeOrderInWork(OrderBindingModel model)
{
CheckModel(model, false);
if (model.Status != OrderStatus.Accepted)
{
_logger.LogWarning("Invalid order status");
return false;
}
model.Status = OrderStatus.Processing;
if (_orderStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
}
return true;
}
public bool FinishOrder(OrderBindingModel model)
{
CheckModel(model, false);
if (model.Status != OrderStatus.Processing)
{
_logger.LogWarning("Invalid order status");
return false;
}
model.Status = OrderStatus.Ready;
if (_orderStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
}
return true;
}
public bool DeliveryOrder(OrderBindingModel model)
{
CheckModel(model, false);
if (model.Status != OrderStatus.Ready)
{
_logger.LogWarning("Invalid order status");
return false;
}
model.Status = OrderStatus.Delivered;
if (_orderStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
}
return true;
}
private void CheckModel(OrderBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.Sum <= 0)
{
throw new ArgumentNullException("Стоимость должна быть больше 0", nameof(model.Sum));
}
_logger.LogInformation("Order. Id: {Id}. Sum: {Sum}. BouquetId: {BouquetId}", model.Id, model.Sum, model.BouquetId);
var element = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
if (element != null && element.Id == model.Id)
{
throw new InvalidOperationException("Заказ с таким идентификатором уже есть");
}
}
}
}