109 lines
3.7 KiB
C#
109 lines
3.7 KiB
C#
using FurnitureAssemblyContracts.BindingModels;
|
||
using FurnitureAssemblyContracts.BusinessLogicsContarcts;
|
||
using FurnitureAssemblyContracts.SearchModels;
|
||
using FurnitureAssemblyContracts.StoragesContracts;
|
||
using FurnitureAssemblyContracts.ViewModels;
|
||
using FurnitureAssemblyDataModels.Enums;
|
||
using Microsoft.Extensions.Logging;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace FurnitureAssemblyBusinessLogic
|
||
{
|
||
public class OrderLogic : IOrderLogic
|
||
{
|
||
private readonly ILogger _logger;
|
||
private readonly IOrderStorage _orderStorage;
|
||
|
||
public OrderLogic(ILogger 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;
|
||
}
|
||
model.Status = OrderStatus.Принят;
|
||
return true;
|
||
}
|
||
|
||
public bool DeliveryOrder(OrderBindingModel model)
|
||
{
|
||
if (model.Status.Equals(OrderStatus.Готов))
|
||
{
|
||
model.Status = OrderStatus.Выдан;
|
||
model.DateImplement = DateTime.Now;
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
public bool FinishOrder(OrderBindingModel model)
|
||
{
|
||
if (model.Status.Equals(OrderStatus.Выполняется))
|
||
{
|
||
model.Status = OrderStatus.Готов;
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
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)
|
||
{
|
||
if (model.Status.Equals(OrderStatus.Принят))
|
||
{
|
||
model.Status = OrderStatus.Выполняется;
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
private void CheckModel(OrderBindingModel model, bool withParams = true)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
if (!withParams)
|
||
{
|
||
return;
|
||
}
|
||
if (model.FurnitureId <= 0)
|
||
{
|
||
throw new ArgumentNullException("Идентификатор изделия должен быть больше 0", nameof(model.FurnitureId));
|
||
}
|
||
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. FurnitureId:{FurnitureId}. Count:{ Count}. Sum:{ Sum}. OrderStatus: {OrderStatus} DateCreate: {DateCreate}. Id: { Id}", model.FurnitureId, model.Count, model.Sum, model.Status, model.DateCreate, model.Id);
|
||
}
|
||
}
|
||
}
|