using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using VeterinaryContracts.BusinessLogicContracts; using VeterinaryContracts.BindingModels; using VeterinaryContracts.SearchModels; using VeterinaryContracts.StorageContracts; using VeterinaryContracts.ViewModels; namespace VeterinaryBusinessLogic.BusinessLogic { public class MedicationLogic : IMedicationLogic { private readonly ILogger _logger; private readonly IMedicationStorage _medicationStorage; public MedicationLogic(ILogger logger, IMedicationStorage medicationStorage) { _logger = logger; _medicationStorage = medicationStorage; } public List? ReadList(MedicationSearchModel? model) { _logger.LogInformation("ReadList. MedicationName:{MedicationName}.Id:{ Id}", model?.MedicationName, model?.Id); var list = model == null ? _medicationStorage.GetFullList() : _medicationStorage.GetFullList()/*GetFilteredList(model)*/; if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count:{Count}", list.Count); return list; } public MedicationViewModel? ReadElement(MedicationSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement. MedicationName:{MedicationName}.Id:{ Id}", model.MedicationName, model.Id); var element = _medicationStorage.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(MedicationBindingModel model) { CheckModel(model); if (_medicationStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Update(MedicationBindingModel model) { CheckModel(model); if (_medicationStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } public bool Delete(MedicationBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. Id:{Id}", model.Id); if (_medicationStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } private void CheckModel(MedicationBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (string.IsNullOrEmpty(model.MedicationName)) { throw new ArgumentNullException("Нет названия медикамента", nameof(model.MedicationName)); } if (model.Price <= 0) { throw new ArgumentNullException("Цена медикамента должна быть больше 0", nameof(model.Price)); } _logger.LogInformation("Medication. MedicationName:{MedicationName}.Price:{ Price}. Id: { Id}", model.MedicationName, model.Price, model.Id); var element = _medicationStorage.GetElement(new MedicationSearchModel { MedicationName = model.MedicationName }); if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Медикамент с таким названием уже есть"); } } } }