133 lines
4.5 KiB
C#
133 lines
4.5 KiB
C#
|
using FishFactoryContracts.BindingModels;
|
|||
|
using FishFactoryContracts.SearchModels;
|
|||
|
using FishFactoryContracts.StoragesContracts;
|
|||
|
using FishFactoryContracts.ViewModels;
|
|||
|
using FishFactoryDataModel.Enums;
|
|||
|
using Microsoft.Extensions.Logging;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace FishFactoryBusinessLogic.BusinessLogic
|
|||
|
{
|
|||
|
internal class OrderLogic
|
|||
|
{
|
|||
|
//Класс с логикой для заказов будет отвечать за получение списка заказов,
|
|||
|
//создания заказа и смены его статусов. Следует учитывать, что у заказа можно
|
|||
|
//менять статус на новый, если его текущий статус предшествует новому
|
|||
|
|
|||
|
private readonly ILogger _logger;
|
|||
|
private readonly IOrderStorage _orderStorage;
|
|||
|
|
|||
|
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
|
|||
|
{
|
|||
|
_logger = logger;
|
|||
|
_orderStorage = orderStorage;
|
|||
|
}
|
|||
|
|
|||
|
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 OrderViewModel? ReadElement(OrderSearchModel model)
|
|||
|
//{
|
|||
|
// if (model == null)
|
|||
|
// {
|
|||
|
// throw new ArgumentNullException(nameof(model));
|
|||
|
// }
|
|||
|
// _logger.LogInformation("ReadElement. OrderName:{OrderName}.Id:{Id}", model.OrderName, 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 bool Create(OrderBindingModel model)
|
|||
|
{
|
|||
|
CheckModel(model);
|
|||
|
|
|||
|
model.Status = OrderStatus.Принят;
|
|||
|
|
|||
|
if (_orderStorage.Insert(model) == null)
|
|||
|
{
|
|||
|
model.Status = OrderStatus.Неизвестен;
|
|||
|
_logger.LogWarning("Insert operation failed");
|
|||
|
return false;
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
public bool ToNextStatus(OrderBindingModel model, OrderStatus orderStatus)
|
|||
|
{
|
|||
|
CheckModel(model);
|
|||
|
if (model.Status != orderStatus - 1)
|
|||
|
{
|
|||
|
_logger.LogWarning("Status update to " + orderStatus + " operation failed");
|
|||
|
return false;
|
|||
|
}
|
|||
|
model.Status = orderStatus;
|
|||
|
|
|||
|
if (model.Status == OrderStatus.Выдан)
|
|||
|
{
|
|||
|
model.DateImplement = DateTime.Now;
|
|||
|
}
|
|||
|
|
|||
|
if (_orderStorage.Update(model) == null)
|
|||
|
{
|
|||
|
model.Status--;
|
|||
|
_logger.LogWarning("Update operation failed");
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
public bool Update(OrderBindingModel model)
|
|||
|
{
|
|||
|
CheckModel(model);
|
|||
|
if (_orderStorage.Update(model) == null)
|
|||
|
{
|
|||
|
_logger.LogWarning("Update 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.Count <= 0)
|
|||
|
{
|
|||
|
throw new ArgumentNullException("Количество изделий должно быть больше 0", nameof(model.Count));
|
|||
|
}
|
|||
|
if (model.Sum <= 0)
|
|||
|
{
|
|||
|
throw new ArgumentNullException("Цена заказа должна быть больше 0", nameof(model.Sum));
|
|||
|
}
|
|||
|
_logger.LogInformation("Order. OrderName: {Count}.Price: {Sum}. Id: {Id}", model.Count, model.Sum, model.Id);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|