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.Reflection; using System.Text; using System.Threading.Tasks; namespace RouteGuideBusinessLogics.BusinessLogics { /// /// Бизнес-логика для сущности "Водитель" /// public class DriverLogic : IDriverLogic { /// /// Логгер /// private readonly ILogger _logger; /// /// Хранилище /// private readonly IDriverStorage _driverStorage; /// /// Конструктор /// /// /// public DriverLogic(ILogger logger, IDriverStorage driverStorage) { _logger = logger; _driverStorage = driverStorage; } /// /// Получение списка /// /// /// public List? ReadList(DriverSearchModel? model) { _logger.LogInformation("ReadList. Driver.Id: {Id}", model?.Id); var list = model == null ? _driverStorage.GetFullList() : _driverStorage.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 = _driverStorage.GetList(count); if (list == null) { _logger.LogWarning("ReadList. Returned null list"); return null; } _logger.LogInformation("ReadList. Count: {Count}", list.Count); return list; } /// /// Получение отдельной записи /// /// /// /// public DriverViewModel? ReadElement(DriverSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement. Driver.Id: {Id}", model?.Id); var element = _driverStorage.GetElement(model!); if (element == null) { _logger.LogWarning("ReadElement. Element not found"); return null; } _logger.LogInformation("ReadElement. Find Driver.Id: {Id}", element.Id); return element; } /// /// Создание записи /// /// /// public bool Create(DriverBindingModel model) { CheckModel(model); _logger.LogInformation("Create. Driver.Id: {Id}", model.Id); if (_driverStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } /// /// Изменение записи /// /// /// public bool Update(DriverBindingModel model) { CheckModel(model); _logger.LogInformation("Update. Driver.Id: {Id}", model.Id); if (_driverStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } /// /// Удаление записи /// /// /// public bool Delete(DriverBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. Driver.Id: {Id}", model.Id); if (_driverStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } /// /// Удаление записи /// /// public bool Delete() { _logger.LogInformation("Delete. Driver"); if (_driverStorage.Delete() == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } /// /// Удаление всех записей /// /// public int Clear() { int count = _driverStorage.Clear(); _logger.LogInformation("Clear. Delete {Count} Drivers", count); return count; } /// /// Проверка модели /// /// /// private void CheckModel(DriverBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (string.IsNullOrEmpty(model.FullName)) { throw new ArgumentNullException("Не указано ФИО водителя", nameof(model.FullName)); } if (string.IsNullOrEmpty(model.Phone)) { throw new ArgumentNullException("Не указан номер телефона водителя", nameof(model.Phone)); } _logger.LogInformation("CheckModel. Driver.Id: {Id}", model.Id); var element = _driverStorage.GetElement(new DriverSearchModel { Phone = model.Phone }); if (element != null && !element.Id.Equals(model.Id)) { throw new InvalidOperationException("Водитель с таким номером телефона уже существует"); } } } }