111 lines
3.0 KiB
C#
111 lines
3.0 KiB
C#
using ComputerShopContracts.BindingModels;
|
|
using ComputerShopContracts.BusinessLogicContracts;
|
|
using ComputerShopContracts.SearchModels;
|
|
using ComputerShopContracts.StorageContracts;
|
|
using ComputerShopContracts.ViewModels;
|
|
using Microsoft.Extensions.Logging;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Xml.Linq;
|
|
|
|
namespace ComputerShopBusinessLogic.BusinessLogics
|
|
{
|
|
public class ShipmentLogic : IShipmentLogic
|
|
{
|
|
private readonly ILogger _logger;
|
|
private readonly IShipmentStorage _shipmentStorage;
|
|
|
|
public ShipmentLogic(ILogger<ShipmentLogic> logger, IShipmentStorage shipmentStorage)
|
|
{
|
|
_logger = logger;
|
|
_shipmentStorage = shipmentStorage;
|
|
}
|
|
|
|
public List<ShipmentViewModel> ReadList(ShipmentSearchModel? model)
|
|
{
|
|
//model.UserId = -1 для swagger, чтобы можно было считать все партии товаров всех пользователей
|
|
var list = (model == null || model.UserId == -1) ? _shipmentStorage.GetFullList() : _shipmentStorage.GetFilteredList(model);
|
|
if (list == null)
|
|
{
|
|
_logger.LogWarning("ReadList return null list");
|
|
return null;
|
|
}
|
|
_logger.LogInformation("ReadList Count:{Count}", list.Count);
|
|
return list;
|
|
}
|
|
|
|
public ShipmentViewModel? ReadElement(ShipmentSearchModel model)
|
|
{
|
|
if (model == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(model));
|
|
}
|
|
var shipment = _shipmentStorage.GetElement(model);
|
|
if (shipment == null)
|
|
{
|
|
_logger.LogWarning("ReadElement shipment not found");
|
|
return null;
|
|
}
|
|
_logger.LogInformation("ReadElement found. Id:{Id}", shipment.Id);
|
|
return shipment;
|
|
}
|
|
|
|
public bool Create(ShipmentBindingModel model)
|
|
{
|
|
CheckModel(model);
|
|
if (_shipmentStorage.Insert(model) == null)
|
|
{
|
|
_logger.LogWarning("Insert operation failed");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public bool Update(ShipmentBindingModel model)
|
|
{
|
|
CheckModel(model);
|
|
if (_shipmentStorage.Update(model) == null)
|
|
{
|
|
_logger.LogWarning("Update operation failed");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public bool Delete(ShipmentBindingModel model)
|
|
{
|
|
CheckModel(model, false);
|
|
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
|
if (_shipmentStorage.Delete(model) == null)
|
|
{
|
|
_logger.LogWarning("Delete operation failed");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private void CheckModel(ShipmentBindingModel model, bool withParams = true)
|
|
{
|
|
if (model == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(model));
|
|
}
|
|
if (!withParams)
|
|
{
|
|
return;
|
|
}
|
|
if (string.IsNullOrEmpty(model.ProviderName))
|
|
{
|
|
throw new ArgumentNullException("Нет имени поставщика", nameof(model.ProviderName));
|
|
}
|
|
if (model.DateShipment > DateTime.Now)
|
|
{
|
|
throw new ArgumentException($"Дата поступления {model.DateShipment} партии товаров не может быть в будущем");
|
|
}
|
|
}
|
|
}
|
|
}
|