171 lines
4.4 KiB
C#
171 lines
4.4 KiB
C#
|
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
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Бизнес-логика для сущности "Рецепт"
|
|||
|
/// </summary>
|
|||
|
public class RecipeLogic : IRecipeLogic
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Логгер
|
|||
|
/// </summary>
|
|||
|
private readonly ILogger _logger;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Хранилище
|
|||
|
/// </summary>
|
|||
|
private readonly IRecipeStorage _recipeStorage;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Конструктор
|
|||
|
/// </summary>
|
|||
|
/// <param name="logger"></param>
|
|||
|
/// <param name="recipeStorage"></param>
|
|||
|
public RecipeLogic(ILogger<RecipeLogic> logger, IRecipeStorage recipeStorage)
|
|||
|
{
|
|||
|
_logger = logger;
|
|||
|
_recipeStorage = recipeStorage;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Получить список
|
|||
|
/// </summary>
|
|||
|
/// <param name="model"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public List<RecipeViewModel>? 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;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Получить отдельную запись
|
|||
|
/// </summary>
|
|||
|
/// <param name="model"></param>
|
|||
|
/// <returns></returns>
|
|||
|
/// <exception cref="ArgumentNullException"></exception>
|
|||
|
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;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Создать запись
|
|||
|
/// </summary>
|
|||
|
/// <param name="model"></param>
|
|||
|
/// <returns></returns>
|
|||
|
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;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Изменить запись
|
|||
|
/// </summary>
|
|||
|
/// <param name="model"></param>
|
|||
|
/// <returns></returns>
|
|||
|
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;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Удалить запись
|
|||
|
/// </summary>
|
|||
|
/// <param name="model"></param>
|
|||
|
/// <returns></returns>
|
|||
|
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;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Проверка модели
|
|||
|
/// </summary>
|
|||
|
/// <param name="model"></param>
|
|||
|
/// <param name="withParams"></param>
|
|||
|
/// <exception cref="ArgumentNullException"></exception>
|
|||
|
/// <exception cref="InvalidOperationException"></exception>
|
|||
|
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);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|