Updating the QuizController

Now that we know how to use Mapster, we can upgrade our QuizController accordingly. Open the QuizController.cs file and perform the following changes (new/updated lines are highlighted):

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

namespace TestMakerFreeWebApp.Controllers
{
[Route("api/[controller]")]
public class QuizController : Controller
{
#region Private Fields
private ApplicationDbContext DbContext;
#endregion

#region Constructor
public QuizController(ApplicationDbContext context)
{
// Instantiate the ApplicationDbContext through DI
DbContext = context;
}
#endregion Constructor


#region RESTful conventions methods
/// <summary>
/// GET: api/quiz/{id}
/// Retrieves the Quiz with the given {id}
/// </summary>
/// <param name="id">The ID of an existing Quiz</param>
/// <returns>the Quiz with the given {id}</returns>
[HttpGet("{id}")]
public IActionResult Get(int id)
{
var quiz = DbContext.Quizzes.Where(i => i.Id ==
id).FirstOrDefault();

return new JsonResult(
quiz.Adapt<QuizViewModel>(),
new JsonSerializerSettings()
{
Formatting = Formatting.Indented
});
}

/// <summary>
/// Adds a new Quiz to the Database
/// </summary>
/// <param name="m">The QuizViewModel containing the data to
insert</param>
[HttpPut]
public IActionResult Put(QuizViewModel m)
{
throw new NotImplementedException();
}

/// <summary>
/// Edit the Quiz with the given {id}
/// </summary>
/// <param name="m">The QuizViewModel containing the data to
update</param>
[HttpPost]
public IActionResult Post(QuizViewModel m)
{
throw new NotImplementedException();
}

/// <summary>
/// Deletes the Quiz with the given {id} from the Database
/// </summary>
/// <param name="id">The ID of an existing Test</param>
[HttpDelete("{id}")]
public IActionResult Delete(int id)
{
throw new NotImplementedException();
}
#endregion

#region Attribute-based routing methods
/// <summary>
/// GET: api/quiz/latest
/// Retrieves the {num} latest Quizzes
/// </summary>
/// <param name="num">the number of quizzes to retrieve</param>
/// <returns>the {num} latest Quizzes</returns>
[HttpGet("Latest/{num:int?}")]
public IActionResult Latest(int num = 10)
{
var latest = DbContext.Quizzes
.OrderByDescending(q => q.CreatedDate)
.Take(num)
.ToArray();
return new JsonResult(
latest.Adapt<QuizViewModel[]>(),
new JsonSerializerSettings()
{
Formatting = Formatting.Indented
});
}

/// <summary>
/// GET: api/quiz/ByTitle
/// Retrieves the {num} Quizzes sorted by Title (A to Z)
/// </summary>
/// <param name="num">the number of quizzes to retrieve</param>
/// <returns>{num} Quizzes sorted by Title</returns>
[HttpGet("ByTitle/{num:int?}")]
public IActionResult ByTitle(int num = 10)
{
var byTitle = DbContext.Quizzes
.OrderBy(q => q.Title)
.Take(num)
.ToArray();
return new JsonResult(
byTitle.Adapt<QuizViewModel[]>(),
new JsonSerializerSettings()
{
Formatting = Formatting.Indented
});
}

/// <summary>
/// GET: api/quiz/mostViewed
/// Retrieves the {num} random Quizzes
/// </summary>
/// <param name="num">the number of quizzes to retrieve</param>
/// <returns>{num} random Quizzes</returns>
[HttpGet("Random/{num:int?}")]
public IActionResult Random(int num = 10)
{
var random = DbContext.Quizzes
.OrderBy(q => Guid.NewGuid())
.Take(num)
.ToArray();
return new JsonResult(
random.Adapt<QuizViewModel[]>(),
new JsonSerializerSettings()
{
Formatting = Formatting.Indented
});
}
#endregion
}
}

We did a lot of changes here:

  • At the start of the file, we added a reference to the TestMakerFreeWebApp.Data and Mapster required namespaces.
  • We added a private DbContext member, which we assign through DI in a new constructor method.
  • We used the DbContext to change the behavior of all our data-retrieval methods: Get, Latest, ByTitle, and Random. We got rid of the Dummy Data Provider and used ApplicationDbContext instead, meaning that all the quiz data will now be fetched from our Database from now on.
  • We used the .Adapt<TDestination> method of Mapster to map the Quiz entity to the QuizViewModel class anywhere; note how we even mapped Quiz arrays into QuizViewModel arrays, as the library supports them too.
..................Content has been hidden....................

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