Rogashova_E.A._CourseWork_H.../Hospital/HospitalBusinessLogic/BusinessLogics/IllnessLogic.cs

113 lines
4.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 HospitalBusinessLogic.BusinessLogics
{
public class IllnessLogic : IIllnessLogic
{
private readonly ILogger _logger;
private readonly IIllnessStorage _illnessStorage;
public IllnessLogic(ILogger<IllnessLogic> logger, IIllnessStorage illnessStorage)
{
_logger = logger;
_illnessStorage = illnessStorage;
}
public List<IllnessViewModel>? ReadList(IllnessSearchModel? model)
{
_logger.LogInformation("ReadList. IllnessName:{IllnessName}.Id:{ Id}", model?.IllnessName, model?.Id);
var list = model == null ? _illnessStorage.GetFullList() : _illnessStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public IllnessViewModel? ReadElement(IllnessSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. IllnessName:{IllnessName}.Id:{ Id}", model.IllnessName, model.Id);
var element = _illnessStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public bool Create(IllnessBindingModel model)
{
CheckModel(model);
if (_illnessStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(IllnessBindingModel model)
{
CheckModel(model);
if (_illnessStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(IllnessBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_illnessStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(IllnessBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.IllnessName))
{
throw new ArgumentNullException("Нет названия болезни",
nameof(model.IllnessName));
}
if (string.IsNullOrEmpty(model.Form))
{
throw new ArgumentNullException("Нет формы болезни", nameof(model.Form));
}
_logger.LogInformation("Illness. IllnessName:{IllnessName}.Form:{ Form}. Id: { Id}", model.IllnessName, model.Form, model.Id);
var element = _illnessStorage.GetElement(new IllnessSearchModel
{
IllnessName = model.IllnessName });
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Болезнь с таким названием уже есть");
}
}
}
}