PIbd-22_Katysheva_N.E._Pizz.../Pizzeria/PizzeriaBusinessLogic/BusinessLogic/OrderLogic.cs

166 lines
5.9 KiB
C#
Raw Normal View History

2024-02-28 01:32:56 +04:00
using Microsoft.Extensions.Logging;
using PizzeriaContracts.BindingModels;
using PizzeriaContracts.BusinessLogicsContracts;
using PizzeriaContracts.SearchModels;
using PizzeriaContracts.StorageContracts;
using PizzeriaContracts.ViewModels;
2024-06-21 15:07:54 +04:00
using PizzeriaDataModels.Enums;
2024-06-21 15:13:53 +04:00
using System.Xml.Linq;
2024-02-28 01:32:56 +04:00
namespace PizzeriaBusinessLogic.BusinessLogic
{
public class OrderLogic : IOrderLogic
{
private readonly ILogger _logger;
private readonly IOrderStorage _orderStorage;
2024-06-21 15:13:53 +04:00
static readonly object _locker = new object();
2024-02-28 01:32:56 +04:00
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
{
_logger = logger;
_orderStorage = orderStorage;
}
2024-06-21 15:13:53 +04:00
public OrderViewModel? ReadElement(OrderSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. ClientId:{ClientId}.Status:{Status}.ImplementerId:{ImplementerId}.DateFrom:{DateFrom}.DateTo:{DateTo}OrderId:{Id}",
model.ClientId, model.Status, model.ImplementerId, model.DateFrom, model.DateTo, model.Id);
var element = _orderStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
2024-02-28 01:32:56 +04:00
public bool CreateOrder(OrderBindingModel model)
{
2024-06-21 15:07:54 +04:00
2024-02-28 01:32:56 +04:00
CheckModel(model);
2024-03-11 00:29:19 +04:00
if (model.Status != OrderStatus.Неизвестен)
2024-02-28 01:32:56 +04:00
{
2024-03-11 00:29:19 +04:00
_logger.LogWarning("Invalid order status");
2024-02-28 01:32:56 +04:00
return false;
}
model.Status = OrderStatus.Принят;
if (_orderStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
2024-06-21 14:39:07 +04:00
2024-03-11 00:29:19 +04:00
2024-02-28 01:32:56 +04:00
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
{
2024-06-21 15:13:53 +04:00
_logger.LogInformation("ReadList. ClientId:{ClientId}.Status:{Status}.ImplementerId:{ImplementerId}.DateFrom:{DateFrom}.DateTo:{DateTo}OrderId:{Id}",
model?.ClientId, model?.Status, model?.ImplementerId, model?.DateFrom, model?.DateTo, model?.Id);
2024-02-28 01:32:56 +04:00
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)
{
return ChangeStatus(model, OrderStatus.Выполняется);
}
public bool FinishOrder(OrderBindingModel model)
{
return ChangeStatus(model, OrderStatus.Готов);
}
public bool DeliveryOrder(OrderBindingModel model)
{
return ChangeStatus(model, OrderStatus.Выдан);
}
public bool ChangeStatus(OrderBindingModel model, OrderStatus status)
{
CheckModel(model, false);
2024-06-21 15:07:54 +04:00
2024-03-12 20:03:49 +04:00
var searchOrder = new OrderSearchModel { Id = model.Id };
var order = _orderStorage.GetElement(searchOrder);
2024-06-21 15:07:54 +04:00
2024-02-28 01:32:56 +04:00
if (order == null)
{
throw new ArgumentNullException(nameof(order));
}
2024-03-11 00:29:19 +04:00
model.PizzaId = order.PizzaId;
model.Count = order.Count;
model.Sum = order.Count;
2024-02-28 01:32:56 +04:00
model.DateCreate = order.DateCreate;
model.DateImplement = order.DateImplement;
model.Status = order.Status;
if (model.Status != status - 1)
{
_logger.LogWarning("Status update to " + status + " operation failed");
return false;
}
model.Status = status;
2024-06-21 15:13:53 +04:00
if (order.ImplementerId.HasValue)
{
model.ImplementerId = order.ImplementerId;
}
if (model.Status == OrderStatus.Выдан)
{
model.DateImplement = DateTime.Now;
}
2024-03-12 23:48:16 +04:00
if (model.Status == OrderStatus.Готов)
2024-02-28 01:32:56 +04:00
{
model.DateImplement = DateTime.Now;
}
if (_orderStorage.Update(model) == null)
{
model.Status--;
_logger.LogWarning("Changing status operation faled");
return false;
}
return true;
2024-06-21 15:07:54 +04:00
2024-02-28 01:32:56 +04:00
}
private void CheckModel(OrderBindingModel model, bool withParams = true)
{
2024-06-21 15:07:54 +04:00
2024-02-28 01:32:56 +04:00
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
2024-05-07 23:20:02 +04:00
if (model.ClientId <= 0)
{
throw new ArgumentNullException("Некорректный идентификатор клиента", nameof(model.ClientId));
}
2024-03-11 00:29:19 +04:00
if (model.PizzaId <= 0)
2024-02-28 01:32:56 +04:00
{
2024-03-11 00:29:19 +04:00
throw new ArgumentNullException("Некорректный идентификатор пиццы", nameof(model.PizzaId));
2024-02-28 01:32:56 +04:00
}
if (model.Count <= 0)
{
2024-03-11 00:29:19 +04:00
throw new ArgumentNullException("В заказе должно быть хотя бы одно пицца", nameof(model.Count));
2024-02-28 01:32:56 +04:00
}
if (model.Sum <= 0)
{
throw new ArgumentNullException("Стоимость заказа должна быть больше 0", nameof(model.Sum));
}
2024-04-10 02:43:55 +04:00
_logger.LogInformation("Order. Id: {Id}. Sum: {Sum}. PizzaId: {PizzaId}. PizzaCount: {Count}", model.Id, model.Sum, model.PizzaId, model.Count);
2024-02-28 01:32:56 +04:00
}
}
}