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 ProceduresLogic : IProceduresLogic { private readonly ILogger _logger; private readonly IProceduresStorage _proceduresStorage; public ProceduresLogic(ILogger logger, IProceduresStorage proceduresStorage) { _logger = logger; _proceduresStorage = proceduresStorage; } public bool Create(ProceduresBindingModel model) { CheckModel(model); if (_proceduresStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Delete(ProceduresBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. Id:{Id}", model.Id); if (_proceduresStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } public ProceduresViewModel? ReadElement(ProceduresSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement. ProceduresName:{ProceduresName}. Id:{ Id}", model.ProceduresName, model.Id); var element = _proceduresStorage.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(ProceduresSearchModel? model) { _logger.LogInformation("ReadList. ProceduresName:{ProceduresName}. Id:{ Id}", model?.ProceduresName, model?.Id); var list = model == null ? _proceduresStorage.GetFullList() : _proceduresStorage.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(ProceduresBindingModel model) { CheckModel(model); if (_proceduresStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } private void CheckModel(ProceduresBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (string.IsNullOrEmpty(model.ProceduresName)) { throw new ArgumentNullException("Нет названия процедуры", nameof(model.ProceduresName)); } if (string.IsNullOrEmpty(model.Type)) { throw new ArgumentNullException("Нет типа процедуры", nameof(model.Type)); } _logger.LogInformation("Procedures. ProceduresName:{ProceduresName}. Type:{ Type}. Id: { Id} ", model.ProceduresName, model.Type, model.Id); var element = _proceduresStorage.GetElement(new ProceduresSearchModel { ProceduresName = model.ProceduresName }); if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Процедура с таким названием уже есть"); } } } }