168 lines
5.6 KiB
C#
168 lines
5.6 KiB
C#
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
|
||
{
|
||
/// <summary>
|
||
/// Реализация интерфейса бизнес-логики для изделия
|
||
/// </summary>
|
||
public class PlaneLogic : IPlaneLogic
|
||
{
|
||
/// <summary>
|
||
/// Логгер
|
||
/// </summary>
|
||
private readonly ILogger _logger;
|
||
|
||
/// <summary>
|
||
/// Взаимодействие с хранилищем изделий
|
||
/// </summary>
|
||
private readonly IPlaneStorage _planeStorage;
|
||
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
/// <param name="logger"></param>
|
||
/// <param name="planeStorage"></param>
|
||
public PlaneLogic(ILogger<PlaneLogic> logger, IPlaneStorage planeStorage)
|
||
{
|
||
_logger = logger;
|
||
_planeStorage = planeStorage;
|
||
}
|
||
/// <summary>
|
||
/// Получение списка
|
||
/// </summary>
|
||
/// <param name="model"></param>
|
||
/// <returns></returns>
|
||
public List<PlaneViewModel>? 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;
|
||
}
|
||
/// <summary>
|
||
/// Получение отдельной записи
|
||
/// </summary>
|
||
/// <param name="model"></param>
|
||
/// <returns></returns>
|
||
/// <exception cref="ArgumentNullException"></exception>
|
||
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;
|
||
}
|
||
/// <summary>
|
||
/// Создание записи
|
||
/// </summary>
|
||
/// <param name="model"></param>
|
||
/// <returns></returns>
|
||
public bool Create(PlaneBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
|
||
if (_planeStorage.Insert(model) == null)
|
||
{
|
||
_logger.LogWarning("Insert operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
/// <summary>
|
||
/// Изменение записи
|
||
/// </summary>
|
||
/// <param name="model"></param>
|
||
/// <returns></returns>
|
||
public bool Update(PlaneBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
|
||
if (_planeStorage.Update(model) == null)
|
||
{
|
||
_logger.LogWarning("Update operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
/// <summary>
|
||
/// Удаление записи
|
||
/// </summary>
|
||
/// <param name="model"></param>
|
||
/// <returns></returns>
|
||
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;
|
||
}
|
||
/// <summary>
|
||
/// Проверка модели изделия
|
||
/// </summary>
|
||
/// <param name="model"></param>
|
||
/// <param name="withParams"></param>
|
||
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("Изделие с таким названием уже есть");
|
||
}
|
||
}
|
||
}
|
||
}
|