added questions output (SHIT CODE)
This commit is contained in:
parent
7d76a9f67c
commit
ddfb5eff54
11 changed files with 93 additions and 9 deletions
|
|
@ -7,22 +7,22 @@ namespace SurveyBackend.Mappers.QuestionDTOs;
|
|||
|
||||
public static class QuestionCreationMapper
|
||||
{
|
||||
public static QuestionBase QuestionCreationToModel(CreateQuestionDTO dto)
|
||||
public static QuestionBase QuestionCreationToModel(CreateQuestionDto dto)
|
||||
{
|
||||
return dto.QuestionType.ToLower() switch
|
||||
{
|
||||
"text" => new TextQuestion
|
||||
"textquestion" => new TextQuestion
|
||||
{
|
||||
Title = dto.Title,
|
||||
SurveyId = dto.SurveyId,
|
||||
},
|
||||
"single" => new SingleAnswerQuestion
|
||||
"singleanswerquestion" => new SingleAnswerQuestion
|
||||
{
|
||||
Title = dto.Title,
|
||||
SurveyId = dto.SurveyId,
|
||||
AnswerVariants = dto.AnswerVariants?.Select(v => new AnswerVariant { Text = v }).ToList() ?? [],
|
||||
},
|
||||
"multiple" => new MultipleAnswerQuestion
|
||||
"multipleanswerquestion" => new MultipleAnswerQuestion
|
||||
{
|
||||
Title = dto.Title,
|
||||
SurveyId = dto.SurveyId,
|
||||
|
|
|
|||
|
|
@ -0,0 +1,48 @@
|
|||
using SurveyBackend.DTOs.Question;
|
||||
using SurveyLib.Core.Models;
|
||||
|
||||
namespace SurveyBackend.Mappers.QuestionDTOs;
|
||||
|
||||
public static class QuestionOutputMapper
|
||||
{
|
||||
public static object ModelToQuestionDTO(QuestionBase question)
|
||||
{
|
||||
return question.Discriminator.ToLower() switch
|
||||
{
|
||||
"textquestion" => new OutputTextQuestionDto
|
||||
{
|
||||
Id = question.Id,
|
||||
Title = question.Title,
|
||||
SurveyId = question.SurveyId,
|
||||
QuestionType = question.Discriminator
|
||||
},
|
||||
"singleanswerquestion" => new OutputSingleAnswerQuestionDto
|
||||
{
|
||||
Id = question.Id,
|
||||
Title = question.Title,
|
||||
SurveyId = question.SurveyId,
|
||||
QuestionType = question.Discriminator,
|
||||
AnswerVariants = GetAnswerVariants(question.AnswerVariants ?? Array.Empty<AnswerVariant>())
|
||||
},
|
||||
"multipleanswerquestion" => new OutputMultipleAnswerQuestionDto
|
||||
{
|
||||
Id = question.Id,
|
||||
Title = question.Title,
|
||||
SurveyId = question.SurveyId,
|
||||
QuestionType = question.Discriminator,
|
||||
AnswerVariants = GetAnswerVariants(question.AnswerVariants ?? Array.Empty<AnswerVariant>())
|
||||
},
|
||||
_ => throw new Exception($"Unknown question type: {question.Discriminator}")
|
||||
};
|
||||
}
|
||||
|
||||
private static List<OutputAnswerVariantDto> GetAnswerVariants(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