2024-04-27 22:13:33 +04:00

108 lines
3.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
}
}
}