2023-01-29 22:10:01 +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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2023-02-12 18:21:48 +04:00
|
|
|
|
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;
|
2023-01-29 22:10:01 +04:00
|
|
|
|
}
|
|
|
|
|
public bool FinishOrder(OrderBindingModel model)
|
|
|
|
|
{
|
2023-02-12 18:21:48 +04:00
|
|
|
|
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;
|
2023-01-29 22:10:01 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool DeliveryOrder(OrderBindingModel model)
|
|
|
|
|
{
|
2023-02-12 18:21:48 +04:00
|
|
|
|
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;
|
2023-01-29 22:10:01 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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("Заказ с таким идентификатором уже есть");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|