using Microsoft.Extensions.Logging; using TravelAgencyContracts.BindingModels; using TravelAgencyContracts.BusinessLogicsContracts; using TravelAgencyContracts.SearchModels; using TravelAgencyContracts.StoragesContracts; using TravelAgencyContracts.ViewModels; namespace TravelAgencyBusinessLogic.BusinessLogics { public class PlaceLogic : IPlaceLogic { private readonly ILogger _logger; private readonly IPlaceStorage _placeStorage; public PlaceLogic(ILogger logger, IPlaceStorage placeStorage) { _logger = logger; _placeStorage = placeStorage; } public List? ReadList(PlaceSearchModel? model) { _logger.LogInformation("ReadList. Id: {Id}, PlaceName: {PlaceName}, ExcursionId: {ExcursionId}.", model?.Id, model?.PlaceName, model?.ExcursionId); var list = (model == null) ? _placeStorage.GetFullList() : _placeStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count: {Count}", list.Count); return list; } public PlaceViewModel? ReadElement(PlaceSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement. Place id: {Id}, Place name: {PlaceName}, Place address: {PlaceAddress}", model?.Id, model?.PlaceName, model?.PlaceAddress); var element = _placeStorage.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(PlaceBindingModel model) { CheckModel(model); if (_placeStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Update(PlaceBindingModel model) { CheckModel(model); if (_placeStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } public bool Delete(PlaceBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. Id: {Id}", model.Id); if (_placeStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } private void CheckModel(PlaceBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (string.IsNullOrEmpty(model.PlaceName)) { throw new ArgumentNullException("Нет названия места", nameof(model.PlaceName)); } if (string.IsNullOrEmpty(model.PlaceAddress)) { throw new ArgumentNullException("Нет адреса места", nameof(model.PlaceAddress)); } if (model.ExcursionId <= 0) { throw new ArgumentNullException("Некорректный идентификатор экскурсии", nameof(model.ExcursionId)); } _logger.LogInformation("Place. Id: {id}, PlaceName: {PlaceName}, PlaceAddress: {PlaceAddress}, ExcursionId: {ExcursionId}", model.Id, model.PlaceName, model.PlaceAddress, model.ExcursionId); var element = _placeStorage.GetElement(new PlaceSearchModel { PlaceName = model.PlaceName, PlaceAddress = model.PlaceAddress }); if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Место с таким названием или адресом уже есть"); } } }