CourseWork_Borschevskaya_A..../Hospital/HospitalBusinessLogic/PatientLogic.cs

112 lines
3.6 KiB
C#
Raw Normal View History

using HospitalContracts.BindingModels;
using HospitalContracts.BusinessLogicContracts;
using HospitalContracts.SearchModels;
using HospitalContracts.StorageContracts;
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
{
public class PatientLogic : IPatientLogic
{
private readonly ILogger _logger;
private readonly IPatientStorage _patientStorage;
public PatientLogic(ILogger<PatientLogic> logger, IPatientStorage patientStorage)
{
_logger = logger;
_patientStorage = patientStorage;
}
public PatientViewModel? ReadElement(PatientSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Id:{ Id}", model.Id);
var element = _patientStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public List<PatientViewModel>? ReadList(PatientSearchModel? model)
{
_logger.LogInformation("ReadList. Id:{ Id}", model?.Id);
var list = model == null ? _patientStorage.GetFullList() : _patientStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public bool Create(PatientBindingModel model)
{
CheckModel(model);
if (_patientStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(PatientBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_patientStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public bool Update(PatientBindingModel model)
{
CheckModel(model);
if (_patientStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(PatientBindingModel 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));
}
// TODO: проверка на наличие даты рождения и фамилии в модели?
_logger.LogInformation("Patient. Surname:{ Date}. Name: {Name}. Patronymic: {Patronymic}. BirthDate: {BirthDate}. Id: { Id}", model.Surname, model.Name, model.Patronymic, model.BirthDate, model.Id);
}
}
}