using HospitalContracts.BindingModels; using HospitalContracts.BusinessLogicsContracts; using HospitalContracts.SearchModels; using HospitalContracts.StoragesContracts; using HospitalContracts.ViewModels; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HospitalBusinessLogic.BusinessLogics { public class SymptomsLogic : ISymptomsLogic { private readonly ILogger _logger; private readonly ISymptomsStorage _symptomsStorage; public SymptomsLogic(ILogger logger, ISymptomsStorage symptomsStorage) { _logger = logger; _symptomsStorage = symptomsStorage; } public bool Create(SymptomsBindingModel model) { CheckModel(model); if (_symptomsStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Delete(SymptomsBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. Id:{Id}", model.Id); if (_symptomsStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } public SymptomsViewModel? ReadElement(SymptomsSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement. SymptomsName:{SymptomsName}. Id:{ Id}", model.SymptomName, model.Id); var element = _symptomsStorage.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(SymptomsSearchModel? model) { _logger.LogInformation("ReadList. SymptomsName:{SymptomsName}. Id:{ Id}", model?.SymptomName, model?.Id); var list = model == null ? _symptomsStorage.GetFullList() : _symptomsStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count:{Count}", list.Count); return list; } public bool Update(SymptomsBindingModel model) { CheckModel(model); if (_symptomsStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } private void CheckModel(SymptomsBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (string.IsNullOrEmpty(model.SymptomName)) { throw new ArgumentNullException("Нет названия симптоматики", nameof(model.SymptomName)); } _logger.LogInformation("Symptoms. SymptomName:{SymptomName}. Id: { Id} ", model.SymptomName, model.Id); var element = _symptomsStorage.GetElement(new SymptomsSearchModel { SymptomName = model.SymptomName }); if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Симптоматика с таким названием уже есть"); } } } }