diff --git a/DocuMed.Api/Controllers/MedicalHistoryController.cs b/DocuMed.Api/Controllers/MedicalHistoryController.cs index a1a9036..3f1fe3b 100644 --- a/DocuMed.Api/Controllers/MedicalHistoryController.cs +++ b/DocuMed.Api/Controllers/MedicalHistoryController.cs @@ -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 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 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 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 Post([FromBody] MedicalHistoryLDto dto, IMedicalHistoryService service, ICurrentUserService currentUserService, CancellationToken cancellationToken) - { - return TypedResults.Ok(await service.AddAsync(dto, cancellationToken)); - } + public virtual async Task Post([FromBody] CreateMedicalHistoryCommand dto, IMediator service, ICurrentUserService currentUserService, CancellationToken cancellationToken) + => TypedResults.Ok(await service.Send(dto, cancellationToken)); // PUT:Update Entity - public virtual async Task Put([FromBody] MedicalHistoryLDto dto, IMedicalHistoryService service, ICurrentUserService currentUserService, CancellationToken cancellationToken) - { - return TypedResults.Ok(await service.EditAsync(dto, cancellationToken)); - } + public virtual async Task Put([FromBody] UpdateMedicalHistoryCommand dto, IMediator service, ICurrentUserService currentUserService, CancellationToken cancellationToken) + => TypedResults.Ok(await service.Send(dto, cancellationToken)); // DELETE:Delete Entity - public virtual async Task Delete(Guid id, IRepositoryWrapper repositoryWrapper, CancellationToken cancellationToken) - { - var ent = await repositoryWrapper.SetRepository().GetByIdAsync(cancellationToken, id); - repositoryWrapper.SetRepository().Delete(ent); - await repositoryWrapper.SaveChangesAsync(cancellationToken); - return TypedResults.Ok(); - } + public virtual async Task Delete(Guid id, IMediator mediator, CancellationToken cancellationToken) + => TypedResults.Ok(await mediator.Send(new DeleteMedicalHistoryCommand(id), cancellationToken)); } diff --git a/DocuMed.Api/Services/CurrentUserService.cs b/DocuMed.Api/Services/CurrentUserService.cs index 80d87b4..50846a3 100644 --- a/DocuMed.Api/Services/CurrentUserService.cs +++ b/DocuMed.Api/Services/CurrentUserService.cs @@ -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"); } \ No newline at end of file diff --git a/DocuMed.Core/BaseServices/JwtService.cs b/DocuMed.Core/BaseServices/JwtService.cs index eb042a3..8210af5 100644 --- a/DocuMed.Core/BaseServices/JwtService.cs +++ b/DocuMed.Core/BaseServices/JwtService.cs @@ -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")); diff --git a/DocuMed.Core/CoreServices/AccountService.cs b/DocuMed.Core/CoreServices/AccountService.cs index 187c9c4..e74767a 100644 --- a/DocuMed.Core/CoreServices/AccountService.cs +++ b/DocuMed.Core/CoreServices/AccountService.cs @@ -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 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() + .TableNoTracking + .FirstOrDefaultAsync(f => f.UserId == user.Id, cancellationToken); + if (student == null) + { + student = Student.Create(requestDto.UniversityId,requestDto.SectionId,user.Id); + repositoryWrapper.SetRepository().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> CompleteLogin(ApplicationUser user, CancellationToken cancellationToken) { var token = await jwtService.Generate(user); + var student = await repositoryWrapper.SetRepository().TableNoTracking + .FirstOrDefaultAsync(s => s.UserId == user.Id, cancellationToken); - if (token.User.SectionId != Guid.Empty) + if (student != null) { var section = await repositoryWrapper.SetRepository
().TableNoTracking - .FirstOrDefaultAsync(s => s.Id == user.SectionId, cancellationToken); + .FirstOrDefaultAsync(s => s.Id == student.SectionId, cancellationToken); if (section != null) { token.User.SectionName = section.Name; diff --git a/DocuMed.Core/EntityServices/Abstracts/IMedicalHistoryService.cs b/DocuMed.Core/EntityServices/Abstracts/IMedicalHistoryService.cs deleted file mode 100644 index ec7f03c..0000000 --- a/DocuMed.Core/EntityServices/Abstracts/IMedicalHistoryService.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace DocuMed.Core.EntityServices.Abstracts; - -public interface IMedicalHistoryService : IScopedDependency -{ - - public Task EditAsync(MedicalHistoryLDto template, CancellationToken cancellationToken); - public Task AddAsync(MedicalHistoryLDto template, CancellationToken cancellationToken); -} \ No newline at end of file diff --git a/DocuMed.Core/EntityServices/MedicalHistoryService.cs b/DocuMed.Core/EntityServices/MedicalHistoryService.cs deleted file mode 100644 index 376cf7d..0000000 --- a/DocuMed.Core/EntityServices/MedicalHistoryService.cs +++ /dev/null @@ -1,70 +0,0 @@ -namespace DocuMed.Core.EntityServices; - -public class MedicalHistoryService( - IRepositoryWrapper repositoryWrapper, - ICurrentUserService currentUserService, - IMedicalHistoryRepository medicalHistoryRepository) - : IMedicalHistoryService -{ - public async Task 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().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().Update(dbAnswer); - await repositoryWrapper.SaveChangesAsync(cancellationToken); - } - } - - medicalHistoryRepository.Update(ent); - await repositoryWrapper.SaveChangesAsync(cancellationToken); - - return true; - } - - public async Task 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; - } -} \ No newline at end of file diff --git a/DocuMed.Core/EntityServices/UserService.cs b/DocuMed.Core/EntityServices/UserService.cs index 6c972bf..1cb4ecf 100644 --- a/DocuMed.Core/EntityServices/UserService.cs +++ b/DocuMed.Core/EntityServices/UserService.cs @@ -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) diff --git a/DocuMed.Domain/CommandQueries/Commands/MedicalHistoryCommands.cs b/DocuMed.Domain/CommandQueries/Commands/MedicalHistoryCommands.cs new file mode 100644 index 0000000..44e460e --- /dev/null +++ b/DocuMed.Domain/CommandQueries/Commands/MedicalHistoryCommands.cs @@ -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 Answers) : IRequest; + +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 Answers) : IRequest; + +public sealed record DeleteMedicalHistoryCommand(Guid Id) : IRequest; diff --git a/DocuMed.Domain/Dtos/RequestDtos/SignUpRequestDto.cs b/DocuMed.Domain/Dtos/RequestDtos/SignUpRequestDto.cs index ccb9509..48a88b3 100644 --- a/DocuMed.Domain/Dtos/RequestDtos/SignUpRequestDto.cs +++ b/DocuMed.Domain/Dtos/RequestDtos/SignUpRequestDto.cs @@ -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; } } \ No newline at end of file diff --git a/DocuMed.Domain/Entities/MedicalHistory/MedicalHistory.Aggregate.cs b/DocuMed.Domain/Entities/MedicalHistory/MedicalHistory.Aggregate.cs index f58b6cb..9c62550 100644 --- a/DocuMed.Domain/Entities/MedicalHistory/MedicalHistory.Aggregate.cs +++ b/DocuMed.Domain/Entities/MedicalHistory/MedicalHistory.Aggregate.cs @@ -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, diff --git a/DocuMed.Domain/Entities/MedicalHistory/MedicalHistory.cs b/DocuMed.Domain/Entities/MedicalHistory/MedicalHistory.cs index 0595f28..047ed3d 100644 --- a/DocuMed.Domain/Entities/MedicalHistory/MedicalHistory.cs +++ b/DocuMed.Domain/Entities/MedicalHistory/MedicalHistory.cs @@ -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; diff --git a/DocuMed.Repository/Handlers/MedicalHistories/CreateMedicalHistoryCommandHandler.cs b/DocuMed.Repository/Handlers/MedicalHistories/CreateMedicalHistoryCommandHandler.cs new file mode 100644 index 0000000..ecc2eed --- /dev/null +++ b/DocuMed.Repository/Handlers/MedicalHistories/CreateMedicalHistoryCommandHandler.cs @@ -0,0 +1,24 @@ +namespace DocuMed.Repository.Handlers.MedicalHistories; + +public class CreateMedicalHistoryCommandHandler(IRepositoryWrapper repositoryWrapper,ICurrentUserService currentUserService) : IRequestHandler +{ + public async Task 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().Add(ent); + await repositoryWrapper.SaveChangesAsync(cancellationToken); + return ent.Id; + } +} \ No newline at end of file diff --git a/DocuMed.Repository/Handlers/MedicalHistories/DeleteMedicalHistoryCommandHandler.cs b/DocuMed.Repository/Handlers/MedicalHistories/DeleteMedicalHistoryCommandHandler.cs new file mode 100644 index 0000000..e29abc1 --- /dev/null +++ b/DocuMed.Repository/Handlers/MedicalHistories/DeleteMedicalHistoryCommandHandler.cs @@ -0,0 +1,16 @@ +namespace DocuMed.Repository.Handlers.MedicalHistories; + +public class DeleteMedicalHistoryCommandHandler(IRepositoryWrapper repositoryWrapper) : IRequestHandler +{ + public async Task Handle(DeleteMedicalHistoryCommand template, CancellationToken cancellationToken) + { + var ent = await repositoryWrapper.SetRepository().TableNoTracking + .FirstOrDefaultAsync(m => m.Id == template.Id, cancellationToken); + if (ent == null) + throw new AppException("شرح حال پیدا نشد", ApiResultStatusCode.NotFound); + + repositoryWrapper.SetRepository().Delete(ent); + await repositoryWrapper.SaveChangesAsync(cancellationToken); + return ent.Id; + } +} \ No newline at end of file diff --git a/DocuMed.Repository/Handlers/MedicalHistories/UpdateMedicalHistoryCommandHandler.cs b/DocuMed.Repository/Handlers/MedicalHistories/UpdateMedicalHistoryCommandHandler.cs new file mode 100644 index 0000000..88a4982 --- /dev/null +++ b/DocuMed.Repository/Handlers/MedicalHistories/UpdateMedicalHistoryCommandHandler.cs @@ -0,0 +1,47 @@ +namespace DocuMed.Repository.Handlers.MedicalHistories; + +public class UpdateMedicalHistoryCommandHandler(IRepositoryWrapper repositoryWrapper, ICurrentUserService currentUserService) : IRequestHandler +{ + public async Task Handle(UpdateMedicalHistoryCommand template, CancellationToken cancellationToken) + { + if (!Guid.TryParse(currentUserService.UserId, out Guid userId)) + throw new AppException("دسترسی غیرمجاز", ApiResultStatusCode.UnAuthorized); + + var ent = await repositoryWrapper.SetRepository().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().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().Update(dbAnswer); + await repositoryWrapper.SaveChangesAsync(cancellationToken); + } + } + + repositoryWrapper.SetRepository().Update(newEnt); + await repositoryWrapper.SaveChangesAsync(cancellationToken); + + return newEnt.Id; + } +} \ No newline at end of file