public string GetQuiz(int id) { QuizRepository repository = new QuizRepository(); QuizCompleteDTO quiz = repository.GetQuizCompleteDTOById(id); string json; json = JsonConvert.SerializeObject( quiz, Formatting.Indented, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }); return(json); }
public QuizCompleteDTO GetQuizCompleteDTOById(int quizId) { using (ModelContext context = new ModelContext()) { var joinQuery = (from a in context.AnswersEntity join questionAnswer in context.QuestionAnswersEntity on a.Id equals questionAnswer.IdAnswer select new AnswersDTO { Id = a.Id, Answer = a.Answer, IdQuestion = questionAnswer.IdQuestion, IsCorrect = questionAnswer.IsRight }).ToList(); var answers = (from b in joinQuery group b by b.IdQuestion into g select g.ToList()).ToList(); var questions = (from quizQuestion in context.QuizQuestionEntity from question in context.QuestionEntity where quizQuestion.IdQuiz == quizId && quizQuestion.IdQuestion == question.Id select new QuestionDTO { Question = question.Question, UpdatedBy = question.UpdatedBy, CreatedBy = question.CreatedBy, InitialDate = question.InitialDate, Link = question.Link, LastUpdate = question.LastUpdate, Id = question.Id, Required = question.Required, status = question.status }).ToList(); foreach (QuestionDTO s in questions) { s.answers = (from answer in answers where answer.FirstOrDefault().IdQuestion == s.Id select answer).FirstOrDefault(); } QuizCompleteDTO query = (from quiz in context.QuizEntity from quizQuestion in context.QuizQuestionEntity from question in context.QuestionEntity where quizQuestion.IdQuiz == quizId && quizQuestion.IdQuestion == question.Id select new QuizCompleteDTO { DateLimit = quiz.DateLimit, GameId = quiz.GameId, InitialDate = quiz.InitialDate, Id = quiz.Id, Name = quiz.Name, IdQuizQuestion = quiz.IdQuizQuestion, Description = quiz.Description, IsMultiple = quiz.IsMultiple, Required = quiz.Required, Score = quiz.Score, CreatedBy = quiz.CreatedBy, LastUpdate = quiz.LastUpdate, Link = quiz.Link, status = quiz.status, UpdatedBy = quiz.UpdatedBy }).FirstOrDefault(); if (query != null) { query.questions = questions; } return(query); } }