101 lines
3.8 KiB
C#

using MedicalDatabaseContracts;
using MedicalDatabaseContracts.Models;
using MedicalDatabaseContracts.SearchModels;
using MedicalDatabaseContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace MedicalBusinessLogic.BusinessLogics
{
public class VisitLogic : AbstractLogic<Visit, VisitViewModel, VisitSearchModel>
{
private readonly IStorage<Patient> _patientStorage;
private readonly IStorage<Doctor> _doctorStorage;
private readonly IStorage<Diagnose> _diagnoseStorage;
public VisitLogic(
ILogger<AbstractLogic<Visit, VisitViewModel, VisitSearchModel>> logger,
IStorage<Visit> storage,
IStorage<Patient> patientStorage,
IStorage<Doctor> doctorStorage,
IStorage<Diagnose> diagnoseStorage) : base(logger, storage)
{
_patientStorage = patientStorage;
_doctorStorage = doctorStorage;
_diagnoseStorage = diagnoseStorage;
}
protected override bool CheckModelBySearchModel(Visit model, VisitSearchModel searchModel)
{
if (searchModel.Id != null && searchModel.Id != model.Id)
return false;
if (searchModel.DoctorId != null && searchModel.DoctorId != model.DoctorId)
return false;
if (searchModel.DiagnoseId != null && searchModel.DiagnoseId != model.DiagnoseId)
return false;
if (searchModel.PatientId != null && searchModel.PatientId != model.PatientId)
return false;
if (!string.IsNullOrEmpty(searchModel.Comment))
{
if (model.Comment == null)
return false;
if (!model.Comment.Contains(searchModel.Comment))
return false;
}
if (searchModel.Date != null && model.Date != searchModel.Date)
return false;
if (searchModel.Time != null && model.Time != searchModel.Time)
return false;
return true;
}
protected override void CheckModelIsValid(Visit model, bool modelUpdate = false)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!modelUpdate && ReadList(new VisitSearchModel {
DoctorId = model.DoctorId,
Date = model.Date,
Time = model.Time
}).Count != 0)
{
throw new InvalidOperationException($"Запись приема к этому врачу на это время и дату уже есть");
}
}
protected override VisitViewModel GetViewModel(Visit model)
{
var visitViewModel = new VisitViewModel {
Id = model.Id,
PatientId = model.PatientId,
DoctorId = model.DoctorId,
DiagnoseId = model.DiagnoseId,
Date = model.Date,
Time = model.Time,
Comment = model.Comment ?? string.Empty,
};
var patient = _patientStorage.Get(model.PatientId);
if (patient != null)
{
visitViewModel.PatientFIO = patient.GetFIO();
}
var doctor = _doctorStorage.Get(model.DoctorId);
if (doctor != null)
{
visitViewModel.DoctorFIO = doctor.GetFIO();
}
if (model.DiagnoseId != null)
{
var diagnose = _diagnoseStorage.Get(model.DiagnoseId.Value);
if (diagnose != null)
{
visitViewModel.DiagnoseName = diagnose.Name;
}
}
return visitViewModel;
}
}
}