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.ViewModels; using Microsoft.Extensions.Logging; using VeterinaryClinicContracts.StoragesContracts; namespace VeterinaryClinicBusinessLogics.BusinessLogics { public class VaccinationLogic : IVaccinationLogic { private readonly ILogger _logger; private readonly IVaccinationStorage _vaccinationStorage; public VaccinationLogic(ILogger logger, IVaccinationStorage vaccinationStorage) { _logger = logger; _vaccinationStorage = vaccinationStorage; } public bool Create(VaccinationBindingModel model) { CheckModel(model); _logger.LogInformation("Create. Vaccination.Id: {Id}", model.Id); if (_vaccinationStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Delete(VaccinationBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. Vaccination.Id: {Id}", model.Id); if (_vaccinationStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } public VaccinationViewModel? ReadElement(VaccinationSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement. Vaccination.Id: {Id}.", model?.Id); var element = _vaccinationStorage.GetElement(model!); if (element == null) { _logger.LogWarning("ReadElement. Element not found"); return null; } _logger.LogInformation("ReadElement. Find Vaccination.Id: {Id}", element?.Id); return element; } public List? ReadList(VaccinationSearchModel? model) { _logger.LogInformation("ReadList. Vaccination.Id: {Id}. Name: {Name}", model?.Id, model?.Name); var list = model == null ? _vaccinationStorage.GetFullList() : _vaccinationStorage.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(VaccinationBindingModel model) { CheckModel(model); _logger.LogInformation("Update. Vaccination.Id: {Id}", model.Id); if (_vaccinationStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } private void CheckModel(VaccinationBindingModel 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 (model.DateInjection == DateTime.MinValue) { throw new ArgumentNullException("Не указана дата укола", nameof(model.DateInjection)); } if (string.IsNullOrEmpty(model.ValidityPeriod)) { throw new ArgumentNullException("Не указан срок действия", nameof(model.ValidityPeriod)); } if (model.UserId <= 0) { throw new ArgumentNullException("Не указан идентификатор пользователя", nameof(model.UserId)); } if (model.AnimalId <= 0) { throw new ArgumentNullException("Не указан идентификатор животного", nameof(model.AnimalId)); } _logger.LogInformation("CheckModel. Vaccination.Id: {Id}", model.Id); var element = _vaccinationStorage.GetElement(new VaccinationSearchModel { Name = model.Name }); if (element != null && !element.Id.Equals(model.Id)) { throw new InvalidOperationException("Прививка с таким названием уже существует"); } } } }