108 lines
3.1 KiB
C#
108 lines
3.1 KiB
C#
using Contracts.BindingModels;
|
||
using Contracts.BusinessLogicsContracts;
|
||
using Contracts.SearchModels;
|
||
using Contracts.StoragesContracts;
|
||
using Contracts.ViewModels;
|
||
using Microsoft.Extensions.Logging;
|
||
|
||
namespace BusinessLogic.BusinessLogic
|
||
{
|
||
public class MachineLogic : IMachineLogic
|
||
{
|
||
private readonly ILogger _logger;
|
||
private readonly IMachineStorage _machineStorage;
|
||
public MachineLogic(ILogger<MachineLogic> logger, IMachineStorage machineStorage)
|
||
{
|
||
_logger = logger;
|
||
_machineStorage = machineStorage;
|
||
}
|
||
public List<MachineViewModel>? ReadList(MachineSearchModel? model)
|
||
{
|
||
_logger.LogInformation("ReadList. MachineTitle:{Title}. Id:{Id}. UserId:{UserId}", model?.Title, model?.Id, model?.UserId);
|
||
var list = model == null ? _machineStorage.GetFullList() : _machineStorage.GetFilteredList(model);
|
||
if (list == null)
|
||
{
|
||
_logger.LogWarning("ReadList return null list");
|
||
return null;
|
||
}
|
||
_logger.LogWarning("ReadList. Count:{Count}", list.Count);
|
||
return list;
|
||
}
|
||
public MachineViewModel? ReadElement(MachineSearchModel? model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
_logger.LogInformation("ReadElement. MachineTitle:{Title}. Id:{Id}", model.Title, model.Id);
|
||
var elem = _machineStorage.GetElement(model);
|
||
if (elem == null)
|
||
{
|
||
_logger.LogWarning("ReadElement element not found");
|
||
return null;
|
||
}
|
||
_logger.LogInformation("ReadElement find. Id:{Id}", elem.Id);
|
||
return elem;
|
||
}
|
||
private void CheckModel(MachineBindingModel? model, bool withParams = true)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
if (!withParams)
|
||
{
|
||
return;
|
||
}
|
||
if (string.IsNullOrEmpty(model.Title))
|
||
{
|
||
throw new ArgumentNullException("Нет названия изделия", nameof(model.Title));
|
||
}
|
||
if (string.IsNullOrEmpty(model.Country))
|
||
{
|
||
throw new ArgumentNullException("Нет страны изготовителя", nameof(model.Country));
|
||
}
|
||
_logger.LogInformation("Machine. MachineTitle:{Title}. Country:{Country}. Id:{Id}", model.Title, model.Country, model.Id);
|
||
var elem = _machineStorage.GetElement(new MachineSearchModel
|
||
{
|
||
Title = model.Title
|
||
});
|
||
if (elem != null && elem.Id != model.Id)
|
||
{
|
||
throw new InvalidOperationException("Станок с таким названием уже существует");
|
||
}
|
||
}
|
||
public bool Create(MachineBindingModel? model)
|
||
{
|
||
CheckModel(model);
|
||
if (_machineStorage.Insert(model!) == null)
|
||
{
|
||
_logger.LogWarning("Insert error");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
public bool Update(MachineBindingModel? model)
|
||
{
|
||
CheckModel(model);
|
||
if (_machineStorage.Update(model!) == null)
|
||
{
|
||
_logger.LogWarning("Update error");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
public bool Delete(MachineBindingModel? model)
|
||
{
|
||
CheckModel(model, false);
|
||
_logger.LogInformation("Delete Id:{Id}", model!.Id);
|
||
if (_machineStorage.Delete(model!) == null)
|
||
{
|
||
_logger.LogWarning("Delete error");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
}
|
||
}
|