PIbd-23_Yakobchuk_S.V._Moto.../MotorPlant/MotorPlantBusinessLogic/EngineLogic.cs

113 lines
3.9 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 MotorPlantContracts.BindingModels;
using MotorPlantContracts.BusinessLogicsContracts;
using MotorPlantContracts.SearchModels;
using MotorPlantContracts.StoragesContracts;
using MotorPlantContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace MotorPlantBusinessLogic.BusinessLogics
{
public class EngineLogic : IEngineLogic
{
private readonly ILogger _logger;
private readonly IEngineStorage _EngineStorage;
public EngineLogic(ILogger<EngineLogic> logger, IEngineStorage EngineStorage)
{
_logger = logger;
_EngineStorage = EngineStorage;
}
public List<EngineViewModel>? ReadList(EngineSearchModel? model)
{
_logger.LogInformation("ReadList. EngineName:{EngineName}.Id:{Id}", model?.EngineName, model?.Id);
var list = model == null ? _EngineStorage.GetFullList() : _EngineStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public EngineViewModel? ReadElement(EngineSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. EngineName:{EngineName}.Id:{Id}", model.EngineName, model.Id);
var element = _EngineStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public bool Create(EngineBindingModel model)
{
CheckModel(model);
if (_EngineStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(EngineBindingModel model)
{
CheckModel(model);
if (_EngineStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(EngineBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_EngineStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(EngineBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.EngineName))
{
throw new ArgumentNullException("Нет названия изделия", nameof(model.EngineName));
}
if (model.Price <= 0)
{
throw new ArgumentNullException("Цена изделия должна быть больше 0", nameof(model.Price));
}
_logger.LogInformation("Engine. EngineName:{EngineName}.Price:{Cost}. Id: {Id}", model.EngineName, model.Price, model.Id);
var element = _EngineStorage.GetElement(new EngineSearchModel
{
EngineName = model.EngineName
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Изделие с таким названием уже есть");
}
}
}
}