90 lines
3.5 KiB
C#
90 lines
3.5 KiB
C#
|
using DinerContracts.BindingModels;
|
|||
|
using DinerContracts.BusinessLogicsContacts;
|
|||
|
using DinerContracts.SearchModels;
|
|||
|
using DinerContracts.StoragesContracts;
|
|||
|
using DinerContracts.ViewModels;
|
|||
|
using DinerDataModels.Enums;
|
|||
|
using Microsoft.Extensions.Logging;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace DineryBusinessLogic.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 (_orderStorage.Insert(model) == null) {
|
|||
|
_logger.LogWarning("Insert operation failed");
|
|||
|
return false;
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
public bool DeliveryOrder(OrderBindingModel model)
|
|||
|
{
|
|||
|
return StatusUpdate(model, OrderStatus.Выдан);
|
|||
|
}
|
|||
|
|
|||
|
public bool FinishOrder(OrderBindingModel model)
|
|||
|
{
|
|||
|
return StatusUpdate(model, OrderStatus.Готов);
|
|||
|
}
|
|||
|
|
|||
|
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)
|
|||
|
{
|
|||
|
return StatusUpdate(model, OrderStatus.Выполняется);
|
|||
|
}
|
|||
|
|
|||
|
private void CheckModel(OrderBindingModel model, bool withParams = true) {
|
|||
|
if (model == null) throw new ArgumentNullException(nameof(model));
|
|||
|
if (!withParams) return;
|
|||
|
if (string.IsNullOrEmpty((model.ID).ToString()))
|
|||
|
throw new ArgumentNullException("Нет ID заказа", nameof(model.ID));
|
|||
|
if (model.Sum <= 0)
|
|||
|
throw new ArgumentNullException("Цена заказа должна быть больше 0", nameof(model.Sum));
|
|||
|
_logger.LogInformation("Order. ProductID:{ProductID}. Count:{Count}. Sum:{Sum}. Status:{Status}. " +
|
|||
|
"DateCreate:{DateCreate}. DateImplement:{DateImplement}. ID:{ID}",
|
|||
|
model.ProductID, model.Count, model.Sum, model.Status, model.DateCreate, model.DateImplement, model.ID);
|
|||
|
var element = _orderStorage.GetElement(new OrderSearchModel { ID = model.ID });
|
|||
|
if (element == null)
|
|||
|
throw new InvalidOperationException("Нет такого заказа");
|
|||
|
}
|
|||
|
private bool StatusUpdate(OrderBindingModel model, OrderStatus newOrderStatus) {
|
|||
|
CheckModel(model, false);
|
|||
|
|
|||
|
if (model.Status + 1 != newOrderStatus) {
|
|||
|
_logger.LogWarning("Status update to " + newOrderStatus.ToString() + " operation failed.");
|
|||
|
return false;
|
|||
|
}
|
|||
|
model.Status = newOrderStatus;
|
|||
|
if (model.Status == OrderStatus.Готов) model.DateImplement = DateTime.Now;
|
|||
|
return true;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|