2023-01-30 19:25:38 +04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using SushiBarContracts.BindingModels;
|
|
|
|
|
using SushiBarContracts.BusinessLogicsContracts;
|
|
|
|
|
using SushiBarContracts.SearchModels;
|
|
|
|
|
using SushiBarContracts.StoragesContracts;
|
|
|
|
|
using SushiBarContracts.ViewModels;
|
2023-01-30 23:13:26 +04:00
|
|
|
|
using SushiBarDataModels.Enums;
|
2023-01-30 19:25:38 +04:00
|
|
|
|
|
|
|
|
|
namespace SushiBarBusinessLogic.BusinessLogics
|
|
|
|
|
{
|
|
|
|
|
public class OrderLogic : IOrderLogic
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IOrderStorage _orderStorage;
|
|
|
|
|
|
2023-01-31 01:11:18 +04:00
|
|
|
|
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
|
2023-01-30 19:25:38 +04:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_orderStorage = orderStorage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool CreateOrder(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
2023-01-31 01:11:18 +04:00
|
|
|
|
model.Status = OrderStatus.Accepted;
|
2023-01-30 19:25:38 +04:00
|
|
|
|
if (_orderStorage.Insert(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Insert operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool DeliveryOrder(OrderBindingModel model)
|
|
|
|
|
{
|
2023-01-30 23:13:26 +04:00
|
|
|
|
return UpdateStatus(model, OrderStatus.Issued);
|
2023-01-30 19:25:38 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool FinishOrder(OrderBindingModel model)
|
|
|
|
|
{
|
2023-01-30 23:13:26 +04:00
|
|
|
|
CheckModel(model, false);
|
|
|
|
|
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
|
|
|
|
if (_orderStorage.Delete(model) == null)
|
2023-01-30 19:25:38 +04:00
|
|
|
|
{
|
2023-01-30 23:13:26 +04:00
|
|
|
|
_logger.LogWarning("Delete operation failed");
|
2023-01-30 19:25:38 +04:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
{
|
2023-01-30 23:13:26 +04:00
|
|
|
|
return UpdateStatus(model, OrderStatus.Performed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool UpdateStatus(OrderBindingModel model, OrderStatus status)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
if (model.Status + 1 != status)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Status update operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
model.Status = status;
|
|
|
|
|
if (model.Status is OrderStatus.Issued)
|
2023-01-30 19:25:38 +04:00
|
|
|
|
{
|
2023-01-30 23:13:26 +04:00
|
|
|
|
model.DateImplement = DateTime.Now;
|
2023-01-30 19:25:38 +04:00
|
|
|
|
}
|
2023-01-30 23:13:26 +04:00
|
|
|
|
if (_orderStorage.Update(model) == null)
|
|
|
|
|
{
|
|
|
|
|
model.Status--;
|
|
|
|
|
_logger.LogWarning("Update operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2023-01-30 19:25:38 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|