Refactor services and add MediatR for medical histories
- Updated MedicalHistoryController to use MediatR for commands/queries - Refactored methods in MedicalHistoryController to use expression-bodied members - Added HospitalId property to CurrentUserService - Removed UniversityId claims handling from JwtService - Updated AccountService to handle Student entities with UniversityId and SectionId - Removed IMedicalHistoryService interface and its implementation - Removed UniversityId and SectionId handling from UserService - Added SectionId property to SignUpRequestDto - Refactored MedicalHistory entity to remove personal information properties - Added CreateMedicalHistoryCommand, UpdateMedicalHistoryCommand, and DeleteMedicalHistoryCommand - Added command handlers for medical history commandsmaster
parent
ae1e8859c0
commit
b3ca3c51ea
|
@ -1,4 +1,6 @@
|
|||
using DocuMed.Domain.Entities.MedicalHistory;
|
||||
using DocuMed.Domain.CommandQueries.Commands;
|
||||
using DocuMed.Domain.Entities.MedicalHistory;
|
||||
using MediatR;
|
||||
|
||||
namespace DocuMed.Api.Controllers;
|
||||
public class MedicalHistoryController : ICarterModule
|
||||
|
@ -34,41 +36,25 @@ public class MedicalHistoryController : ICarterModule
|
|||
|
||||
// GET:Get All Entity
|
||||
public virtual async Task<IResult> GetAllByFilterAsync([FromQuery]DayQueryFilter dayQuery,[FromQuery] int page, IMedicalHistoryRepository repository, CancellationToken cancellationToken)
|
||||
{
|
||||
return TypedResults.Ok(await repository.GetMedicalHistoriesByFilterAsync(dayQuery , page, cancellationToken));
|
||||
}
|
||||
=> TypedResults.Ok(await repository.GetMedicalHistoriesByFilterAsync(dayQuery, page, cancellationToken));
|
||||
|
||||
// GET:Get All Entity
|
||||
public virtual async Task<IResult> GetAllAsync([FromQuery] int page, IMedicalHistoryRepository repository, CancellationToken cancellationToken)
|
||||
{
|
||||
return TypedResults.Ok(await repository.GetMedicalHistoriesAsync(page, cancellationToken));
|
||||
}
|
||||
=> TypedResults.Ok(await repository.GetMedicalHistoriesAsync(page, cancellationToken));
|
||||
|
||||
// GET:Get An Entity By Id
|
||||
public async Task<IResult> GetAsync(Guid id, IMedicalHistoryRepository repository, CancellationToken cancellationToken)
|
||||
{
|
||||
|
||||
return TypedResults.Ok(await repository.GetMedicalHistoryAsync(id, cancellationToken));
|
||||
}
|
||||
=> TypedResults.Ok(await repository.GetMedicalHistoryAsync(id, cancellationToken));
|
||||
|
||||
// POST:Add New Entity
|
||||
public virtual async Task<IResult> Post([FromBody] MedicalHistoryLDto dto, IMedicalHistoryService service, ICurrentUserService currentUserService, CancellationToken cancellationToken)
|
||||
{
|
||||
return TypedResults.Ok(await service.AddAsync(dto, cancellationToken));
|
||||
}
|
||||
public virtual async Task<IResult> Post([FromBody] CreateMedicalHistoryCommand dto, IMediator service, ICurrentUserService currentUserService, CancellationToken cancellationToken)
|
||||
=> TypedResults.Ok(await service.Send(dto, cancellationToken));
|
||||
|
||||
// PUT:Update Entity
|
||||
public virtual async Task<IResult> Put([FromBody] MedicalHistoryLDto dto, IMedicalHistoryService service, ICurrentUserService currentUserService, CancellationToken cancellationToken)
|
||||
{
|
||||
return TypedResults.Ok(await service.EditAsync(dto, cancellationToken));
|
||||
}
|
||||
public virtual async Task<IResult> Put([FromBody] UpdateMedicalHistoryCommand dto, IMediator service, ICurrentUserService currentUserService, CancellationToken cancellationToken)
|
||||
=> TypedResults.Ok(await service.Send(dto, cancellationToken));
|
||||
|
||||
// DELETE:Delete Entity
|
||||
public virtual async Task<IResult> Delete(Guid id, IRepositoryWrapper repositoryWrapper, CancellationToken cancellationToken)
|
||||
{
|
||||
var ent = await repositoryWrapper.SetRepository<MedicalHistory>().GetByIdAsync(cancellationToken, id);
|
||||
repositoryWrapper.SetRepository<MedicalHistory>().Delete(ent);
|
||||
await repositoryWrapper.SaveChangesAsync(cancellationToken);
|
||||
return TypedResults.Ok();
|
||||
}
|
||||
public virtual async Task<IResult> Delete(Guid id, IMediator mediator, CancellationToken cancellationToken)
|
||||
=> TypedResults.Ok(await mediator.Send(new DeleteMedicalHistoryCommand(id), cancellationToken));
|
||||
}
|
||||
|
|
|
@ -6,4 +6,5 @@ public class CurrentUserService(IHttpContextAccessor httpContextAccessor) : ICur
|
|||
public string? RoleName => httpContextAccessor.HttpContext?.User?.FindFirstValue(ClaimTypes.Role);
|
||||
public string? UserName => httpContextAccessor.HttpContext?.User?.FindFirstValue(ClaimTypes.Name);
|
||||
public string? UniversityId => httpContextAccessor.HttpContext?.User?.FindFirstValue("UniversityId");
|
||||
public string? HospitalId => httpContextAccessor.HttpContext?.User?.FindFirstValue("HospitalId");
|
||||
}
|
|
@ -82,8 +82,6 @@ public class JwtService(
|
|||
claims.Add(new Claim(ClaimTypes.NameIdentifier, baseUser.Id.ToString()));
|
||||
if (baseUser.Email != null)
|
||||
claims.Add(new Claim(ClaimTypes.Email, baseUser.Email));
|
||||
if(baseUser.UniversityId != null)
|
||||
claims.Add(new Claim("UniversityId",baseUser.UniversityId.ToString() ?? string.Empty ));
|
||||
claims.Add(new Claim(ClaimTypes.Gender, baseUser.Gender == 0 ? "Female" : "Mail"));
|
||||
return claims;
|
||||
|
||||
|
@ -99,8 +97,6 @@ public class JwtService(
|
|||
claims.Add(new Claim(ClaimTypes.Role, applicationRole.EnglishName));
|
||||
if (baseUser.Email != null)
|
||||
claims.Add(new Claim(ClaimTypes.Email, baseUser.Email));
|
||||
if (baseUser.UniversityId != null)
|
||||
claims.Add(new Claim("UniversityId", baseUser.UniversityId.ToString() ?? string.Empty));
|
||||
claims.AddRange(roleClaims);
|
||||
claims.Add(new Claim("JwtID", jwtId));
|
||||
claims.Add(new Claim(ClaimTypes.Gender, baseUser.Gender == 0 ? "Female" : "Mail"));
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
using DocuMed.Domain.Entities.City;
|
||||
using DocuMed.Domain.Entities.Staffs;
|
||||
using Section = DocuMed.Domain.Entities.Hospitals.Section;
|
||||
|
||||
namespace DocuMed.Core.CoreServices;
|
||||
|
@ -10,8 +10,7 @@ public class AccountService(
|
|||
ICurrentUserService currentUserService,
|
||||
IUserService userService,
|
||||
ISmsService smsService,
|
||||
IRepositoryWrapper repositoryWrapper)
|
||||
: IAccountService
|
||||
IRepositoryWrapper repositoryWrapper) : IAccountService
|
||||
{
|
||||
public async Task<bool> ForgetPasswordAsync(string phoneNumber)
|
||||
{
|
||||
|
@ -109,7 +108,17 @@ public class AccountService(
|
|||
user.FirstName = requestDto.FirstName;
|
||||
user.LastName = requestDto.LastName;
|
||||
user.SignUpStatus = SignUpStatus.SignUpCompleted;
|
||||
user.UniversityId = requestDto.UniversityId;
|
||||
|
||||
var student = await repositoryWrapper.SetRepository<Student>()
|
||||
.TableNoTracking
|
||||
.FirstOrDefaultAsync(f => f.UserId == user.Id, cancellationToken);
|
||||
if (student == null)
|
||||
{
|
||||
student = Student.Create(requestDto.UniversityId,requestDto.SectionId,user.Id);
|
||||
repositoryWrapper.SetRepository<Student>().Add(student);
|
||||
await repositoryWrapper.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
var result = await userManager.UpdateAsync(user);
|
||||
if (!result.Succeeded)
|
||||
throw new AppException(string.Join('|', result.Errors));
|
||||
|
@ -121,11 +130,13 @@ public class AccountService(
|
|||
private async Task<AccessToken<ApplicationUserSDto>> CompleteLogin(ApplicationUser user, CancellationToken cancellationToken)
|
||||
{
|
||||
var token = await jwtService.Generate<ApplicationUserSDto, ApplicationUser>(user);
|
||||
var student = await repositoryWrapper.SetRepository<Student>().TableNoTracking
|
||||
.FirstOrDefaultAsync(s => s.UserId == user.Id, cancellationToken);
|
||||
|
||||
if (token.User.SectionId != Guid.Empty)
|
||||
if (student != null)
|
||||
{
|
||||
var section = await repositoryWrapper.SetRepository<Section>().TableNoTracking
|
||||
.FirstOrDefaultAsync(s => s.Id == user.SectionId, cancellationToken);
|
||||
.FirstOrDefaultAsync(s => s.Id == student.SectionId, cancellationToken);
|
||||
if (section != null)
|
||||
{
|
||||
token.User.SectionName = section.Name;
|
||||
|
|
|
@ -1,8 +0,0 @@
|
|||
namespace DocuMed.Core.EntityServices.Abstracts;
|
||||
|
||||
public interface IMedicalHistoryService : IScopedDependency
|
||||
{
|
||||
|
||||
public Task<bool> EditAsync(MedicalHistoryLDto template, CancellationToken cancellationToken);
|
||||
public Task<bool> AddAsync(MedicalHistoryLDto template, CancellationToken cancellationToken);
|
||||
}
|
|
@ -1,70 +0,0 @@
|
|||
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;
|
||||
}
|
||||
}
|
|
@ -45,8 +45,7 @@ public class UserService(
|
|||
NationalId = request.NationalId,
|
||||
BirthDate = request.BirthDate,
|
||||
Gender = request.Gender,
|
||||
SignUpStatus = SignUpStatus.SignUpCompleted,
|
||||
UniversityId = request.UniversityId
|
||||
SignUpStatus = SignUpStatus.PhoneNumberVerified,
|
||||
};
|
||||
if (!request.Password.IsNullOrEmpty())
|
||||
{
|
||||
|
@ -80,16 +79,11 @@ public class UserService(
|
|||
user.FirstName = request.FirstName;
|
||||
user.UserName = request.PhoneNumber;
|
||||
user.PhoneNumber = request.PhoneNumber;
|
||||
user.StudentId = request.StudentId;
|
||||
user.FirstName = request.FirstName;
|
||||
user.LastName = request.LastName;
|
||||
user.NationalId = request.NationalId;
|
||||
user.BirthDate = request.BirthDate;
|
||||
user.Gender = request.Gender;
|
||||
if (request.UniversityId != Guid.Empty)
|
||||
user.UniversityId = request.UniversityId;
|
||||
if (request.SectionId != Guid.Empty)
|
||||
user.SectionId = request.SectionId;
|
||||
|
||||
var result = await userManager.UpdateAsync(user);
|
||||
if (!result.Succeeded)
|
||||
|
|
|
@ -0,0 +1,58 @@
|
|||
namespace DocuMed.Domain.CommandQueries.Commands;
|
||||
|
||||
public sealed record CreateMedicalHistoryCommand(
|
||||
string ChiefComplaint,
|
||||
Guid SectionId,
|
||||
string FirstName,
|
||||
string LastName,
|
||||
string FatherName,
|
||||
string NationalId,
|
||||
DateTime BirthDate,
|
||||
string PresentIllnessDetail,
|
||||
string PastDiseasesHistoryDetail,
|
||||
string PastSurgeryHistoryDetail,
|
||||
string FamilyHistoryDetail,
|
||||
string AllergyDetail,
|
||||
string DrugHistoryDetail,
|
||||
string AddictionHistoryDetail,
|
||||
string SystemReviewDetail,
|
||||
string VitalSignDetail,
|
||||
string GeneralAppearanceDetail,
|
||||
double SystolicBloodPressure,
|
||||
double DiastolicBloodPressure,
|
||||
double PulseRate,
|
||||
double SPO2,
|
||||
double Temperature,
|
||||
Guid ApplicationUserId,
|
||||
Guid MedicalHistoryTemplateId,
|
||||
List<MedicalHistoryAnswerSDto> Answers) : IRequest<Guid>;
|
||||
|
||||
public sealed record UpdateMedicalHistoryCommand(
|
||||
Guid Id,
|
||||
string ChiefComplaint,
|
||||
Guid SectionId,
|
||||
string FirstName,
|
||||
string LastName,
|
||||
string FatherName,
|
||||
string NationalId,
|
||||
DateTime BirthDate,
|
||||
string PresentIllnessDetail,
|
||||
string PastDiseasesHistoryDetail,
|
||||
string PastSurgeryHistoryDetail,
|
||||
string FamilyHistoryDetail,
|
||||
string AllergyDetail,
|
||||
string DrugHistoryDetail,
|
||||
string AddictionHistoryDetail,
|
||||
string SystemReviewDetail,
|
||||
string VitalSignDetail,
|
||||
string GeneralAppearanceDetail,
|
||||
double SystolicBloodPressure,
|
||||
double DiastolicBloodPressure,
|
||||
double PulseRate,
|
||||
double SPO2,
|
||||
double Temperature,
|
||||
Guid ApplicationUserId,
|
||||
Guid MedicalHistoryTemplateId,
|
||||
List<MedicalHistoryAnswerSDto> Answers) : IRequest<Guid>;
|
||||
|
||||
public sealed record DeleteMedicalHistoryCommand(Guid Id) : IRequest<Guid>;
|
|
@ -5,5 +5,6 @@ public class SignUpRequestDto
|
|||
public string FirstName { get; set; } = string.Empty;
|
||||
public string LastName { get; set; } = string.Empty;
|
||||
public Guid UniversityId { get; set; }
|
||||
public Guid SectionId { get; set; }
|
||||
public Guid CityId { get; set; }
|
||||
}
|
|
@ -26,12 +26,6 @@ public partial class MedicalHistory
|
|||
public static MedicalHistory Create(
|
||||
string chiefComplaint,
|
||||
Guid sectionId,
|
||||
string firstName,
|
||||
string lastName,
|
||||
string fatherName,
|
||||
string nationalId,
|
||||
int age,
|
||||
DateTime birthDate,
|
||||
string presentIllnessDetail,
|
||||
string pastDiseasesHistoryDetail,
|
||||
string pastSurgeryHistoryDetail,
|
||||
|
@ -63,12 +57,6 @@ public partial class MedicalHistory
|
|||
generalAppearanceDetail,
|
||||
chiefComplaint,
|
||||
sectionId,
|
||||
firstName,
|
||||
lastName,
|
||||
fatherName,
|
||||
nationalId,
|
||||
age,
|
||||
birthDate,
|
||||
systolicBloodPressure,
|
||||
diastolicBloodPressure,
|
||||
pulseRate,
|
||||
|
|
|
@ -25,12 +25,6 @@ public partial class MedicalHistory : ApiEntity
|
|||
string generalAppearanceDetail,
|
||||
string chiefComplaint,
|
||||
Guid sectionId,
|
||||
string firstName,
|
||||
string lastName,
|
||||
string fatherName,
|
||||
string nationalId,
|
||||
int age,
|
||||
DateTime birthDate,
|
||||
double systolicBloodPressure,
|
||||
double diastolicBloodPressure,
|
||||
double pulseRate,
|
||||
|
@ -52,12 +46,6 @@ public partial class MedicalHistory : ApiEntity
|
|||
GeneralAppearanceDetail = generalAppearanceDetail;
|
||||
ChiefComplaint = chiefComplaint;
|
||||
SectionId = sectionId;
|
||||
FirstName = firstName;
|
||||
LastName = lastName;
|
||||
FatherName = fatherName;
|
||||
NationalId = nationalId;
|
||||
Age = age;
|
||||
BirthDate = birthDate;
|
||||
SystolicBloodPressure = systolicBloodPressure;
|
||||
DiastolicBloodPressure = diastolicBloodPressure;
|
||||
PulseRate = pulseRate;
|
||||
|
@ -71,13 +59,6 @@ public partial class MedicalHistory : ApiEntity
|
|||
public Guid SectionId { get; internal set; }
|
||||
public Section? Section { get; internal set; }
|
||||
|
||||
public string FirstName { get; internal set; } = string.Empty;
|
||||
public string LastName { get; internal set; } = string.Empty;
|
||||
public string FatherName { get; internal set; } = string.Empty;
|
||||
public string NationalId { get; internal set; } = string.Empty;
|
||||
public int Age { get; internal set; }
|
||||
public DateTime BirthDate { get; internal set; }
|
||||
|
||||
public string PresentIllnessDetail { get; internal set; } = string.Empty;
|
||||
public string PastDiseasesHistoryDetail { get; internal set; } = string.Empty;
|
||||
public string PastSurgeryHistoryDetail { get; internal set; } = string.Empty;
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
namespace DocuMed.Repository.Handlers.MedicalHistories;
|
||||
|
||||
public class CreateMedicalHistoryCommandHandler(IRepositoryWrapper repositoryWrapper,ICurrentUserService currentUserService) : IRequestHandler<CreateMedicalHistoryCommand,Guid>
|
||||
{
|
||||
public async Task<Guid> Handle(CreateMedicalHistoryCommand 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.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);
|
||||
|
||||
repositoryWrapper.SetRepository<MedicalHistory>().Add(ent);
|
||||
await repositoryWrapper.SaveChangesAsync(cancellationToken);
|
||||
return ent.Id;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
namespace DocuMed.Repository.Handlers.MedicalHistories;
|
||||
|
||||
public class DeleteMedicalHistoryCommandHandler(IRepositoryWrapper repositoryWrapper) : IRequestHandler<DeleteMedicalHistoryCommand, Guid>
|
||||
{
|
||||
public async Task<Guid> Handle(DeleteMedicalHistoryCommand template, CancellationToken cancellationToken)
|
||||
{
|
||||
var ent = await repositoryWrapper.SetRepository<MedicalHistory>().TableNoTracking
|
||||
.FirstOrDefaultAsync(m => m.Id == template.Id, cancellationToken);
|
||||
if (ent == null)
|
||||
throw new AppException("شرح حال پیدا نشد", ApiResultStatusCode.NotFound);
|
||||
|
||||
repositoryWrapper.SetRepository<MedicalHistory>().Delete(ent);
|
||||
await repositoryWrapper.SaveChangesAsync(cancellationToken);
|
||||
return ent.Id;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
namespace DocuMed.Repository.Handlers.MedicalHistories;
|
||||
|
||||
public class UpdateMedicalHistoryCommandHandler(IRepositoryWrapper repositoryWrapper, ICurrentUserService currentUserService) : IRequestHandler<UpdateMedicalHistoryCommand, Guid>
|
||||
{
|
||||
public async Task<Guid> Handle(UpdateMedicalHistoryCommand template, CancellationToken cancellationToken)
|
||||
{
|
||||
if (!Guid.TryParse(currentUserService.UserId, out Guid userId))
|
||||
throw new AppException("دسترسی غیرمجاز", ApiResultStatusCode.UnAuthorized);
|
||||
|
||||
var ent = await repositoryWrapper.SetRepository<MedicalHistory>().TableNoTracking
|
||||
.FirstOrDefaultAsync(m => m.Id == template.Id, cancellationToken);
|
||||
if(ent==null)
|
||||
throw new AppException("شرح حال پیدا نشد", ApiResultStatusCode.NotFound);
|
||||
var newEnt = MedicalHistory.Create(template.ChiefComplaint, template.SectionId,
|
||||
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);
|
||||
newEnt.Id = ent.Id;
|
||||
newEnt.CreatedAt = ent.CreatedAt;
|
||||
|
||||
|
||||
foreach (var answer in template.Answers.Where(a => a.Id == Guid.Empty))
|
||||
newEnt.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);
|
||||
}
|
||||
}
|
||||
|
||||
repositoryWrapper.SetRepository<MedicalHistory>().Update(newEnt);
|
||||
await repositoryWrapper.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return newEnt.Id;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue