update question
This commit is contained in:
parent
9555497d4e
commit
7cbff22d8a
3 changed files with 45 additions and 0 deletions
|
|
@ -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)
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
namespace SurveyBackend.DTOs.Question;
|
||||||
|
|
||||||
|
public class QuestionUpdateDto : QuestionCreateDto
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue