From cc8aa32447d2d52a76fc9124e7040a3c059c5956 Mon Sep 17 00:00:00 2001 From: "Amir.H Khademi" Date: Tue, 24 Oct 2023 12:45:25 +0330 Subject: [PATCH] complete editing templates --- .../MedicalHistoryTemplateController.cs | 46 ++--- DocuMed.Api/DocuMed.Api.csproj | 2 + DocuMed.Core/DocuMed.Core.csproj | 2 +- .../IMedicalHistoryTemplateService.cs | 7 + .../MedicalHistoryTemplateService.cs | 61 +++++++ .../LargDtos/MedicalHistoryTemplateLDto.cs | 1 + .../MedicalHistoryTemplate.cs | 1 + DocuMed.Domain/Mappers/CityMapper.g.cs | 4 +- .../Mappers/MedicalHistoryTemplateMapper.g.cs | 167 ++++++++++++------ .../MedicalHistoryTemplateActionPage.razor | 25 ++- .../MedicalHistoryTemplateActionPage.razor.cs | 88 ++++++++- .../MedicalHistoryTemplateActionStep1.razor | 7 +- DocuMed.Repository/DocuMed.Repository.csproj | 9 + .../Repositories/Base/BaseRepository.cs | 35 +++- .../Repositories/Base/RepositoryWrapper.cs | 27 +-- .../IMedicalHistoryTemplateRepository.cs | 7 + .../MedicalHistoryTemplateRepository.cs | 32 ++++ 17 files changed, 401 insertions(+), 120 deletions(-) create mode 100644 DocuMed.Core/EntityServices/Abstracts/IMedicalHistoryTemplateService.cs create mode 100644 DocuMed.Core/EntityServices/MedicalHistoryTemplateService.cs create mode 100644 DocuMed.Repository/Repositories/Entities/Abstracts/IMedicalHistoryTemplateRepository.cs create mode 100644 DocuMed.Repository/Repositories/Entities/MedicalHistoryTemplateRepository.cs diff --git a/DocuMed.Api/Controllers/MedicalHistoryTemplateController.cs b/DocuMed.Api/Controllers/MedicalHistoryTemplateController.cs index a075b5e..d1b56e3 100644 --- a/DocuMed.Api/Controllers/MedicalHistoryTemplateController.cs +++ b/DocuMed.Api/Controllers/MedicalHistoryTemplateController.cs @@ -1,5 +1,4 @@ -using DocuMed.Api.Services; -using DocuMed.Domain.Dtos.LargDtos; +using Microsoft.AspNetCore.Mvc.RazorPages; namespace DocuMed.Api.Controllers; @@ -30,49 +29,28 @@ public class MedicalHistoryTemplateController : ICarterModule // GET:Get All Entity - public virtual async Task GetAllAsync([FromQuery] int page, IRepositoryWrapper repositoryWrapper,ICurrentUserService currentUserService, CancellationToken cancellationToken) + public virtual async Task GetAllAsync([FromQuery] int page, IMedicalHistoryTemplateRepository repository, CancellationToken cancellationToken) { - if (!Guid.TryParse(currentUserService.UserId, out Guid userId)) - throw new AppException("توکن غیرمجاز", ApiResultStatusCode.UnAuthorized); - var list = await repositoryWrapper.SetRepository().TableNoTracking - .Where(t=>t.ApplicationUserId== userId) - .OrderByDescending(t=>t.CreatedAt) - .Skip(page*15) - .Take(15) - .Select(MedicalHistoryTemplateMapper.ProjectToSDto) - .ToListAsync(cancellationToken); - return TypedResults.Ok(list); + return TypedResults.Ok(await repository.GetMedicalHistoryTemplatesAsync(page,cancellationToken)); } // GET:Get An Entity By Id - public async Task GetAsync(Guid id, IRepositoryWrapper repositoryWrapper, - CancellationToken cancellationToken) - => TypedResults.Ok(await repositoryWrapper.SetRepository() - .GetByIdAsync(cancellationToken, id)); + public async Task GetAsync(Guid id, IMedicalHistoryTemplateRepository repository, CancellationToken cancellationToken) + { + + return TypedResults.Ok(await repository.GetMedicalHistoryTemplateAsync(id, cancellationToken)); + } // POST:Add New Entity - public virtual async Task Post([FromBody] MedicalHistoryTemplateLDto dto, IRepositoryWrapper repositoryWrapper,ICurrentUserService currentUserService, CancellationToken cancellationToken) + public virtual async Task Post([FromBody] MedicalHistoryTemplateLDto dto, IMedicalHistoryTemplateService service, ICurrentUserService currentUserService, CancellationToken cancellationToken) { - if (!Guid.TryParse(currentUserService.UserId, out Guid userId)) - throw new AppException("توکن غیرمجاز", ApiResultStatusCode.UnAuthorized); - var ent = MedicalHistoryTemplate.Create(dto.ChiefComplaint,dto.SectionId, userId); - foreach (var question in dto.Questions) - ent.AddQuestion(question.Question, question.Part, question.QuestionType); - repositoryWrapper.SetRepository().Add(ent); - await repositoryWrapper.SaveChangesAsync(cancellationToken); - return TypedResults.Ok(); + return TypedResults.Ok(await service.AddAsync(dto,cancellationToken)); } // PUT:Update Entity - public virtual async Task Put([FromBody] MedicalHistoryTemplateLDto dto, IRepositoryWrapper repositoryWrapper,ICurrentUserService currentUserService, CancellationToken cancellationToken) + public virtual async Task Put([FromBody] MedicalHistoryTemplateLDto dto, IMedicalHistoryTemplateService service,ICurrentUserService currentUserService, CancellationToken cancellationToken) { - if (!Guid.TryParse(currentUserService.UserId, out Guid userId)) - throw new AppException("توکن غیرمجاز", ApiResultStatusCode.UnAuthorized); - var ent = MedicalHistoryTemplate.Create(dto.ChiefComplaint, dto.SectionId, userId); - ent.Id = dto.Id; - repositoryWrapper.SetRepository().Update(ent); - await repositoryWrapper.SaveChangesAsync(cancellationToken); - return TypedResults.Ok(); + return TypedResults.Ok(await service.EditAsync(dto, cancellationToken)); } // DELETE:Delete Entity diff --git a/DocuMed.Api/DocuMed.Api.csproj b/DocuMed.Api/DocuMed.Api.csproj index 3f7fe52..b4994f1 100644 --- a/DocuMed.Api/DocuMed.Api.csproj +++ b/DocuMed.Api/DocuMed.Api.csproj @@ -71,6 +71,7 @@ + @@ -85,6 +86,7 @@ + diff --git a/DocuMed.Core/DocuMed.Core.csproj b/DocuMed.Core/DocuMed.Core.csproj index 4ceaf82..96e5fcb 100644 --- a/DocuMed.Core/DocuMed.Core.csproj +++ b/DocuMed.Core/DocuMed.Core.csproj @@ -22,7 +22,6 @@ - @@ -37,6 +36,7 @@ + diff --git a/DocuMed.Core/EntityServices/Abstracts/IMedicalHistoryTemplateService.cs b/DocuMed.Core/EntityServices/Abstracts/IMedicalHistoryTemplateService.cs new file mode 100644 index 0000000..42a455a --- /dev/null +++ b/DocuMed.Core/EntityServices/Abstracts/IMedicalHistoryTemplateService.cs @@ -0,0 +1,7 @@ +namespace DocuMed.Core.EntityServices.Abstracts; + +public interface IMedicalHistoryTemplateService:IScopedDependency +{ + public Task EditAsync(MedicalHistoryTemplateLDto template, CancellationToken cancellationToken); + public Task AddAsync(MedicalHistoryTemplateLDto template, CancellationToken cancellationToken); +} \ No newline at end of file diff --git a/DocuMed.Core/EntityServices/MedicalHistoryTemplateService.cs b/DocuMed.Core/EntityServices/MedicalHistoryTemplateService.cs new file mode 100644 index 0000000..2a6cf6a --- /dev/null +++ b/DocuMed.Core/EntityServices/MedicalHistoryTemplateService.cs @@ -0,0 +1,61 @@ +using DocuMed.Domain.Entities.MedicalHistoryTemplate; +using DocuMed.Repository.Repositories.Entities; +using DocuMed.Repository.Repositories.Entities.Abstracts; +using System.Threading; + +namespace DocuMed.Core.EntityServices; + +public class MedicalHistoryTemplateService : IMedicalHistoryTemplateService +{ + private readonly IRepositoryWrapper _repositoryWrapper; + private readonly ICurrentUserService _currentUserService; + private readonly IMedicalHistoryTemplateRepository _medicalHistoryTemplateRepository; + + public MedicalHistoryTemplateService(IRepositoryWrapper repositoryWrapper,ICurrentUserService currentUserService,IMedicalHistoryTemplateRepository medicalHistoryTemplateRepository) + { + _repositoryWrapper = repositoryWrapper; + _currentUserService = currentUserService; + _medicalHistoryTemplateRepository = medicalHistoryTemplateRepository; + } + public async Task EditAsync(MedicalHistoryTemplateLDto 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 = MedicalHistoryTemplate.Create(template.ChiefComplaint, template.SectionId, userId); + ent.Id = template.Id; + + var questions = await _repositoryWrapper.SetRepository() + .TableNoTracking + .Where(q => q.MedicalHistoryTemplateId == ent.Id) + .ToListAsync(cancellationToken); + foreach (var question in questions) + { + if (template.Questions.FirstOrDefault(q => q.Id == question.Id) == null) + { + _repositoryWrapper.SetRepository() + .Delete(question); + await _repositoryWrapper.SaveChangesAsync(cancellationToken); + } + } + + foreach (var question in template.Questions.Where(q=>q.Id==Guid.Empty)) + ent.AddQuestion(question.Question, question.Part, question.QuestionType); + _medicalHistoryTemplateRepository.Update(ent); + await _repositoryWrapper.SaveChangesAsync(cancellationToken); + return true; + } + + public async Task AddAsync(MedicalHistoryTemplateLDto template,CancellationToken cancellationToken) + { + if (!Guid.TryParse(_currentUserService.UserId, out Guid userId)) + throw new AppException("توکن غیرمجاز", ApiResultStatusCode.UnAuthorized); + var ent = MedicalHistoryTemplate.Create(template.ChiefComplaint, template.SectionId, userId); + foreach (var question in template.Questions) + ent.AddQuestion(question.Question, question.Part, question.QuestionType); + _medicalHistoryTemplateRepository.Add(ent); + await _repositoryWrapper.SaveChangesAsync(cancellationToken); + return true; + } +} \ No newline at end of file diff --git a/DocuMed.Domain/Dtos/LargDtos/MedicalHistoryTemplateLDto.cs b/DocuMed.Domain/Dtos/LargDtos/MedicalHistoryTemplateLDto.cs index 2e313f7..77582c7 100644 --- a/DocuMed.Domain/Dtos/LargDtos/MedicalHistoryTemplateLDto.cs +++ b/DocuMed.Domain/Dtos/LargDtos/MedicalHistoryTemplateLDto.cs @@ -5,6 +5,7 @@ public class MedicalHistoryTemplateLDto : BaseDto Questions { get; set; } = new(); } \ No newline at end of file diff --git a/DocuMed.Domain/Entities/MedicalHistoryTemplate/MedicalHistoryTemplate.cs b/DocuMed.Domain/Entities/MedicalHistoryTemplate/MedicalHistoryTemplate.cs index 4fc31c8..c295e40 100644 --- a/DocuMed.Domain/Entities/MedicalHistoryTemplate/MedicalHistoryTemplate.cs +++ b/DocuMed.Domain/Entities/MedicalHistoryTemplate/MedicalHistoryTemplate.cs @@ -21,6 +21,7 @@ public partial class MedicalHistoryTemplate : ApiEntity public string ChiefComplaint { get; internal set; } = string.Empty; public Guid SectionId { get; set; } + public Section? Section { get; set; } public Guid ApplicationUserId { get; internal set; } public ApplicationUser? ApplicationUser { get; set; } public List Questions { get; internal set; } = new(); diff --git a/DocuMed.Domain/Mappers/CityMapper.g.cs b/DocuMed.Domain/Mappers/CityMapper.g.cs index 2f705c9..9003355 100644 --- a/DocuMed.Domain/Mappers/CityMapper.g.cs +++ b/DocuMed.Domain/Mappers/CityMapper.g.cs @@ -31,7 +31,7 @@ namespace DocuMed.Domain.Mappers return result; } - public static Expression> ProjectLDtoToCity => p4 => new City() + public static Expression> ProjectToCity => p4 => new City() { Name = p4.Name, Id = p4.Id @@ -85,7 +85,7 @@ namespace DocuMed.Domain.Mappers return result; } - public static Expression> ProjectToCity => p15 => new City() + public static Expression> ProjectLDtoToCity => p15 => new City() { Name = p15.Name, Universities = p15.Universities.Select(p16 => new University() diff --git a/DocuMed.Domain/Mappers/MedicalHistoryTemplateMapper.g.cs b/DocuMed.Domain/Mappers/MedicalHistoryTemplateMapper.g.cs index ed7044f..4a41cd1 100644 --- a/DocuMed.Domain/Mappers/MedicalHistoryTemplateMapper.g.cs +++ b/DocuMed.Domain/Mappers/MedicalHistoryTemplateMapper.g.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Linq.Expressions; using DocuMed.Domain.Dtos.LargDtos; using DocuMed.Domain.Dtos.SmallDtos; +using DocuMed.Domain.Entities.City; using DocuMed.Domain.Entities.MedicalHistoryTemplate; namespace DocuMed.Domain.Mappers @@ -80,6 +81,13 @@ namespace DocuMed.Domain.Mappers { ChiefComplaint = p9.ChiefComplaint, SectionId = p9.SectionId, + Section = p9.Section == null ? null : new Section() + { + Name = p9.Section.Name, + Detail = p9.Section.Detail, + UniversityId = p9.Section.UniversityId, + Id = p9.Section.Id + }, ApplicationUserId = p9.ApplicationUserId, Questions = funcMain1(p9.Questions), Id = p9.Id @@ -95,68 +103,91 @@ namespace DocuMed.Domain.Mappers result.ChiefComplaint = p11.ChiefComplaint; result.SectionId = p11.SectionId; + result.Section = funcMain2(p11.Section, result.Section); result.ApplicationUserId = p11.ApplicationUserId; - result.Questions = funcMain2(p11.Questions, result.Questions); + result.Questions = funcMain3(p11.Questions, result.Questions); result.Id = p11.Id; return result; } - public static Expression> ProjectLDtoToMedicalHistoryTemplate => p15 => new MedicalHistoryTemplate() + public static Expression> ProjectLDtoToMedicalHistoryTemplate => p17 => new MedicalHistoryTemplate() { - ChiefComplaint = p15.ChiefComplaint, - SectionId = p15.SectionId, - ApplicationUserId = p15.ApplicationUserId, - Questions = p15.Questions.Select(p16 => new MedicalHistoryQuestion() + ChiefComplaint = p17.ChiefComplaint, + SectionId = p17.SectionId, + Section = p17.Section == null ? null : new Section() { - Question = p16.Question, - Part = p16.Part, - QuestionType = p16.QuestionType, - MedicalHistoryTemplateId = p16.MedicalHistoryTemplateId, - Id = p16.Id + Name = p17.Section.Name, + Detail = p17.Section.Detail, + UniversityId = p17.Section.UniversityId, + Id = p17.Section.Id + }, + ApplicationUserId = p17.ApplicationUserId, + Questions = p17.Questions.Select(p18 => new MedicalHistoryQuestion() + { + Question = p18.Question, + Part = p18.Part, + QuestionType = p18.QuestionType, + MedicalHistoryTemplateId = p18.MedicalHistoryTemplateId, + Id = p18.Id }).ToList(), - Id = p15.Id + Id = p17.Id }; - public static MedicalHistoryTemplateLDto AdaptToLDto(this MedicalHistoryTemplate p17) + public static MedicalHistoryTemplateLDto AdaptToLDto(this MedicalHistoryTemplate p19) { - return p17 == null ? null : new MedicalHistoryTemplateLDto() + return p19 == null ? null : new MedicalHistoryTemplateLDto() { - ChiefComplaint = p17.ChiefComplaint, - SectionId = p17.SectionId, - ApplicationUserId = p17.ApplicationUserId, - Questions = funcMain3(p17.Questions), - Id = p17.Id + ChiefComplaint = p19.ChiefComplaint, + SectionId = p19.SectionId, + Section = p19.Section == null ? null : new SectionSDto() + { + Name = p19.Section.Name, + Detail = p19.Section.Detail, + UniversityId = p19.Section.UniversityId, + Id = p19.Section.Id + }, + ApplicationUserId = p19.ApplicationUserId, + Questions = funcMain4(p19.Questions), + Id = p19.Id }; } - public static MedicalHistoryTemplateLDto AdaptTo(this MedicalHistoryTemplate p19, MedicalHistoryTemplateLDto p20) + public static MedicalHistoryTemplateLDto AdaptTo(this MedicalHistoryTemplate p21, MedicalHistoryTemplateLDto p22) { - if (p19 == null) + if (p21 == null) { return null; } - MedicalHistoryTemplateLDto result = p20 ?? new MedicalHistoryTemplateLDto(); + MedicalHistoryTemplateLDto result = p22 ?? new MedicalHistoryTemplateLDto(); - result.ChiefComplaint = p19.ChiefComplaint; - result.SectionId = p19.SectionId; - result.ApplicationUserId = p19.ApplicationUserId; - result.Questions = funcMain4(p19.Questions, result.Questions); - result.Id = p19.Id; + result.ChiefComplaint = p21.ChiefComplaint; + result.SectionId = p21.SectionId; + result.Section = funcMain5(p21.Section, result.Section); + result.ApplicationUserId = p21.ApplicationUserId; + result.Questions = funcMain6(p21.Questions, result.Questions); + result.Id = p21.Id; return result; } - public static Expression> ProjectToLDto => p23 => new MedicalHistoryTemplateLDto() + public static Expression> ProjectToLDto => p27 => new MedicalHistoryTemplateLDto() { - ChiefComplaint = p23.ChiefComplaint, - SectionId = p23.SectionId, - ApplicationUserId = p23.ApplicationUserId, - Questions = p23.Questions.Select(p24 => new MedicalHistoryQuestionSDto() + ChiefComplaint = p27.ChiefComplaint, + SectionId = p27.SectionId, + Section = p27.Section == null ? null : new SectionSDto() { - Question = p24.Question, - Part = p24.Part, - QuestionType = p24.QuestionType, - MedicalHistoryTemplateId = p24.MedicalHistoryTemplateId, - Id = p24.Id + Name = p27.Section.Name, + Detail = p27.Section.Detail, + UniversityId = p27.Section.UniversityId, + Id = p27.Section.Id + }, + ApplicationUserId = p27.ApplicationUserId, + Questions = p27.Questions.Select(p28 => new MedicalHistoryQuestionSDto() + { + Question = p28.Question, + Part = p28.Part, + QuestionType = p28.QuestionType, + MedicalHistoryTemplateId = p28.MedicalHistoryTemplateId, + Id = p28.Id }).ToList(), - Id = p23.Id + Id = p27.Id }; private static List funcMain1(List p10) @@ -187,20 +218,36 @@ namespace DocuMed.Domain.Mappers } - private static List funcMain2(List p13, List p14) + private static Section funcMain2(SectionSDto p13, Section p14) { if (p13 == null) { return null; } - List result = new List(p13.Count); + Section result = p14 ?? new Section(); + + result.Name = p13.Name; + result.Detail = p13.Detail; + result.UniversityId = p13.UniversityId; + result.Id = p13.Id; + return result; + + } + + private static List funcMain3(List p15, List p16) + { + if (p15 == null) + { + return null; + } + List result = new List(p15.Count); int i = 0; - int len = p13.Count; + int len = p15.Count; while (i < len) { - MedicalHistoryQuestionSDto item = p13[i]; + MedicalHistoryQuestionSDto item = p15[i]; result.Add(item == null ? null : new MedicalHistoryQuestion() { Question = item.Question, @@ -215,20 +262,20 @@ namespace DocuMed.Domain.Mappers } - private static List funcMain3(List p18) + private static List funcMain4(List p20) { - if (p18 == null) + if (p20 == null) { return null; } - List result = new List(p18.Count); + List result = new List(p20.Count); int i = 0; - int len = p18.Count; + int len = p20.Count; while (i < len) { - MedicalHistoryQuestion item = p18[i]; + MedicalHistoryQuestion item = p20[i]; result.Add(item == null ? null : new MedicalHistoryQuestionSDto() { Question = item.Question, @@ -243,20 +290,36 @@ namespace DocuMed.Domain.Mappers } - private static List funcMain4(List p21, List p22) + private static SectionSDto funcMain5(Section p23, SectionSDto p24) { - if (p21 == null) + if (p23 == null) { return null; } - List result = new List(p21.Count); + SectionSDto result = p24 ?? new SectionSDto(); + + result.Name = p23.Name; + result.Detail = p23.Detail; + result.UniversityId = p23.UniversityId; + result.Id = p23.Id; + return result; + + } + + private static List funcMain6(List p25, List p26) + { + if (p25 == null) + { + return null; + } + List result = new List(p25.Count); int i = 0; - int len = p21.Count; + int len = p25.Count; while (i < len) { - MedicalHistoryQuestion item = p21[i]; + MedicalHistoryQuestion item = p25[i]; result.Add(item == null ? null : new MedicalHistoryQuestionSDto() { Question = item.Question, diff --git a/DocuMed.PWA/Pages/MedicalHistoryTemplateActionPage.razor b/DocuMed.PWA/Pages/MedicalHistoryTemplateActionPage.razor index f185ac4..555f187 100644 --- a/DocuMed.PWA/Pages/MedicalHistoryTemplateActionPage.razor +++ b/DocuMed.PWA/Pages/MedicalHistoryTemplateActionPage.razor @@ -1,5 +1,5 @@ @page "/MedicalHistoryTemplateActionPage" -@page "/MedicalHistoryTemplateActionPage/{TemplateId:string}" +@page "/MedicalHistoryTemplateActionPage/{TemplateId}" @inject NavigationManager NavigationManager @inject IRestWrapper RestWrapper @inject IUserUtility UserUtility @@ -15,6 +15,7 @@
+
@@ -55,7 +56,7 @@

