using Microsoft.Extensions.Logging;
using RouteGuideContracts.BindingModels;
using RouteGuideContracts.BusinessLogicsContracts;
using RouteGuideContracts.SearchModels;
using RouteGuideContracts.StoragesContracts;
using RouteGuideContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RouteGuideBusinessLogics.BusinessLogics
{
///
/// Бизнес-логика для сущности "Транспорт"
///
public class TransportLogic : ITransportLogic
{
///
/// Логгер
///
private readonly ILogger _logger;
///
/// Хранилище
///
private readonly ITransportStorage _transportStorage;
///
/// Конструктор
///
///
///
public TransportLogic(ILogger logger, ITransportStorage transportStorage)
{
_logger = logger;
_transportStorage = transportStorage;
}
///
/// Получение списка
///
///
///
public List? ReadList(TransportSearchModel? model)
{
_logger.LogInformation("ReadList. Transport: {Id}.{License}", model?.Id, model?.License);
var list = model == null ? _transportStorage.GetFullList() : _transportStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList. Returned null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
///
/// Получение отдельной записи
///
///
///
///
public TransportViewModel? ReadElement(TransportSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Transport: {Id}.{License}", model?.Id, model?.License);
var element = _transportStorage.GetElement(model!);
if (element == null)
{
_logger.LogWarning("ReadElement. Element not found");
return null;
}
_logger.LogInformation("ReadElement. Find Transport.Id: {Id}", element.Id);
return element;
}
///
/// Создание записи
///
///
///
public bool Create(TransportBindingModel model)
{
CheckModel(model);
_logger.LogInformation("Create. Transport.Id: {Id}", model.Id);
if (_transportStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
///
/// Изменение записи
///
///
///
public bool Update(TransportBindingModel model)
{
CheckModel(model);
_logger.LogInformation("Update. Transport.Id: {Id}", model.Id);
if (_transportStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
///
/// Удаление записи
///
///
///
public bool Delete(TransportBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Transport.Id: {Id}", model.Id);
if (_transportStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
///
/// Проверка модели
///
///
///
private void CheckModel(TransportBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.License))
{
throw new ArgumentNullException("Не указан номерной знак транспортного средства", nameof(model.License));
}
if (model.Capacity <= 0)
{
throw new ArgumentNullException("Вместимость транспортного средства должна быть положительным числом", nameof(model.Capacity));
}
if (model.DriverId < 0)
{
throw new ArgumentNullException("Некорректный идентификатор водителя", nameof(model.DriverId));
}
_logger.LogInformation("CheckModel. Transport.Id: {Id}", model.Id);
var element = _transportStorage.GetElement(new TransportSearchModel
{
License = model.License
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Транспортное средство с таким номерным знаком уже есть");
}
}
}
}