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 StopLogic : IStopLogic { /// /// Логгер /// private readonly ILogger _logger; /// /// Хранилище /// private readonly IStopStorage _stopStorage; /// /// Конструктор /// /// /// public StopLogic(ILogger logger, IStopStorage stopStorage) { _logger = logger; _stopStorage = stopStorage; } /// /// Получение списка /// /// /// public List? ReadList(StopSearchModel? model) { _logger.LogInformation("ReadList. Stop: {Name}.{Id}", model?.Id, model?.Name); var list = model == null ? _stopStorage.GetFullList() : _stopStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList. Returned null list"); return null; } _logger.LogInformation("ReadList. Count: {Count}", list.Count); return list; } /// /// Получение списка из заданного количества элементов /// /// /// public List? ReadList(int count) { _logger.LogInformation("ReadList. Count: {Count}", count); var list = _stopStorage.GetList(count); if (list == null) { _logger.LogWarning("ReadList. Returned null list"); return null; } _logger.LogInformation("ReadList. Count: {Count}", list.Count); return list; } /// /// Получение отдельной записи /// /// /// /// public StopViewModel? ReadElement(StopSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement. Stop: {Name}.{Id}", model?.Id, model?.Name); var element = _stopStorage.GetElement(model!); if (element == null) { _logger.LogWarning("ReadElement. Element not found"); return null; } _logger.LogInformation("ReadElement. Find Stop.Id: {Id}", element.Id); return element; } /// /// Создание записи /// /// /// public bool Create(StopBindingModel model) { CheckModel(model); _logger.LogInformation("Create. Stop.Id: {Id}", model.Id); if (_stopStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } /// /// Изменение записи /// /// /// public bool Update(StopBindingModel model) { CheckModel(model); _logger.LogInformation("Update. Stop.Id: {Id}", model.Id); if (_stopStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } /// /// Удаление записи /// /// /// public bool Delete(StopBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. Stop.Id: {Id}", model.Id); if (_stopStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } /// /// Удаление записи /// /// public bool Delete() { _logger.LogInformation("Delete. Stop"); if (_stopStorage.Delete() == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } /// /// Удаление всех записей /// /// public int Clear() { int count = _stopStorage.Clear(); _logger.LogInformation("Clear. Delete {Count} Stops", count); return count; } /// /// Проверка модели /// /// /// private void CheckModel(StopBindingModel 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 (string.IsNullOrEmpty(model.Street)) { throw new ArgumentNullException("Не указано название улицы", nameof(model.Street)); } if (model.Number <= 0) { throw new ArgumentNullException("Не указан номер дома", nameof(model.Number)); } _logger.LogInformation("CheckModel. Stop.Id: {Id}", model.Id); var element = _stopStorage.GetElement(new StopSearchModel { Name = model.Name }); if (element != null && !element.Id.Equals(model.Id)) { throw new InvalidOperationException("Остановка с таким названием уже существует"); } } } }