From 7eb0b664b0b40bb3862dfe8d7c448f3e6010d4be Mon Sep 17 00:00:00 2001 From: Mahmoud Nasr <239.nasr@gmail.com> Date: Sun, 29 Jun 2025 22:40:11 +0300 Subject: [PATCH 1/2] Refactor QAService and update QAController access Removed null/empty check for questions in QAService, directly mapping to QuestionViewDto. Added [AllowAnonymous] attribute to GetQuestionsForPost in QAController to allow unauthenticated access to the endpoint. --- Dentizone.Application/Services/QAService.cs | 5 +---- Dentizone.Presentaion/Controllers/QAController.cs | 1 + 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/Dentizone.Application/Services/QAService.cs b/Dentizone.Application/Services/QAService.cs index 7a06d7e..f406486 100644 --- a/Dentizone.Application/Services/QAService.cs +++ b/Dentizone.Application/Services/QAService.cs @@ -102,10 +102,7 @@ public async Task> GetQuestionsForPostAsync(string var questions = await questionRepository.FindAllBy( q => q.PostId == postId && !q.IsDeleted, includes); - if (questions == null || !questions.Any()) - { - return []; - } + return mapper.Map>(questions); } diff --git a/Dentizone.Presentaion/Controllers/QAController.cs b/Dentizone.Presentaion/Controllers/QAController.cs index c48a7fa..d156793 100644 --- a/Dentizone.Presentaion/Controllers/QAController.cs +++ b/Dentizone.Presentaion/Controllers/QAController.cs @@ -21,6 +21,7 @@ public async Task AskQuestion([FromBody] CreateQuestionDto dto) } [HttpGet("questions/{postId}")] + [AllowAnonymous] public async Task GetQuestionsForPost(string postId) { var questions = await qaService.GetQuestionsForPostAsync(postId); From 73165b016a2e901a69412413c2ca59781ab7a2a1 Mon Sep 17 00:00:00 2001 From: Mahmoud Nasr <239.nasr@gmail.com> Date: Sun, 29 Jun 2025 22:40:44 +0300 Subject: [PATCH 2/2] Add null check for answer retrieval in UpdateAnswerAsync Implemented a null check when fetching an answer by ID. If no answer is found, a NotFoundException is thrown, improving error handling and preventing potential null reference errors. --- Dentizone.Application/Services/QAService.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Dentizone.Application/Services/QAService.cs b/Dentizone.Application/Services/QAService.cs index f406486..fcb1db1 100644 --- a/Dentizone.Application/Services/QAService.cs +++ b/Dentizone.Application/Services/QAService.cs @@ -109,7 +109,8 @@ public async Task> GetQuestionsForPostAsync(string public async Task UpdateAnswerAsync(string answerId, UpdateAnswerDto dto) { - var answer = await answerRepository.GetByIdAsync(answerId); + var answer = await answerRepository.GetByIdAsync(answerId) ?? + throw new NotFoundException("No Answer with this ID"); answer.Text = dto.Text; await answerRepository.UpdateAsync(answer); }