132 lines
4.2 KiB
C#
132 lines
4.2 KiB
C#
using Contracts.BindingModels;
|
|
using Contracts.BusinessLogicContracts;
|
|
using Contracts.SearchModels;
|
|
using Contracts.StorageContracts;
|
|
using Contracts.ViewModels;
|
|
using DataModels.Enums;
|
|
using Microsoft.Extensions.Logging;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace BusinessLogic.BusinessLogic
|
|
{
|
|
public class SupplyLogic : ISupplyLogic
|
|
{
|
|
private readonly ISupplyStorage _supplyStorage;
|
|
private readonly ILogger _logger;
|
|
public SupplyLogic(ISupplyStorage supplyStorage, ILogger<SupplyLogic> logger)
|
|
{
|
|
_supplyStorage = supplyStorage;
|
|
_logger = logger;
|
|
}
|
|
|
|
public bool Create(SupplyBindingModel model)
|
|
{
|
|
CheckModel(model);
|
|
if (_supplyStorage.Insert(model) == null)
|
|
{
|
|
_logger.LogWarning("Insert operation failed");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public bool Delete(SupplyBindingModel model)
|
|
{
|
|
CheckModel(model, false);
|
|
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
|
if (_supplyStorage.Delete(model) == null)
|
|
{
|
|
_logger.LogWarning("Delete operation failed");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public SupplyViewModel? ReadElement(SupplySearchModel model)
|
|
{
|
|
if (model == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(model));
|
|
}
|
|
_logger.LogInformation("ReadElement. Name:{Name}.Id:{ Id}", model.Name, model.Id);
|
|
var element = _supplyStorage.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<SupplyViewModel>? ReadList(SupplySearchModel? model)
|
|
{
|
|
_logger.LogInformation("ReadList. Name:{Name}.Id:{ Id}", model?.Name, model?.Id);
|
|
var list = model == null ? _supplyStorage.GetFullList() : _supplyStorage.GetFilteredList(model);
|
|
if (list == null)
|
|
{
|
|
_logger.LogWarning("ReadList return null list");
|
|
return null;
|
|
}
|
|
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
|
return list;
|
|
}
|
|
|
|
public bool Update(SupplyBindingModel model)
|
|
{
|
|
CheckModel(model);
|
|
if (_supplyStorage.Update(model) == null)
|
|
{
|
|
_logger.LogWarning("Update operation failed");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private void CheckModel(SupplyBindingModel model, bool withParams = true)
|
|
{
|
|
if (model == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(model));
|
|
}
|
|
if (!withParams)
|
|
{
|
|
return;
|
|
}
|
|
if (string.IsNullOrEmpty(model.Name))
|
|
{
|
|
throw new ArgumentNullException("Нет названия поставки", nameof(model.Name));
|
|
}
|
|
}
|
|
|
|
public bool StatusUpdate(SupplyBindingModel model, SupplyStatus newStatus)
|
|
{
|
|
if (model.Status + 1 != newStatus)
|
|
{
|
|
_logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Supply status incorrect.");
|
|
return false;
|
|
}
|
|
model.Status = newStatus;
|
|
if (newStatus == SupplyStatus.Arriving)
|
|
{
|
|
model.DateArriving = DateTime.UtcNow;
|
|
}
|
|
if (newStatus == SupplyStatus.Completed)
|
|
{
|
|
model.DateComplete = DateTime.UtcNow;
|
|
}
|
|
if (_supplyStorage.Update(model) == null)
|
|
{
|
|
model.Status--;
|
|
_logger.LogWarning("Update operation failed");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
}
|