70 lines
3.8 KiB
C#
70 lines
3.8 KiB
C#
namespace DocuMed.Core.EntityServices;
|
|
|
|
public class MedicalHistoryService(
|
|
IRepositoryWrapper repositoryWrapper,
|
|
ICurrentUserService currentUserService,
|
|
IMedicalHistoryRepository medicalHistoryRepository)
|
|
: IMedicalHistoryService
|
|
{
|
|
public async Task<bool> EditAsync(MedicalHistoryLDto template, CancellationToken cancellationToken)
|
|
{
|
|
if (!Guid.TryParse(currentUserService.UserId, out Guid userId))
|
|
throw new AppException("دسترسی غیرمجاز", ApiResultStatusCode.UnAuthorized);
|
|
if (template.Id == Guid.Empty)
|
|
throw new AppException("شرح حال پیدا نشد", ApiResultStatusCode.NotFound);
|
|
|
|
var ent = MedicalHistory.Create(template.ChiefComplaint, template.SectionId, template.FirstName,
|
|
template.LastName, template.FatherName, template.NationalId, template.Age, template.BirthDate,
|
|
template.PresentIllnessDetail, template.PastDiseasesHistoryDetail, template.PastSurgeryHistoryDetail,
|
|
template.FamilyHistoryDetail, template.AllergyDetail, template.DrugHistoryDetail,
|
|
template.AddictionHistoryDetail, template.SystemReviewDetail, template.VitalSignDetail, template.GeneralAppearanceDetail,
|
|
template.SystolicBloodPressure, template.DiastolicBloodPressure, template.PulseRate, template.SPO2,
|
|
template.Temperature, template.ApplicationUserId, template.MedicalHistoryTemplateId);
|
|
ent.Id = template.Id;
|
|
ent.CreatedAt = template.CreatedAt;
|
|
|
|
|
|
foreach (var answer in template.Answers.Where(a=>a.Id == Guid.Empty))
|
|
ent.AddAnswer(answer.Answer, answer.Question, answer.Part, answer.QuestionType);
|
|
|
|
|
|
foreach (var answer in template.Answers.Where(a => a.Id != Guid.Empty))
|
|
{
|
|
var dbAnswer = await repositoryWrapper.SetRepository<MedicalHistoryAnswer>().TableNoTracking
|
|
.FirstOrDefaultAsync(a => a.Id == answer.Id, cancellationToken);
|
|
if (dbAnswer != null && dbAnswer.Answer != answer.Answer && answer.Answer != null)
|
|
{
|
|
dbAnswer = MedicalHistoryAnswer.Create(answer.Answer, answer.Question, answer.Part, answer.QuestionType,
|
|
dbAnswer.MedicalHistoryId);
|
|
dbAnswer.Id = answer.Id;
|
|
repositoryWrapper.SetRepository<MedicalHistoryAnswer>().Update(dbAnswer);
|
|
await repositoryWrapper.SaveChangesAsync(cancellationToken);
|
|
}
|
|
}
|
|
|
|
medicalHistoryRepository.Update(ent);
|
|
await repositoryWrapper.SaveChangesAsync(cancellationToken);
|
|
|
|
return true;
|
|
}
|
|
|
|
public async Task<bool> AddAsync(MedicalHistoryLDto template, CancellationToken cancellationToken)
|
|
{
|
|
if (!Guid.TryParse(currentUserService.UserId, out Guid userId))
|
|
throw new AppException("دسترسی غیرمجاز", ApiResultStatusCode.UnAuthorized);
|
|
var ent = MedicalHistory.Create(template.ChiefComplaint, template.SectionId, template.FirstName,
|
|
template.LastName, template.FatherName, template.NationalId, template.Age, template.BirthDate,
|
|
template.PresentIllnessDetail, template.PastDiseasesHistoryDetail, template.PastSurgeryHistoryDetail,
|
|
template.FamilyHistoryDetail, template.AllergyDetail, template.DrugHistoryDetail,
|
|
template.AddictionHistoryDetail, template.SystemReviewDetail, template.VitalSignDetail, template.GeneralAppearanceDetail,
|
|
template.SystolicBloodPressure, template.DiastolicBloodPressure, template.PulseRate, template.SPO2,
|
|
template.Temperature, userId,template.MedicalHistoryTemplateId);
|
|
|
|
foreach (var answer in template.Answers)
|
|
ent.AddAnswer(answer.Answer, answer.Question, answer.Part, answer.QuestionType);
|
|
|
|
medicalHistoryRepository.Add(ent);
|
|
await repositoryWrapper.SaveChangesAsync(cancellationToken);
|
|
return true;
|
|
}
|
|
} |