100 lines
No EOL
3.2 KiB
C#
100 lines
No EOL
3.2 KiB
C#
using SurveyBackend.DTOs.Question;
|
|
using SurveyBackend.Services.Exceptions;
|
|
using SurveyLib.Core.Models;
|
|
using SurveyLib.Core.Models.QuestionVariants;
|
|
|
|
namespace SurveyBackend.Mappers;
|
|
|
|
/// <summary>
|
|
/// Маппер всего про вопросы
|
|
/// </summary>
|
|
public static class QuestionMapper
|
|
{
|
|
/// <summary>
|
|
/// Создание вопроса в модель
|
|
/// </summary>
|
|
/// <param name="dto"></param>
|
|
/// <param name="surveyId"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="BadRequestException"></exception>
|
|
public static QuestionBase QuestionCreationToModel(QuestionCreateDto 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 = [],
|
|
},
|
|
"multipleanswerquestion" => new MultipleAnswerQuestion
|
|
{
|
|
Title = dto.Title,
|
|
SurveyId = surveyId,
|
|
AnswerVariants = []
|
|
},
|
|
_ => throw new BadRequestException("Unknown question type")
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Модель в выходную схему
|
|
/// </summary>
|
|
/// <param name="question"></param>
|
|
/// <returns></returns>
|
|
public static QuestionOutputDto ModelToQuestionDto(QuestionBase question)
|
|
{
|
|
var withAnswerVariants = question.GetType() != typeof(TextQuestion);
|
|
return new QuestionOutputDto
|
|
{
|
|
Id = question.Id,
|
|
SurveyId = question.SurveyId,
|
|
Title = question.Title,
|
|
QuestionType = question.Discriminator,
|
|
AnswerVariants = withAnswerVariants ? AnswerVariantsToDto(question.AnswerVariants) : null,
|
|
};
|
|
}
|
|
|
|
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)
|
|
{
|
|
return answerVariants.Select(av => new OutputAnswerVariantDto
|
|
{
|
|
Id = av.Id,
|
|
QuestionId = av.QuestionId,
|
|
Text = av.Text,
|
|
}).ToList();
|
|
}
|
|
} |