using HospitalContracts.BindingModels; using HospitalContracts.BusinessLogicContracts; using HospitalContracts.SearchModels; using HospitalContracts.StorageContracts; using HospitalContracts.ViewModels; using Microsoft.Extensions.Logging; namespace HospitalBusinessLogic { public class TreatmentLogic : ITreatmentLogic { private readonly ILogger _logger; private readonly ITreatmentStorage _treatmentStorage; public TreatmentLogic(ILogger logger, ITreatmentStorage treatmentStorage) { _logger = logger; _treatmentStorage = treatmentStorage; } public TreatmentViewModel? ReadElement(TreatmentSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement. Id:{ Id}", model.Id); var element = _treatmentStorage.GetElement(model); if (element == null) { _logger.LogWarning("ReadElement element not found"); return null; } _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); return element; } public List? ReadList(TreatmentSearchModel? model) { _logger.LogInformation("ReadList. Id:{ Id}", model?.Id); var list = model == null ? _treatmentStorage.GetFullList() : _treatmentStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count:{Count}", list.Count); return list; } public bool Create(TreatmentBindingModel model) { CheckModel(model); if (_treatmentStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Delete(TreatmentBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. Id:{Id}", model.Id); if (_treatmentStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } public bool Update(TreatmentBindingModel model) { CheckModel(model); if (_treatmentStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } private void CheckModel(TreatmentBindingModel 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)); } _logger.LogInformation("Treatment. Name: {Name}.Id: { Id}", model.Name, model.Id); } } }