PIbd-21_Afanasev_S.S_MotorP.../MotorPlant/MotorPlantBusinessLogic/EngineLogic.cs

113 lines
3.9 KiB
C#
Raw Permalink Normal View History

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