public async Task <bool> UpdateQuestion(int id, QuizQuestionDtoForAdd model) { var question = _context.QuizQuestions.Where(m => m.Id == id).FirstOrDefault(); if (question != null) { question.QuestionTypeId = model.QuestionTypeId; question.Question = model.Question; question.Marks = model.Marks; await _context.SaveChangesAsync(); } foreach (var item in model.Answers) { var getAnswer = _context.QuizAnswers.Where(m => m.Id == item.Id).FirstOrDefault(); if (getAnswer != null) { getAnswer.Answer = item.Answer; getAnswer.IsTrue = item.IsTrue; } //else //{ // var answer = new QuizAnswers // { // QuestionId = question.Id, // Answer = item.Answer, // IsTrue = item.IsTrue // }; // await _context.QuizAnswers.AddAsync(answer); //} await _context.SaveChangesAsync(); } return(true); }
public async Task <IActionResult> PostQuizQuestion(QuizQuestionDtoForAdd model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } //if (await _repo.SubjectExists(subject.Name)) // return BadRequest(new { message = "Subject Already Exist" }); _response = await _repo.AddQuestion(model); return(Ok(_response)); }
public async Task <IActionResult> PutQuizQuestion(int id, QuizQuestionDtoForAdd model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } //if (await _repo.SubjectExists(subject.Name)) // return BadRequest(new { message = "Subject Already Exist" }); var createdObj = await _repo.UpdateQuestion(id, model); return(Ok(createdObj)); }
public async Task <ServiceResponse <object> > AddQuestion(QuizQuestionDtoForAdd model) { if (model.Answers.Where(m => m.IsTrue == true).Count() > 0) { var questionObj = new QuizQuestions { QuizId = model.QuizId, QuestionTypeId = model.QuestionTypeId, Question = model.Question, Marks = model.Marks }; await _context.QuizQuestions.AddAsync(questionObj); await _context.SaveChangesAsync(); foreach (var item in model.Answers) { var answer = new QuizAnswers { QuestionId = questionObj.Id, Answer = item.Answer, IsTrue = item.IsTrue }; await _context.QuizAnswers.AddAsync(answer); await _context.SaveChangesAsync(); } List <QuestionForListDto> questions = await(from q in _context.QuizQuestions join qType in _context.QuestionTypes on q.QuestionTypeId equals qType.Id where q.QuizId == model.QuizId select new QuestionForListDto { QuestionId = q.Id, Question = q.Question, QuestionTypeId = q.QuestionTypeId, QuestionType = qType.Type, Marks = Convert.ToInt32(q.Marks), }).ToListAsync(); foreach (var question in questions) { List <AnswerForListDto> answers = await(from ans in _context.QuizAnswers where ans.QuestionId == question.QuestionId select new AnswerForListDto { AnswerId = ans.Id, Answer = ans.Answer, IsTrue = Convert.ToBoolean(ans.IsTrue), }).ToListAsync(); question.Answers.AddRange(answers); } _serviceResponse.Data = new { QuestionCount = questions.Count(), Questions = questions }; _serviceResponse.Success = true; _serviceResponse.Message = CustomMessage.Added; return(_serviceResponse); } else { _serviceResponse.Success = false; _serviceResponse.Message = CustomMessage.SelectLeastOneTrue; return(_serviceResponse); } }