119 lines
4.2 KiB
C#
119 lines
4.2 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using SushiBarContracts.BindingModels;
|
|
using SushiBarContracts.BusinessLogicsContracts;
|
|
using SushiBarContracts.SearchModels;
|
|
using SushiBarContracts.StoragesContracts;
|
|
using SushiBarContracts.ViewModels;
|
|
|
|
namespace SushiBarBusinessLogic.BusinessLogics
|
|
{
|
|
public class OrderLogic : IOrderLogic
|
|
{
|
|
private readonly ILogger _logger;
|
|
private readonly IOrderStorage _orderStorage;
|
|
|
|
public OrderLogic(ILogger logger, IOrderStorage orderStorage)
|
|
{
|
|
_logger = logger;
|
|
_orderStorage = orderStorage;
|
|
}
|
|
|
|
public bool CreateOrder(OrderBindingModel model)
|
|
{
|
|
CheckModel(model);
|
|
model.Status = SushiBarDataModels.Enums.OrderStatus.Accepted;
|
|
if (_orderStorage.Insert(model) == null)
|
|
{
|
|
_logger.LogWarning("Insert operation failed");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public bool DeliveryOrder(OrderBindingModel model)
|
|
{
|
|
CheckModel(model);
|
|
OrderViewModel? element = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
|
|
if (element == null || model.Status - element?.Status != 1)
|
|
{
|
|
_logger.LogWarning("Delivery order is not ready");
|
|
return false;
|
|
}
|
|
element.Status = SushiBarDataModels.Enums.OrderStatus.Ready;
|
|
return true;
|
|
}
|
|
|
|
public bool FinishOrder(OrderBindingModel model)
|
|
{
|
|
CheckModel(model);
|
|
OrderViewModel? element = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
|
|
if (element == null || model.Status - element?.Status != 1)
|
|
{
|
|
_logger.LogWarning("Finish order is not ready");
|
|
return false;
|
|
}
|
|
element.Status = SushiBarDataModels.Enums.OrderStatus.Issued;
|
|
return true;
|
|
}
|
|
|
|
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 TakeOrderInWork(OrderBindingModel model)
|
|
{
|
|
_logger.LogInformation("ReadList .Id:{Id}", model?.Id);
|
|
List<OrderViewModel> list = _orderStorage.GetFullList();
|
|
foreach (OrderViewModel order in list)
|
|
{
|
|
if (order.Status is >= (SushiBarDataModels.Enums.OrderStatus)1 and not (SushiBarDataModels.Enums.OrderStatus)3)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private void CheckModel(OrderBindingModel model, bool withParams = true)
|
|
{
|
|
if (model == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(model));
|
|
}
|
|
if (!withParams)
|
|
{
|
|
return;
|
|
}
|
|
if (model.SushiId <= 0)
|
|
{
|
|
throw new ArgumentNullException("Sushi id must be more then zero", nameof(model.SushiId));
|
|
}
|
|
if (model.Count <= 0)
|
|
{
|
|
throw new ArgumentNullException("Count must be more then zero", nameof(model.Count));
|
|
}
|
|
if (model.Sum <= 0)
|
|
{
|
|
throw new ArgumentNullException("Sum must be more then zero", nameof(model.Sum));
|
|
}
|
|
_logger.LogInformation("Order. OrderId:{Id} .Count:{Count} .Sum:{Sum} .Status{Status} .DateCreate{DateCreate}", model.Id, model.Count, model.Sum, model.Status.ToString(), model.DateCreate.ToString());
|
|
var element = _orderStorage.GetElement(new OrderSearchModel {
|
|
Id = model.Id
|
|
});
|
|
if (element != null && element.Id != model.Id)
|
|
{
|
|
throw new InvalidOperationException("This name is already exists");
|
|
}
|
|
}
|
|
}
|
|
}
|