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 RecipeLogic : IRecipeLogic
{
///
/// Логгер
///
private readonly ILogger _logger;
///
/// Хранилище
///
private readonly IRecipeStorage _recipeStorage;
///
/// Конструктор
///
///
///
public RecipeLogic(ILogger logger, IRecipeStorage recipeStorage)
{
_logger = logger;
_recipeStorage = recipeStorage;
}
///
/// Получить список
///
///
///
public List? ReadList(RecipeSearchModel? model)
{
_logger.LogInformation("ReadList. Recipe.Id: {Id}. DoctorId: {DoctorId}", model?.Id, model?.DoctorId);
var list = model == null ? _recipeStorage.GetFullList() : _recipeStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList. Returned null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
///
/// Получить отдельную запись
///
///
///
///
public RecipeViewModel? ReadElement(RecipeSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Recipe.Id: {Id}", model?.Id);
var element = _recipeStorage.GetElement(model!);
if (element == null)
{
_logger.LogWarning("ReadElement. Element not found");
return null;
}
_logger.LogInformation("ReadElement. Find Recipe.Id: {Id}", element?.Id);
return element;
}
///
/// Создать запись
///
///
///
public bool Create(RecipeBindingModel model)
{
CheckModel(model);
_logger.LogInformation("Create. Recipe.Id: {Id}", model.Id);
if (_recipeStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
///
/// Изменить запись
///
///
///
public bool Update(RecipeBindingModel model)
{
CheckModel(model);
_logger.LogInformation("Update. Recipe.Id: {Id}", model.Id);
if (_recipeStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
///
/// Удалить запись
///
///
///
public bool Delete(RecipeBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Recipe.Id: {Id}", model.Id);
if (_recipeStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
///
/// Проверка модели
///
///
///
///
///
private void CheckModel(RecipeBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.IssueDate == DateTime.MinValue)
{
throw new ArgumentNullException("Не указана дата выписки рецепта", nameof(model.IssueDate));
}
if (model.DoctorId <= 0)
{
throw new ArgumentNullException("Не указан идентификатор доктора", nameof(model.DoctorId));
}
_logger.LogInformation("CheckModel. Recipe.Id: {Id}", model.Id);
}
}
}