منتظر بمانید

- + } else { @@ -63,7 +64,18 @@ @if (@ViewModel.CurrentStep == 4) { - تـــکمیل + @if (@ViewModel.IsEditing) + { + تـــکمیل + } + else + { + ویرایش + } } else { @@ -83,12 +95,15 @@ @code { + [Parameter] - public string TemplateId { get; set; } + public string TemplateId { get; set; } = string.Empty; public MedicalHistoryTemplateActionPageViewModel ViewModel { get; set; } protected override async Task OnInitializedAsync() { - ViewModel = new MedicalHistoryTemplateActionPageViewModel(NavigationManager,Snackbar,RestWrapper,UserUtility); + ViewModel = Guid.TryParse(TemplateId, out Guid templateId) ? + new MedicalHistoryTemplateActionPageViewModel(NavigationManager, Snackbar, RestWrapper, UserUtility, templateId) + : new MedicalHistoryTemplateActionPageViewModel(NavigationManager, Snackbar, RestWrapper, UserUtility); await ViewModel.InitializeAsync(); await base.OnInitializedAsync(); } diff --git a/DocuMed.PWA/Pages/MedicalHistoryTemplateActionPage.razor.cs b/DocuMed.PWA/Pages/MedicalHistoryTemplateActionPage.razor.cs index 3f01866..526d5f3 100644 --- a/DocuMed.PWA/Pages/MedicalHistoryTemplateActionPage.razor.cs +++ b/DocuMed.PWA/Pages/MedicalHistoryTemplateActionPage.razor.cs @@ -22,7 +22,7 @@ public class MedicalHistoryTemplateActionPageViewModel : BaseViewModel VsQuestions { get; set; } = new(); public SectionSDto? SelectedSelection { get; set; } - + public MedicalHistoryTemplateActionPageViewModel( @@ -52,20 +52,60 @@ public class MedicalHistoryTemplateActionPageViewModel : BaseViewModel(Address.MedicalHistoryTemplateController) + .ReadOne(_templateId, token); + PageDto.ApplicationUserId = dto.ApplicationUserId; + PageDto.ChiefComplaint = dto.ChiefComplaint; + PageDto.SectionId = dto.SectionId; + PageDto.Id = dto.Id; + SelectedSelection = dto.Section; + PiQuestions.AddRange(dto.Questions.Where(q => q.Part == MedicalHistoryPart.PresentIllness)); + PdhQuestions.AddRange(dto.Questions.Where(q => q.Part == MedicalHistoryPart.PastDiseasesHistory)); + PshQuestions.AddRange(dto.Questions.Where(q => q.Part == MedicalHistoryPart.PastSurgeryHistory)); + FhQuestions.AddRange(dto.Questions.Where(q => q.Part == MedicalHistoryPart.FamilyHistory)); + DhQuestions.AddRange(dto.Questions.Where(q => q.Part == MedicalHistoryPart.DrugHistory)); + AhQuestions.AddRange(dto.Questions.Where(q => q.Part == MedicalHistoryPart.AddictionHistory)); + VsQuestions.AddRange(dto.Questions.Where(q => q.Part == MedicalHistoryPart.VitalSign)); + } + catch (ApiException ex) + { + var exe = await ex.GetContentAsAsync(); + _snackbar.Add(exe != null ? exe.Message : ex.Content, Severity.Error); + } + catch (Exception e) + { + _snackbar.Add(e.Message, Severity.Error); + } + finally + { + + IsProcessing = false; + } + } + await base.InitializeAsync(); } public async Task CompleteStepClicked() { - Carousel?.MoveTo(++CurrentStep); + ++CurrentStep; StepCounter = $"{CurrentStep + 1} / 5"; if (CurrentStep == 5) { + if (IsEditing) + await EditTemplateAsync(); + else + await SubmitTemplateAsync(); MedicalHistorySubmitted = true; - await SubmitTemplateAsync(); } + Carousel?.MoveTo(CurrentStep); } public async Task SubmitTemplateAsync() @@ -105,6 +145,44 @@ public class MedicalHistoryTemplateActionPageViewModel : BaseViewModel(Address.MedicalHistoryTemplateController) + .Update(PageDto, token); + + } + catch (ApiException ex) + { + var exe = await ex.GetContentAsAsync(); + _snackbar.Add(exe != null ? exe.Message : ex.Content, Severity.Error); + } + catch (Exception e) + { + _snackbar.Add(e.Message, Severity.Error); + } + finally + { + + IsProcessing = false; + } + } public void RollBackStepClicked() { Carousel?.MoveTo(--CurrentStep); diff --git a/DocuMed.PWA/Pages/MedicalHistoryTemplateActionParts/MedicalHistoryTemplateActionStep1.razor b/DocuMed.PWA/Pages/MedicalHistoryTemplateActionParts/MedicalHistoryTemplateActionStep1.razor index 7871958..c2c9791 100644 --- a/DocuMed.PWA/Pages/MedicalHistoryTemplateActionParts/MedicalHistoryTemplateActionStep1.razor +++ b/DocuMed.PWA/Pages/MedicalHistoryTemplateActionParts/MedicalHistoryTemplateActionStep1.razor @@ -8,8 +8,8 @@ مورد ده

- - + @@ -31,7 +31,6 @@ @code { - [Parameter] public string ChiefComplaint { get; set; } = string.Empty; [Parameter] @@ -42,6 +41,8 @@ [Parameter] public EventCallback SelectedSectionChanged { get; set; } + private MedicalHistoryTemplateLDto _editingTemplate = new(); + public List Sections { get; private set; } = new List(); public async Task> SearchSection(string section) diff --git a/DocuMed.Repository/DocuMed.Repository.csproj b/DocuMed.Repository/DocuMed.Repository.csproj index dc858ae..a14577c 100644 --- a/DocuMed.Repository/DocuMed.Repository.csproj +++ b/DocuMed.Repository/DocuMed.Repository.csproj @@ -31,14 +31,23 @@ + + + + + + + + + diff --git a/DocuMed.Repository/Repositories/Base/BaseRepository.cs b/DocuMed.Repository/Repositories/Base/BaseRepository.cs index 6af587c..d3f8bd7 100644 --- a/DocuMed.Repository/Repositories/Base/BaseRepository.cs +++ b/DocuMed.Repository/Repositories/Base/BaseRepository.cs @@ -1,10 +1,14 @@ -namespace DocuMed.Repository.Repositories.Base +using DocuMed.Repository.Abstracts; + +namespace DocuMed.Repository.Repositories.Base { public class BaseRepository : Repository, IBaseRepository where T : class, IApiEntity { - public BaseRepository(ApplicationContext dbContext) : base(dbContext) - { + protected readonly ICurrentUserService CurrentUserService; + public BaseRepository(ApplicationContext dbContext,ICurrentUserService currentUserService) : base(dbContext) + { + CurrentUserService = currentUserService; } public virtual async ValueTask GetByIdAsync(CancellationToken cancellationToken, params object[] ids) @@ -55,13 +59,32 @@ public virtual void Delete(T entity) { AssertExtensions.NotNull(entity, nameof(entity)); - Entities.Remove(entity); + + Entities.Entry(entity).Property(e => e.RemovedAt) + .CurrentValue = DateTime.Now; + Entities.Entry(entity).Property(e => e.IsRemoved) + .CurrentValue = true; + if (CurrentUserService.UserName != null) + Entities.Entry(entity).Property(e => e.RemovedBy) + .CurrentValue = CurrentUserService.UserName; + Entities.Update(entity); } public virtual void DeleteRange(IEnumerable entities) { - AssertExtensions.NotNull(entities, nameof(entities)); - Entities.RemoveRange(entities); + var apiEntities = entities.ToList(); + AssertExtensions.NotNull(apiEntities, nameof(entities)); + foreach (var entity in apiEntities) + { + Entities.Entry(entity).Property(e => e.RemovedAt) + .CurrentValue = DateTime.Now; + Entities.Entry(entity).Property(e => e.IsRemoved) + .CurrentValue = true; + if (CurrentUserService.UserName != null) + Entities.Entry(entity).Property(e => e.RemovedBy) + .CurrentValue = CurrentUserService.UserName; + Entities.Update(entity); + } } #endregion diff --git a/DocuMed.Repository/Repositories/Base/RepositoryWrapper.cs b/DocuMed.Repository/Repositories/Base/RepositoryWrapper.cs index 7f0909b..dfcbbe4 100644 --- a/DocuMed.Repository/Repositories/Base/RepositoryWrapper.cs +++ b/DocuMed.Repository/Repositories/Base/RepositoryWrapper.cs @@ -2,13 +2,15 @@ public class RepositoryWrapper : IRepositoryWrapper { private readonly ApplicationContext _context; + private readonly ICurrentUserService _currentUserService; private IDbContextTransaction? _currentTransaction; - public RepositoryWrapper(ApplicationContext context) + public RepositoryWrapper(ApplicationContext context,ICurrentUserService currentUserService) { _context = context; + _currentUserService = currentUserService; } - public IBaseRepository SetRepository() where T : ApiEntity => new BaseRepository(_context); + public IBaseRepository SetRepository() where T : ApiEntity => new BaseRepository(_context, _currentUserService); public async Task RollBackAsync(CancellationToken cancellationToken) @@ -42,20 +44,21 @@ public class RepositoryWrapper : IRepositoryWrapper { entity.Property(e => e.CreatedAt) .CurrentValue = DateTime.Now; + if (_currentUserService.UserName != null) + entity.Property(e => e.CreatedBy) + .CurrentValue = _currentUserService.UserName; } if (entity.State == EntityState.Modified) { - entity.Property(e => e.ModifiedAt) - .CurrentValue = DateTime.Now; - } - - if (entity.State == EntityState.Deleted) - { - entity.Property(e => e.RemovedAt) - .CurrentValue = DateTime.Now; - entity.Property(e => e.IsRemoved) - .CurrentValue = true; + if (!entity.Property(e => e.IsRemoved).CurrentValue) + { + entity.Property(e => e.ModifiedAt) + .CurrentValue = DateTime.Now; + if (_currentUserService.UserName != null) + entity.Property(e => e.ModifiedBy) + .CurrentValue = _currentUserService.UserName; + } } } } diff --git a/DocuMed.Repository/Repositories/Entities/Abstracts/IMedicalHistoryTemplateRepository.cs b/DocuMed.Repository/Repositories/Entities/Abstracts/IMedicalHistoryTemplateRepository.cs new file mode 100644 index 0000000..1797bb2 --- /dev/null +++ b/DocuMed.Repository/Repositories/Entities/Abstracts/IMedicalHistoryTemplateRepository.cs @@ -0,0 +1,7 @@ +namespace DocuMed.Repository.Repositories.Entities.Abstracts; + +public interface IMedicalHistoryTemplateRepository : IBaseRepository,IScopedDependency +{ + public Task> GetMedicalHistoryTemplatesAsync(int page = 0, CancellationToken cancellationToken = default); + public Task GetMedicalHistoryTemplateAsync(Guid id, CancellationToken cancellationToken = default); +} \ No newline at end of file diff --git a/DocuMed.Repository/Repositories/Entities/MedicalHistoryTemplateRepository.cs b/DocuMed.Repository/Repositories/Entities/MedicalHistoryTemplateRepository.cs new file mode 100644 index 0000000..722c2b2 --- /dev/null +++ b/DocuMed.Repository/Repositories/Entities/MedicalHistoryTemplateRepository.cs @@ -0,0 +1,32 @@ +namespace DocuMed.Repository.Repositories.Entities; + +public class MedicalHistoryTemplateRepository : BaseRepository, IMedicalHistoryTemplateRepository +{ + public MedicalHistoryTemplateRepository(ApplicationContext dbContext,ICurrentUserService currentUserService) : base(dbContext,currentUserService) + { + } + + public async Task> GetMedicalHistoryTemplatesAsync(int page = 0, CancellationToken cancellationToken = default) + { + if (!Guid.TryParse(CurrentUserService.UserId, out Guid userId)) + throw new AppException("توکن غیرمجاز", ApiResultStatusCode.UnAuthorized); + var list = await TableNoTracking + .Where(t => t.ApplicationUserId == userId) + .OrderByDescending(t => t.CreatedAt) + .Skip(page * 15) + .Take(15) + .Select(MedicalHistoryTemplateMapper.ProjectToSDto) + .ToListAsync(cancellationToken); + return list; + } + + public async Task GetMedicalHistoryTemplateAsync(Guid id, CancellationToken cancellationToken = default) + { + var dto = await TableNoTracking.Where(t => t.Id == id) + .Select(MedicalHistoryTemplateMapper.ProjectToLDto) + .FirstOrDefaultAsync(cancellationToken); + if (dto == null) + throw new AppException("پیش نویس پیدا نشد", ApiResultStatusCode.NotFound); + return dto; + } +} \ No newline at end of file