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);
|
|
|
|
|
|
2023-02-12 23:20:34 +04:00
|
|
|
|
if (model.Status != OrderStatus.Неизвестен)
|
2023-01-29 22:10:01 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Invalid order status");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-12 23:20:34 +04:00
|
|
|
|
model.Status = OrderStatus.Принят;
|
2023-01-29 22:10:01 +04:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2023-02-12 23:20:34 +04:00
|
|
|
|
if (model.Status != OrderStatus.Принят)
|
2023-02-12 18:21:48 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Invalid order status");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-12 23:20:34 +04:00
|
|
|
|
model.Status = OrderStatus.Выполняется;
|
2023-02-12 18:21:48 +04:00
|
|
|
|
|
2023-02-12 23:20:34 +04:00
|
|
|
|
if (_orderStorage.Update(model) == null)
|
2023-02-12 18:21:48 +04:00
|
|
|
|
{
|
2023-02-12 23:20:34 +04:00
|
|
|
|
_logger.LogWarning("Update operation failed");
|
2023-02-12 18:21:48 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2023-02-12 23:20:34 +04:00
|
|
|
|
if (model.Status != OrderStatus.Выполняется)
|
2023-02-12 18:21:48 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Invalid order status");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-12 23:20:34 +04:00
|
|
|
|
model.Status = OrderStatus.Готов;
|
2023-02-12 18:21:48 +04:00
|
|
|
|
|
2023-02-12 23:20:34 +04:00
|
|
|
|
if (_orderStorage.Update(model) == null)
|
2023-02-12 18:21:48 +04:00
|
|
|
|
{
|
2023-02-12 23:20:34 +04:00
|
|
|
|
_logger.LogWarning("Update operation failed");
|
2023-02-12 18:21:48 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2023-02-12 23:20:34 +04:00
|
|
|
|
if (model.Status != OrderStatus.Готов)
|
2023-02-12 18:21:48 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Invalid order status");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-12 23:20:34 +04:00
|
|
|
|
model.Status = OrderStatus.Выдан;
|
|
|
|
|
model.DateImplement = DateTime.Now;
|
2023-02-12 18:21:48 +04:00
|
|
|
|
|
2023-02-12 23:20:34 +04:00
|
|
|
|
if (_orderStorage.Update(model) == null)
|
2023-02-12 18:21:48 +04:00
|
|
|
|
{
|
2023-02-12 23:20:34 +04:00
|
|
|
|
_logger.LogWarning("Update operation failed");
|
2023-02-12 18:21:48 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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("Заказ с таким идентификатором уже есть");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|