using Microsoft.Extensions.Logging; using TravelAgencyContracts.BindingModels; using TravelAgencyContracts.BusinessLogicsContracts; using TravelAgencyContracts.SearchModels; using TravelAgencyContracts.StoragesContracts; using TravelAgencyContracts.ViewModels; namespace TravelAgencyBusinessLogic.BusinessLogics { public class ExcursionGroupLogic : IExcursionGroupLogic { private readonly ILogger _logger; private readonly IExcursionGroupStorage _excursionGroupStorage; public ExcursionGroupLogic(ILogger logger, IExcursionGroupStorage excursionGroupStorage) { _logger = logger; _excursionGroupStorage = excursionGroupStorage; } public List? ReadList(ExcursionGroupSearchModel? model) { _logger.LogInformation("ReadList. Id: {Id}, ExcursionGroupName: {ExcursionGroupName}, UserId: {UserId}, GuideId: {GuideId}.", model?.Id, model?.ExcursionGroupName, model?.UserId, model?.GuideId); var list = (model == null) ? _excursionGroupStorage.GetFullList() : _excursionGroupStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count: {Count}", list.Count); return list; } public ExcursionGroupViewModel? ReadElement(ExcursionGroupSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement. ExcursionGroup id: {Id}, ExcursionGroup name: {ExcursionGroupName}", model?.Id, model?.ExcursionGroupName); var element = _excursionGroupStorage.GetElement(model!); if (element == null) { _logger.LogWarning("ReadElement element not found"); return null; } _logger.LogInformation("ReadElement find. Id: {Id}", element.Id); return element; } public bool Create(ExcursionGroupBindingModel model) { CheckModel(model); if (_excursionGroupStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Update(ExcursionGroupBindingModel model) { CheckModel(model); if (_excursionGroupStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } public bool Delete(ExcursionGroupBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. Id: {Id}", model.Id); if (_excursionGroupStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } private void CheckModel(ExcursionGroupBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (string.IsNullOrEmpty(model.ExcursionGroupName)) { throw new ArgumentNullException("Нет названия группы", nameof(model.ExcursionGroupName)); } if (model.ParticipantsAmount <= 0) { throw new ArgumentNullException("Количество участников быть больше 0", nameof(model.ParticipantsAmount)); } if (model.UserId <= 0) { throw new ArgumentNullException("Некорректный идентификатор пользователя", nameof(model.UserId)); } _logger.LogInformation("ExcursionGroup. Id: {id}, ExcursionGroupName: {ExcursionGroupName}, ParticipantsAmount: {ParticipantsAmount}, UserId: {UserId}", model.Id, model.ExcursionGroupName, model.ParticipantsAmount, model.UserId); var element = _excursionGroupStorage.GetElement(new ExcursionGroupSearchModel { ExcursionGroupName = model.ExcursionGroupName }); if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Группа с таким названием уже есть"); } } } }