PIbd-21_CourseWork_Polyclin.../Polyclinic/PolyclinicBusinessLogic/BusinessLogics/SymptomLogic.cs

110 lines
3.8 KiB
C#
Raw Permalink Normal View History

using Microsoft.Extensions.Logging;
using PolyclinicContracts.BindingModels;
using PolyclinicContracts.BusinessLogicsContracts;
using PolyclinicContracts.SearchModels;
using PolyclinicContracts.StoragesContracts;
using PolyclinicContracts.ViewModels;
namespace PolyclinicBusinessLogic.BusinessLogics
{
public class SymptomLogic : ISymptomLogic
{
private ILogger _logger;
private ISymptomStorage _symptomStorage;
public SymptomLogic(ILogger<SymptomLogic> logger, ISymptomStorage symptomStorage)
{
_logger = logger;
_symptomStorage = symptomStorage;
}
public bool Create(SymptomBindingModel model)
{
CheckModel(model);
if (_symptomStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(SymptomBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Name:{Name}, Id:{Id}", model.Name, model.Id);
if (_symptomStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public SymptomViewModel? ReadElement(SymptomSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Name:{Name}, Id:{Id}", model.Name, model.Id);
var element = _symptomStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Name:{Name}, Id:{Id}", element.Name, element.Id);
return element;
}
public List<SymptomViewModel> ReadList(SymptomSearchModel? model = null)
{
_logger.LogInformation("ReadList. Name:{Name} Id:{Id}", model?.Name, model?.Id);
var list = model == null ? _symptomStorage.GetFullList() : _symptomStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return new List<SymptomViewModel>();
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public bool Update(SymptomBindingModel model)
{
CheckModel(model, false);
if (_symptomStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(SymptomBindingModel 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("Symptom. Name:{Name}. Id: {Id}", model.Name, model.Id);
var element = _symptomStorage.GetElement(new SymptomSearchModel
{
Name = model.Name
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Симптом с таким названием уже есть");
}
}
}
}