ISEbd-21_Khaliullov_R.A._Co.../Canteen/CanteenBusinessLogic/BusinessLogics/OrderLogic.cs

142 lines
4.5 KiB
C#
Raw Normal View History

2024-04-30 18:48:42 +04:00
using CanteenContracts.BindingModels;
using CanteenContracts.BusinessLogicsContracts;
using CanteenContracts.SearchModel;
using CanteenContracts.StorageContracts;
using CanteenContracts.ViewModels;
using CanteenDataModels.Enums;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CanteenBusinessLogic.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 OrderViewModel? ReadElement(OrderSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. DateFrom:{DateFrom}. DateTo:{DateTo}. Id:{Id}", 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;
}
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);
if (model.OrderStatus != OrderStatus.Неизвестен && model.PaymentStatus != PaymentStatus.Неизвестно)
{
_logger.LogWarning("Invalid order status");
return false;
}
model.OrderStatus = OrderStatus.Принят; model.PaymentStatus = PaymentStatus.Неоплачен;
if (_orderStorage.Insert(model) == null)
{
model.OrderStatus = OrderStatus.Неизвестен;
model.PaymentStatus = PaymentStatus.Неизвестно;
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
private bool StatusUpdate(OrderBindingModel model, OrderStatus newStatusO, PaymentStatus newStatusP)
{
var viewModel = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
if (viewModel == null)
{
throw new ArgumentNullException(nameof(model));
}
if (viewModel.OrderStatus + 1 != newStatusO && viewModel.PaymentStatus + 1 != newStatusP)
{
_logger.LogWarning("Change status operation failed");
throw new InvalidOperationException();
}
model.OrderStatus = newStatusO;
model.PaymentStatus = newStatusP;
if (model.OrderStatus == OrderStatus.Готов && model.PaymentStatus == PaymentStatus.Частично)
{
model.DateImplement = DateTime.Now;
}
else
{
model.DateImplement = viewModel.DateImplement;
}
if (viewModel.CookId.HasValue)
{
model.CookId = viewModel.CookId.Value;
}
CheckModel(model, false);
if (_orderStorage.Update(model) == null)
{
_logger.LogWarning("Change status operation failed");
return false;
}
return true;
}
public bool TakeOrderInWork(OrderBindingModel model)
{
return StatusUpdate(model, OrderStatus.Выполняется, PaymentStatus.Частично);
}
public bool FinishOrder(OrderBindingModel model)
{
model.DateImplement = DateTime.Now;
return StatusUpdate(model, OrderStatus.Готов, PaymentStatus.Оплачен);
}
public bool DeliveryOrder(OrderBindingModel model)
{
return StatusUpdate(model, OrderStatus.Выдан, PaymentStatus.Оплачен);
}
private void CheckModel(OrderBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.OrderPrice <= 0)
{
throw new ArgumentNullException("Стоимость заказа должна быть больше 0", nameof(model.OrderPrice));
}
_logger.LogInformation("Order. Id: {Id}. OrderPrice: {OrderPrice}. DishId: {DishId}", model.Id, model.OrderPrice, model.DishId);
var element = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
if (element != null && element.Id == model.Id)
{
throw new InvalidOperationException("Заказ с таким номером уже есть");
}
}
}
}