- refactored SurveyController.cs - added ProducesResponseType to every endpoint in every controller - remade mappers
57 lines
No EOL
1.9 KiB
C#
57 lines
No EOL
1.9 KiB
C#
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();
|
|
}
|
|
} |