180 lines
4.7 KiB
C#
180 lines
4.7 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 DiseaseLogic : IDiseaseLogic
|
||
{
|
||
/// <summary>
|
||
/// Логгер
|
||
/// </summary>
|
||
private readonly ILogger _logger;
|
||
|
||
/// <summary>
|
||
/// Хранилище
|
||
/// </summary>
|
||
private readonly IDiseaseStorage _diseaseStorage;
|
||
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
/// <param name="logger"></param>
|
||
/// <param name="diseaseStorage"></param>
|
||
public DiseaseLogic(ILogger<DiseaseLogic> logger, IDiseaseStorage diseaseStorage)
|
||
{
|
||
_logger = logger;
|
||
_diseaseStorage = diseaseStorage;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Получить список
|
||
/// </summary>
|
||
/// <param name="model"></param>
|
||
/// <returns></returns>
|
||
public List<DiseaseViewModel>? ReadList(DiseaseSearchModel? model)
|
||
{
|
||
_logger.LogInformation("ReadList. Disease.Id: {Id}. RecipeId: {RecipeId}. Name: {Name}", model?.Id, model?.RecipeId, model?.Name);
|
||
|
||
var list = model == null ? _diseaseStorage.GetFullList() : _diseaseStorage.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 DiseaseViewModel? ReadElement(DiseaseSearchModel model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
|
||
_logger.LogInformation("ReadElement. Disease.Id: {Id}. Name: {Name}", model?.Id, model?.Name);
|
||
|
||
var element = _diseaseStorage.GetElement(model!);
|
||
if (element == null)
|
||
{
|
||
_logger.LogWarning("ReadElement. Element not found");
|
||
return null;
|
||
}
|
||
|
||
_logger.LogInformation("ReadElement. Find Disease.Id: {Id}", element?.Id);
|
||
return element;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Создать запись
|
||
/// </summary>
|
||
/// <param name="model"></param>
|
||
/// <returns></returns>
|
||
public bool Create(DiseaseBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
_logger.LogInformation("Create. Disease.Id: {Id}", model.Id);
|
||
|
||
if (_diseaseStorage.Insert(model) == null)
|
||
{
|
||
_logger.LogWarning("Insert operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Изменить запись
|
||
/// </summary>
|
||
/// <param name="model"></param>
|
||
/// <returns></returns>
|
||
public bool Update(DiseaseBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
_logger.LogInformation("Update. Disease.Id: {Id}", model.Id);
|
||
|
||
if (_diseaseStorage.Update(model) == null)
|
||
{
|
||
_logger.LogWarning("Update operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Удалить запись
|
||
/// </summary>
|
||
/// <param name="model"></param>
|
||
/// <returns></returns>
|
||
public bool Delete(DiseaseBindingModel model)
|
||
{
|
||
CheckModel(model, false);
|
||
_logger.LogInformation("Delete. Disease.Id: {Id}", model.Id);
|
||
|
||
if (_diseaseStorage.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(DiseaseBindingModel 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));
|
||
}
|
||
if (model.RecipeId <= 0)
|
||
{
|
||
throw new ArgumentNullException("Не указан идентификатор рецепта", nameof(model.RecipeId));
|
||
}
|
||
|
||
_logger.LogInformation("CheckModel. Disease.Id: {Id}", model.Id);
|
||
|
||
var element = _diseaseStorage.GetElement(new DiseaseSearchModel
|
||
{
|
||
Name = model.Name
|
||
});
|
||
if (element != null && !element.Id.Equals(model.Id))
|
||
{
|
||
throw new InvalidOperationException("Болезнь с таким названием уже существует");
|
||
}
|
||
}
|
||
}
|
||
}
|