private static Question Convert(QuestionEntity entity)
        {
            Question question;

            switch (entity)
            {
            case GeneralQuestionEntity generalQuestionEntity:
                question = new GeneralQuestion();
                break;

            case InputQuestionEntity inputQuestionEntity:
                question = new InputQuestion();
                break;

            case SelectQuestionEntity selectQuestionEntity:
                question = new SelectQuestion
                {
                    OneCorrectAnswer = selectQuestionEntity.OneCorrectAnswer,
                    Options          = selectQuestionEntity.Options
                                       .Select(option => new Option
                    {
                        Id    = option.Id,
                        Title = option.Title
                    })
                                       .ToArray()
                };
                break;

            default:
                throw new InvalidOperationException($"Can't cast '{entity.GetType().FullName}'.");
            }

            question.Title       = entity.Title;
            question.Description = entity.Description;
            question.OrderIndex  = entity.OrderIndex;

            return(question);
        }
Esempio n. 2
0
        public static Question CreateContractQuestion(this QuestionEntity entity)
        {
            Question question;

            switch (entity)
            {
            case InputQuestionEntity inputEntity:
                question = new InputQuestion
                {
                    CorrectAnswer = inputEntity.CorrectAnswer
                };
                break;

            case SelectQuestionEntity select:
                question = new SelectQuestion
                {
                    OneCorrectAnswer = select.OneCorrectAnswer,
                    Options          = select.Options.Select(o => new Option
                    {
                        IsCorrect = o.IsCorrect,
                        Title     = o.Title
                    }).ToArray()
                };
                break;

            case GeneralQuestionEntity _:
                question = new GeneralQuestion();
                break;

            default:
                throw new InvalidOperationException($"Can't create contract model from '{entity.GetType().Name}' entity.");
            }

            question.OrderIndex       = entity.OrderIndex;
            question.Title            = entity.Title;
            question.Description      = entity.Description;
            question.MaxAnswerSeconds = entity.MaxAnswerSeconds;

            return(question);
        }