QuestionController

Here's the QuestionController, which we also need to create in the /Controllers/ folder as a .cs file, just like we did with the QuizController:

 
using System;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using TestMakerFreeWebApp.ViewModels;
using System.Collections.Generic;

namespace TestMakerFreeWebApp.Controllers
{
[Route("api/[controller]")]
public class QuestionController : Controller
{
// GET api/question/all
[HttpGet("All/{quizId}")]
public IActionResult All(int quizId)
{
var sampleQuestions = new List<QuestionViewModel>();

// add a first sample question
sampleQuestions.Add(new QuestionViewModel()
{
Id = 1,
QuizId = quizId,
Text = "What do you value most in your life?",
CreatedDate = DateTime.Now,
LastModifiedDate = DateTime.Now
});

// add a bunch of other sample questions
for (int i = 2; i <= 5; i++)
{
sampleQuestions.Add(new QuestionViewModel()
{
Id = i,
QuizId = quizId,
Text = String.Format("Sample Question {0}", i),
CreatedDate = DateTime.Now,
LastModifiedDate = DateTime.Now
});
}

// output the result in JSON format
return new JsonResult(
sampleQuestions,
new JsonSerializerSettings()
{
Formatting = Formatting.Indented
});
}
}
}

As we can see, we ditched the Latest method we defined in the QuizController and replaced it with an All method that will give us the chance to retrieve all the questions related to a specific Quiz, given its Id.

Implementing a Latest method will have little sense here, as Questions won't have a distinctive meaning on their own. They are only meant to be retrieved--and presented to the user--along with the Quiz they're related to; that's why the All method makes much more sense.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.118.229