Quiz

The next entity will be the one identifying the quizzes, which will allow us to rewrite most of the QuizController sample code. Right-click on the /Data/Models/ folder, add a Quiz.cs class file, and fill it with the following code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace TestMakerFreeWebApp.Data
{
public class Quiz
{
#region Constructor
public Quiz()
{

}
#endregion

#region Properties
[Key]
[Required]
public int Id { get; set; }

[Required]
public string Title { get; set; }

public string Description { get; set; }

public string Text { get; set; }

public string Notes { get; set; }

[DefaultValue(0)]
public int Type { get; set; }

[DefaultValue(0)]
public int Flags { get; set; }

[Required]
public string UserId { get; set; }

[Required]
public int ViewCount { get; set; }

[Required]
public DateTime CreatedDate { get; set; }

[Required]
public DateTime LastModifiedDate { get; set; }
#endregion
}
}

Note the presence of the UserId foreign key, which will point to the user who created the quiz.

It’s also worth noting that we used a lot of Data Annotations attributes, as they are the most convenient way to override the default Code-First conventions.

If you want to know more about Data Annotations in EF Core, we strongly suggest reading the official documentation at the following URL:

https://docs.efproject.net/en/latest/modeling/index.html

As we can see, this Quiz entity class is very similar to the QuizViewModel class we created in Chapter 2, Backend with .NET Core. That’s perfectly fine, because that class was originally meant to resemble the public properties of the Data Source underlying model, which is precisely what we’re defining now.

The following diagram can help us better understand this:

As we can see, we’re creating the Quiz entity that will be used by EF to generate the Database (using code-first) and also translated (using property mapping) into the QuizViewModel we’ll use to serve our content to our Angular client.

As we might guess, the ApplicationUser and Quiz entities alone will hardly be enough to achieve what we want. In order to complete our initial requirements, we need to define some more entity classes, such as the following:

  • Question, which will be used to store the questions related to each quiz
  • Answer, which will be used to store the answers related to each question
  • Result, which will be used to store the results related to each quiz

Other than that, sooner or later we'll also need the entities to store the user response, but we can postpone them for the time being.

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

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