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 HospitalBusinessLogics.BusinessLogics
{
///
/// Бизнес-логика для сущности "Процедура"
///
public class ProcedureLogic : IProcedureLogic
{
///
/// Логгер
///
private readonly ILogger _logger;
///
/// Хранилище
///
private readonly IProcedureStorage _procedureStorage;
///
/// Конструктор
///
///
///
public ProcedureLogic(ILogger logger, IProcedureStorage procedureStorage)
{
_logger = logger;
_procedureStorage = procedureStorage;
}
///
/// Получить список
///
///
///
public List? ReadList(ProcedureSearchModel? model)
{
_logger.LogInformation("ReadList. Procedure.Id: {Id}. Name: {Name}", model?.Id, model?.Name);
var list = model == null ? _procedureStorage.GetFullList() : _procedureStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList. Returned null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
///
/// Получить отдельную запись
///
///
///
///
public ProcedureViewModel? ReadElement(ProcedureSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Procedure.Id: {Id}. Name: {Name}", model?.Id, model?.Name);
var element = _procedureStorage.GetElement(model!);
if (element == null)
{
_logger.LogWarning("ReadElement. Element not found");
return null;
}
_logger.LogInformation("ReadElement. Find Procedure.Id: {Id}", element?.Id);
return element;
}
///
/// Создать запись
///
///
///
public bool Create(ProcedureBindingModel model)
{
CheckModel(model);
_logger.LogInformation("Create. Procedure.Id: {Id}", model.Id);
if (_procedureStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
///
/// Изменить запись
///
///
///
public bool Update(ProcedureBindingModel model)
{
CheckModel(model);
_logger.LogInformation("Update. Procedure.Id: {Id}", model.Id);
if (_procedureStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
///
/// Удалить запись
///
///
///
public bool Delete(ProcedureBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Procedure.Id: {Id}", model.Id);
if (_procedureStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
///
/// Проверка модели
///
///
///
///
///
private void CheckModel(ProcedureBindingModel 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("CheckModel. Procedure.Id: {Id}", model.Id);
var element = _procedureStorage.GetElement(new ProcedureSearchModel
{
Name = model.Name
});
if (element != null && !element.Id.Equals(model.Id))
{
throw new InvalidOperationException("Процедура с таким названием уже существует");
}
}
}
}