life quality changes
- refactored SurveyController.cs - added ProducesResponseType to every endpoint in every controller - remade mappers
This commit is contained in:
parent
9e50b97bc9
commit
6692a91821
11 changed files with 146 additions and 93 deletions
57
SurveyBackend/SurveyBackend.API/Mappers/QuestionMapper.cs
Normal file
57
SurveyBackend/SurveyBackend.API/Mappers/QuestionMapper.cs
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
using SurveyBackend.DTOs.Question;
|
||||
using SurveyBackend.Services.Exceptions;
|
||||
using SurveyLib.Core.Models;
|
||||
using SurveyLib.Core.Models.QuestionVariants;
|
||||
|
||||
namespace SurveyBackend.Mappers;
|
||||
|
||||
public static class QuestionMapper
|
||||
{
|
||||
public static QuestionBase QuestionCreationToModel(CreateQuestionDto dto, int surveyId)
|
||||
{
|
||||
return dto.QuestionType.ToLower() switch
|
||||
{
|
||||
"textquestion" => new TextQuestion
|
||||
{
|
||||
Title = dto.Title,
|
||||
SurveyId = surveyId,
|
||||
},
|
||||
"singleanswerquestion" => new SingleAnswerQuestion
|
||||
{
|
||||
Title = dto.Title,
|
||||
SurveyId = surveyId,
|
||||
AnswerVariants = dto.AnswerVariants?.Select(v => new AnswerVariant { Text = v }).ToList() ?? [],
|
||||
},
|
||||
"multipleanswerquestion" => new MultipleAnswerQuestion
|
||||
{
|
||||
Title = dto.Title,
|
||||
SurveyId = surveyId,
|
||||
AnswerVariants = dto.AnswerVariants?.Select(v => new AnswerVariant { Text = v }).ToList() ?? []
|
||||
},
|
||||
_ => throw new BadRequestException("Unknown question type")
|
||||
};
|
||||
}
|
||||
|
||||
public static OutputQuestionDto ModelToQuestionDto(QuestionBase question)
|
||||
{
|
||||
var withAnswerVariants = question.GetType() != typeof(TextQuestion);
|
||||
return new OutputQuestionDto
|
||||
{
|
||||
Id = question.Id,
|
||||
SurveyId = question.SurveyId,
|
||||
Title = question.Title,
|
||||
QuestionType = question.Discriminator,
|
||||
AnswerVariants = withAnswerVariants ? AnswerVariantsToDto(question.AnswerVariants) : null,
|
||||
};
|
||||
}
|
||||
|
||||
private static List<OutputAnswerVariantDto> AnswerVariantsToDto(IEnumerable<AnswerVariant> answerVariants)
|
||||
{
|
||||
return answerVariants.Select(av => new OutputAnswerVariantDto
|
||||
{
|
||||
Id = av.Id,
|
||||
QuestionId = av.QuestionId,
|
||||
Text = av.Text,
|
||||
}).ToList();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue