using AircraftPlantContracts.BindingModels;
using AircraftPlantContracts.BusinessLogicsContracts;
using AircraftPlantContracts.SearchModels;
using AircraftPlantContracts.StoragesContracts;
using AircraftPlantContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AircraftPlantBusinessLogic.BusinessLogics
{
///
/// Реализация интерфейса бизнес-логики для изделия
///
public class PlaneLogic : IPlaneLogic
{
///
/// Логгер
///
private readonly ILogger _logger;
///
/// Взаимодействие с хранилищем изделий
///
private readonly IPlaneStorage _planeStorage;
///
/// Конструктор
///
///
///
public PlaneLogic(ILogger logger, IPlaneStorage
planeStorage)
{
_logger = logger;
_planeStorage = planeStorage;
}
///
/// Получение списка
///
///
///
public List? ReadList(PlaneSearchModel? model)
{
_logger.LogInformation("ReadList. PlaneName:{PlaneName}.Id:{ Id}", model?.PlaneName, model?.Id);
var list = model == null ? _planeStorage.GetFullList() : _planeStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
///
/// Получение отдельной записи
///
///
///
///
public PlaneViewModel? ReadElement(PlaneSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. PlaneName:{PlaneName}.Id:{ Id}", model.PlaneName, model.Id);
var element = _planeStorage.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(PlaneBindingModel model)
{
CheckModel(model);
if (_planeStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
///
/// Изменение записи
///
///
///
public bool Update(PlaneBindingModel model)
{
CheckModel(model);
if (_planeStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
///
/// Удаление записи
///
///
///
public bool Delete(PlaneBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_planeStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
///
/// Проверка модели изделия
///
///
///
private void CheckModel(PlaneBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.PlaneName))
{
throw new ArgumentNullException("Нет названия изделия", nameof(model.PlaneName));
}
if (model.Price <= 0)
{
throw new ArgumentNullException("Цена изделия должна быть больше 0", nameof(model.Price));
}
_logger.LogInformation("Plane. PlaneName:{PlaneName}.Price:{ Price}. Id: { Id}", model.PlaneName, model.Price, model.Id);
var element = _planeStorage.GetElement(new PlaneSearchModel
{
PlaneName = model.PlaneName
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Изделие с таким названием уже есть");
}
}
}
}