PIbd-21_MasenkinMS_Coursewo.../Hospital/HospitalBusinessLogics/BusinessLogics/PatientLogic.cs
2024-04-25 23:50:21 +04:00

188 lines
5.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 HospitalBusinessLogics.BusinessLogics
{
/// <summary>
/// Бизнес-логика для сущности "Пациент"
/// </summary>
public class PatientLogic : IPatientLogic
{
/// <summary>
/// Логгер
/// </summary>
private readonly ILogger _logger;
/// <summary>
/// Хранилище
/// </summary>
private readonly IPatientStorage _patientStorage;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="logger"></param>
/// <param name="patientStorage"></param>
public PatientLogic(ILogger<PatientLogic> logger, IPatientStorage patientStorage)
{
_logger = logger;
_patientStorage = patientStorage;
}
/// <summary>
/// Получить список
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public List<PatientViewModel>? ReadList(PatientSearchModel? model)
{
_logger.LogInformation("ReadList. Patient.Id: {Id}. DoctorId: {DoctorId}. FullName: {FullName}", model?.Id, model?.DoctorId, model?.FullName);
var list = model == null ? _patientStorage.GetFullList() : _patientStorage.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 PatientViewModel? ReadElement(PatientSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Patient.Id: {Id}. Phone: {Phone}", model?.Id, model?.Phone);
var element = _patientStorage.GetElement(model!);
if (element == null)
{
_logger.LogWarning("ReadElement. Element not found");
return null;
}
_logger.LogInformation("ReadElement. Find Patient.Id: {Id}", element?.Id);
return element;
}
/// <summary>
/// Создать запись
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public bool Create(PatientBindingModel model)
{
CheckModel(model);
_logger.LogInformation("Create. Patient.Id: {Id}", model.Id);
if (_patientStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
/// <summary>
/// Изменить запись
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public bool Update(PatientBindingModel model)
{
CheckModel(model);
_logger.LogInformation("Update. Patient.Id: {Id}", model.Id);
if (_patientStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
/// <summary>
/// Удалить запись
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public bool Delete(PatientBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Patient.Id: {Id}", model.Id);
if (_patientStorage.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(PatientBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.FullName))
{
throw new ArgumentNullException("Не указано ФИО пациента", nameof(model.FullName));
}
if (model.BirthDate == DateTime.MinValue)
{
throw new ArgumentNullException("Не указана дата рождения пациента", nameof(model.BirthDate));
}
if (string.IsNullOrEmpty(model.Phone))
{
throw new ArgumentNullException("Не указан номер телефона", nameof(model.Phone));
}
if (model.DoctorId <= 0)
{
throw new ArgumentNullException("Не указан идентификатор лечащего врача", nameof(model.DoctorId));
}
_logger.LogInformation("CheckModel. Patient.Id: {Id}", model.Id);
var element = _patientStorage.GetElement(new PatientSearchModel
{
Phone = model.Phone
});
if (element != null && !element.Id.Equals(model.Id))
{
throw new InvalidOperationException("Пациент с таким номером телефона уже существует");
}
}
}
}