Quiz angefangen
This commit is contained in:
78
MultipleChoiceTrainer/Controllers/QuizController.cs
Normal file
78
MultipleChoiceTrainer/Controllers/QuizController.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MultipleChoiceTrainer.Data;
|
||||
using MultipleChoiceTrainer.Models;
|
||||
using MultipleChoiceTrainer.Models.DataModels;
|
||||
|
||||
namespace MultipleChoiceTrainer.Controllers
|
||||
{
|
||||
public class QuizController : Controller
|
||||
{
|
||||
private readonly ApplicationDbContext _context;
|
||||
|
||||
public QuizController(ApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public IActionResult Section(int id)
|
||||
{
|
||||
var vm = new QuizViewModel()
|
||||
{
|
||||
QuizType = QuizType.Section,
|
||||
CurrentTypeId = id
|
||||
};
|
||||
|
||||
GetQuestion(vm);
|
||||
|
||||
return Quiz(vm);
|
||||
}
|
||||
|
||||
public IActionResult Category(int id)
|
||||
{
|
||||
var vm = new QuizViewModel()
|
||||
{
|
||||
QuizType = QuizType.Category,
|
||||
CurrentTypeId = id
|
||||
};
|
||||
GetQuestion(vm);
|
||||
|
||||
return Quiz(vm);
|
||||
}
|
||||
|
||||
private void GetQuestion(QuizViewModel vm)
|
||||
{
|
||||
var questions = _context.Questions.Include(e => e.Section).ThenInclude(e => e.Category).Include(e => e.Choices).Include(e => e.Answers).AsQueryable();
|
||||
|
||||
switch(vm.QuizType)
|
||||
{
|
||||
case QuizType.Category:
|
||||
questions = questions.Where(e => e.Section.CategoryId == vm.CurrentTypeId);
|
||||
break;
|
||||
case QuizType.Section:
|
||||
questions = questions.Where(e => e.SectionId == vm.CurrentTypeId);
|
||||
break;
|
||||
}
|
||||
|
||||
questions = questions.ToList().AsQueryable();
|
||||
questions = questions.OrderByDescending(e => e.Answers.Count());
|
||||
questions = questions.Take(10);
|
||||
|
||||
var rnd = new Random();
|
||||
vm.CurrentQuestion = questions.ElementAt(rnd.Next(0, questions.Count() - 1));
|
||||
|
||||
vm.Choices = vm.CurrentQuestion.Choices.Select(oc => new Choice() { Id = oc.Id, Text = oc.Text }).ToList();
|
||||
}
|
||||
|
||||
public IActionResult Quiz(QuizViewModel viewModel)
|
||||
{
|
||||
|
||||
|
||||
return View("quiz", viewModel);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user