PIbd-21_MasenkinMS_SUBD_Rou.../RouteGuide/RouteGuideBusinessLogics/BusinessLogics/TransportLogic.cs
2024-04-23 19:38:25 +04:00

229 lines
7.5 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 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
{
/// <summary>
/// Бизнес-логика для сущности "Транспорт"
/// </summary>
public class TransportLogic : ITransportLogic
{
/// <summary>
/// Логгер
/// </summary>
private readonly ILogger _logger;
/// <summary>
/// Хранилище
/// </summary>
private readonly ITransportStorage _transportStorage;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="logger"></param>
/// <param name="transportStorage"></param>
public TransportLogic(ILogger<TransportLogic> logger, ITransportStorage transportStorage)
{
_logger = logger;
_transportStorage = transportStorage;
}
/// <summary>
/// Получение списка
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public List<TransportViewModel>? 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;
}
/// <summary>
/// Получение списка из заданного количества элементов
/// </summary>
/// <param name="count"></param>
/// <returns></returns>
public List<TransportViewModel>? ReadList(int count)
{
_logger.LogInformation("ReadList. Count: {Count}", count);
var list = _transportStorage.GetList(count);
if (list == null)
{
_logger.LogWarning("ReadList. Returned 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 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;
}
/// <summary>
/// Создание записи
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
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;
}
/// <summary>
/// Изменение записи
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
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;
}
/// <summary>
/// Удаление записи
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
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;
}
/// <summary>
/// Удаление записи
/// </summary>
/// <returns></returns>
public bool Delete()
{
_logger.LogInformation("Delete. Transport");
if (_transportStorage.Delete() == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
/// <summary>
/// Удаление всех записей
/// </summary>
/// <returns></returns>
public int Clear()
{
int count = _transportStorage.Clear();
_logger.LogInformation("Clear. Delete {Count} Transports", count);
return count;
}
/// <summary>
/// Проверка модели
/// </summary>
/// <param name="model"></param>
/// <param name="withParams"></param>
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 == null || (model.DriverId is int && (int)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.Equals(model.Id))
{
throw new InvalidOperationException("Транспортное средство с таким номерным знаком уже есть");
}
}
}
}