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(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") }; } /// /// Модель в выходную схему /// /// /// 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 AnswerVariantsToDto(IEnumerable answerVariants) { return answerVariants.Select(av => new OutputAnswerVariantDto { Id = av.Id, QuestionId = av.QuestionId, Text = av.Text, }).ToList(); } }