survey-webapp/SurveyBackend/SurveyBackend.API/Mappers/QuestionDTOs/QuestionCreationMapper.cs

34 lines
No EOL
1.1 KiB
C#

using SurveyBackend.DTOs.Question;
using SurveyBackend.Services.Exceptions;
using SurveyLib.Core.Models;
using SurveyLib.Core.Models.QuestionVariants;
namespace SurveyBackend.Mappers.QuestionDTOs;
public static class QuestionCreationMapper
{
public static QuestionBase QuestionCreationToModel(CreateQuestionDTO dto)
{
return dto.QuestionType.ToLower() switch
{
"text" => new TextQuestion
{
Title = dto.Title,
SurveyId = dto.SurveyId,
},
"single" => new SingleAnswerQuestion
{
Title = dto.Title,
SurveyId = dto.SurveyId,
AnswerVariants = dto.AnswerVariants?.Select(v => new AnswerVariant { Text = v }).ToList() ?? [],
},
"multiple" => new MultipleAnswerQuestion
{
Title = dto.Title,
SurveyId = dto.SurveyId,
AnswerVariants = dto.AnswerVariants?.Select(v => new AnswerVariant { Text = v }).ToList() ?? []
},
_ => throw new BadRequestException("Unknown question type")
};
}
}