PIbd-21_MasenkinMS_SUBD_Rou.../RouteGuide/RouteGuideBusinessLogics/BusinessLogics/RouteLogic.cs

178 lines
5.6 KiB
C#
Raw Normal View History

2024-04-02 18:46:47 +04:00
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 RouteLogic : IRouteLogic
{
/// <summary>
/// Логгер
/// </summary>
private readonly ILogger _logger;
/// <summary>
/// Хранилище
/// </summary>
private readonly IRouteStorage _routeStorage;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="logger"></param>
/// <param name="routeStorage"></param>
public RouteLogic(ILogger<RouteLogic> logger, IRouteStorage routeStorage)
{
_logger = logger;
_routeStorage = routeStorage;
}
/// <summary>
/// Получение списка
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public List<RouteViewModel>? ReadList(RouteSearchModel? model)
{
_logger.LogInformation("ReadList. Route: {Name}.{Id}", model?.Id, model?.Name);
var list = model == null ? _routeStorage.GetFullList() : _routeStorage.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="model"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public RouteViewModel? ReadElement(RouteSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Route: {Name}.{Id}", model?.Id, model?.Name);
var element = _routeStorage.GetElement(model!);
if (element == null)
{
_logger.LogWarning("ReadElement. Element not found");
return null;
}
_logger.LogInformation("ReadElement. Find Route.Id: {Id}", element.Id);
return element;
}
/// <summary>
/// Создание записи
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public bool Create(RouteBindingModel model)
{
CheckModel(model);
_logger.LogInformation("Create. Route.Id: {Id}", model.Id);
if (_routeStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
/// <summary>
/// Изменение записи
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public bool Update(RouteBindingModel model)
{
CheckModel(model);
_logger.LogInformation("Update. Route.Id: {Id}", model.Id);
if (_routeStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
/// <summary>
/// Удаление записи
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public bool Delete(RouteBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Route.Id: {Id}", model.Id);
if (_routeStorage.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(RouteBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Name))
{
throw new ArgumentNullException("Не указано название маршрута", nameof(model.Name));
}
if (model.TransportId < 0)
{
throw new ArgumentNullException("Некорректный идентификатор транспортного средства", nameof(model.TransportId));
}
_logger.LogInformation("CheckModel. Route.Id: {Id}", model.Id);
var element = _routeStorage.GetElement(new RouteSearchModel
{
Name = model.Name
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Маршрут с таким названием уже существует");
}
}
}
}