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

165 lines
5.1 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;
using PizzeriaDataModels.Enums;
using System.Xml.Linq;
namespace PizzeriaBusinessLogic.BusinessLogic
{
public class OrderLogic : IOrderLogic
{
private readonly ILogger _logger;
private readonly IOrderStorage _orderStorage;
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
{
_logger = logger;
_orderStorage = orderStorage;
}
public bool CreateOrder(OrderBindingModel model)
{
CheckModel(model);
if (model.Status == OrderStatus.Неизвестен)
{
_logger.LogWarning("Order status unknown");
return false;
}
model.Status = OrderStatus.Принят;
if (_orderStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
{
_logger.LogInformation("ReadList. OrderId: {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)
{
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)
{
/*Неизвестен = -1,
Принят = 0,
Выполняется = 1,
Готов = 2,
Выдан = 3*/
CheckModel(model, false);
var order = _orderStorage.GetElement(new OrderSearchModel {
Id = model.Id
});
if (order == null)
{
throw new ArgumentNullException(nameof(order));
}
model.ProductId = order.ProductId;
model.DateCreate = order.DateCreate;
model.DateImplement = order.DateImplement;
model.Status = order.Status;
model.Count = order.Count;
model.Sum = order.Sum;
return true;
/*
if (model.Status != status - 1)
{
_logger.LogWarning("Status update to " + status + " operation failed");
return false;
}
model.Status = status;
if (model.Status == OrderStatus.Выдан)
{
model.DateImplement = DateTime.Now;
}
if (_orderStorage.Update(model) == null)
{
model.Status--;
_logger.LogWarning("Changing status operation faled");
return false;
}
*/
/*
if (model.Status == OrderStatus.Готов)
{
model.DateImplement = DateTime.Now;
}
else
{
model.DateImplement = order.DateImplement;
}
if (_orderStorage.Update(model) == null)
{
_logger.LogWarning("Change status operation failed");
return false;
}
return true;
*/
}
private void CheckModel(OrderBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.ProductId <= 0)
{
throw new ArgumentNullException("Некорректный идентификатор изделия", nameof(model.ProductId));
}
if (model.Count <= 0)
{
throw new ArgumentNullException("В заказе должно быть хотя бы одно изделие", nameof(model.Count));
}
if (model.Sum <= 0)
{
throw new ArgumentNullException("Стоимость заказа должна быть больше 0", nameof(model.Sum));
}
_logger.LogInformation("Order. Id: {Id}. Sum: {Sum}. ProductId: {ProductId}. ReinforcedCount: {Count}", model.Id, model.Sum, model.ProductId, model.Count);
}
}
}