update question

This commit is contained in:
Вячеслав 2025-05-13 19:19:43 +05:00
parent 9555497d4e
commit 7cbff22d8a
3 changed files with 45 additions and 0 deletions

View file

@ -56,6 +56,17 @@ public class QuestionController : ControllerBase
return Ok(result); return Ok(result);
} }
[Authorize]
[HttpPut("{id}")]
public async Task<IActionResult> 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] [Authorize]
[HttpDelete("{id}")] [HttpDelete("{id}")]
public async Task<IActionResult> DeleteQuestion([FromRoute] int id, [FromRoute] int surveyId) public async Task<IActionResult> DeleteQuestion([FromRoute] int id, [FromRoute] int surveyId)

View file

@ -0,0 +1,6 @@
namespace SurveyBackend.DTOs.Question;
public class QuestionUpdateDto : QuestionCreateDto
{
}

View file

@ -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<OutputAnswerVariantDto> AnswerVariantsToDto(IEnumerable<AnswerVariant> answerVariants) private static List<OutputAnswerVariantDto> AnswerVariantsToDto(IEnumerable<AnswerVariant> answerVariants)
{ {
return answerVariants.Select(av => new OutputAnswerVariantDto return answerVariants.Select(av => new OutputAnswerVariantDto