From 7cbff22d8a28311c7ea641bb3b44e0e75b23100c Mon Sep 17 00:00:00 2001 From: shept Date: Tue, 13 May 2025 19:19:43 +0500 Subject: [PATCH] update question --- .../Controllers/QuestionController.cs | 11 ++++++++ .../DTOs/Question/QuestionUpdateDto.cs | 6 ++++ .../Mappers/QuestionMapper.cs | 28 +++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 SurveyBackend/SurveyBackend.API/DTOs/Question/QuestionUpdateDto.cs diff --git a/SurveyBackend/SurveyBackend.API/Controllers/QuestionController.cs b/SurveyBackend/SurveyBackend.API/Controllers/QuestionController.cs index fb72465..41f6ac8 100644 --- a/SurveyBackend/SurveyBackend.API/Controllers/QuestionController.cs +++ b/SurveyBackend/SurveyBackend.API/Controllers/QuestionController.cs @@ -56,6 +56,17 @@ public class QuestionController : ControllerBase return Ok(result); } + [Authorize] + [HttpPut("{id}")] + public async Task UpdateQuestion([FromBody] QuestionUpdateDto dto, [FromRoute] int id, + [FromRoute] int surveyId) + { + var question = QuestionMapper.QuestionUpdateToModel(dto, surveyId, id); + await _questionService.UpdateQuestionAsync(question); + var result = QuestionMapper.ModelToQuestionDto(question); + return Ok(result); + } + [Authorize] [HttpDelete("{id}")] public async Task DeleteQuestion([FromRoute] int id, [FromRoute] int surveyId) diff --git a/SurveyBackend/SurveyBackend.API/DTOs/Question/QuestionUpdateDto.cs b/SurveyBackend/SurveyBackend.API/DTOs/Question/QuestionUpdateDto.cs new file mode 100644 index 0000000..fcefc31 --- /dev/null +++ b/SurveyBackend/SurveyBackend.API/DTOs/Question/QuestionUpdateDto.cs @@ -0,0 +1,6 @@ +namespace SurveyBackend.DTOs.Question; + +public class QuestionUpdateDto : QuestionCreateDto +{ + +} \ No newline at end of file diff --git a/SurveyBackend/SurveyBackend.API/Mappers/QuestionMapper.cs b/SurveyBackend/SurveyBackend.API/Mappers/QuestionMapper.cs index bede586..044c052 100644 --- a/SurveyBackend/SurveyBackend.API/Mappers/QuestionMapper.cs +++ b/SurveyBackend/SurveyBackend.API/Mappers/QuestionMapper.cs @@ -60,6 +60,34 @@ public static class QuestionMapper }; } + public static QuestionBase QuestionUpdateToModel(QuestionCreateDto dto, int surveyId, int questionId) + { + return dto.QuestionType.ToLower() switch + { + "textquestion" => new TextQuestion + { + Id = questionId, + Title = dto.Title, + SurveyId = surveyId, + }, + "singleanswerquestion" => new SingleAnswerQuestion + { + Id = questionId, + Title = dto.Title, + SurveyId = surveyId, + AnswerVariants = [], + }, + "multipleanswerquestion" => new MultipleAnswerQuestion + { + Id = questionId, + Title = dto.Title, + SurveyId = surveyId, + AnswerVariants = [] + }, + _ => throw new BadRequestException("Unknown question type") + }; + } + private static List AnswerVariantsToDto(IEnumerable answerVariants) { return answerVariants.Select(av => new OutputAnswerVariantDto