using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using VeterinaryClinicContracts.BindingModels; using VeterinaryClinicContracts.BusinessLogicsContracts; using VeterinaryClinicContracts.SearchModels; using VeterinaryClinicContracts.StoragesContracts; using VeterinaryClinicContracts.ViewModels; using Microsoft.Extensions.Logging; namespace VeterinaryClinicBusinessLogics.BusinessLogics { public class AnimalLogic : IAnimalLogic { private readonly ILogger _logger; private readonly IAnimalStorage _animalStorage; public AnimalLogic(ILogger logger, IAnimalStorage animalStorage) { _logger = logger; _animalStorage = animalStorage; } public bool Create(AnimalBindingModel model) { CheckModel(model); _logger.LogInformation("Create. Animal.Id: {Id}", model.Id); if (_animalStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Delete(AnimalBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. Animal.Id: {Id}", model.Id); if (_animalStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } public AnimalViewModel? ReadElement(AnimalSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement. Animal.Id: {Id}", model?.Id); var element = _animalStorage.GetElement(model!); if (element == null) { _logger.LogWarning("ReadElement. Element not found"); return null; } _logger.LogInformation("ReadElement. Find Animal.Id: {Id}", element?.Id); return element; } public List? ReadList(AnimalSearchModel? model) { _logger.LogInformation("ReadList. Animal.Id: {Id}. UserId: {UserId}", model?.Id, model?.UserId); var list = model == null ? _animalStorage.GetFullList() : _animalStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList. Returned null list"); return null; } _logger.LogInformation("ReadList. Count: {Count}", list.Count); return list; } public bool Update(AnimalBindingModel model) { CheckModel(model); _logger.LogInformation("Update. Animal.Id: {Id}", model.Id); if (_animalStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } private void CheckModel(AnimalBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (string.IsNullOrEmpty(model.Type)) { throw new ArgumentNullException("Не указан вид животного", nameof(model.Type)); } if (string.IsNullOrEmpty(model.Breed)) { throw new ArgumentNullException("Не указан порода животного", nameof(model.Breed)); } if (model.Age < 0) { throw new ArgumentNullException("Не указан возраст животного", nameof(model.Age)); } if (model.UserId <= 0) { throw new ArgumentNullException("Не указан идентификатор пользователя", nameof(model.UserId)); } _logger.LogInformation("CheckModel. Animal.Id: {Id}", model.Id); } } }