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 RouteLogic : IRouteLogic { /// /// Логгер /// private readonly ILogger _logger; /// /// Хранилище /// private readonly IRouteStorage _routeStorage; /// /// Конструктор /// /// /// public RouteLogic(ILogger logger, IRouteStorage routeStorage) { _logger = logger; _routeStorage = routeStorage; } /// /// Получение списка /// /// /// public List? 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; } /// /// Получение отдельной записи /// /// /// /// 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; } /// /// Создание записи /// /// /// 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; } /// /// Изменение записи /// /// /// 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; } /// /// Удаление записи /// /// /// 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; } /// /// Проверка модели /// /// /// 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("Маршрут с таким названием уже существует"); } } } }