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

110 lines
3.9 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 DiagnoseLogic : IDiagnoseLogic
{
private ILogger _logger;
private IDiagnoseStorage _diagnoseStorage;
public DiagnoseLogic(ILogger<DiagnoseLogic> logger, IDiagnoseStorage diagnoseStorage)
{
_logger = logger;
_diagnoseStorage = diagnoseStorage;
}
public bool Create(DiagnoseBindingModel model)
{
CheckModel(model);
if (_diagnoseStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(DiagnoseBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Name:{Name}, Id:{Id}", model.Name, model.Id);
if (_diagnoseStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public DiagnoseViewModel? ReadElement(DiagnoseSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Name:{Name}, Id:{Id}, UserId:{UserId}", model.Name, model.Id, model.UserId);
var element = _diagnoseStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Name:{Name}, Id:{Id}, UserId:{UserId}", element.Name, element.Id, element.UserId);
return element;
}
public List<DiagnoseViewModel> ReadList(DiagnoseSearchModel? model = null)
{
_logger.LogInformation("ReadList. Name:{Name} Id:{Id}, UserId:{UserId}", model?.Name, model?.Id, model?.UserId);
var list = model == null ? _diagnoseStorage.GetFullList() : _diagnoseStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return new List<DiagnoseViewModel>();
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public bool Update(DiagnoseBindingModel model)
{
CheckModel(model);
if (_diagnoseStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(DiagnoseBindingModel 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("Diagnose. Name:{Name}. Id: {Id}", model.Name, model.Id);
var element = _diagnoseStorage.GetElement(new DiagnoseSearchModel
{
Name = model.Name
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Болезнь с таким названием уже есть");
}
}
}
}