This material is public and published on YouTube by "Les Jackson".
It is a 3.5 hour tutorial titled ".NET Core 3.1 MVC REST API - Full Course".
The text below is a brief description of the content.
This course shows you how to build a full REST API using .NET Core 3.1. We’ll employ MVC, REST, the Repository Pattern, Dependency Injection, Entity Framework, Data Transfer Objects, (DTOs), AutoMapper to provide 6 API endpoints that will allow you to Create, Read Update and Delete resources.
Tools used = Visual Studio, SQL Server Management Studio and Postman.
An interface is used here because initially, the repository will contain hard-coded mock data.
This is so that the API can be tested using Postman before setting up a database using Entity Framework Core.
Later in the project this repository is switched to one that queries a real database.
This is easy to do because of the decoupling provided by the interface.
In order to implement dependancy injection for the controller I added the line -
services.AddScoped<IShoppingRepo, MockShoppingRepo()>
Setting the service lifetime to "Scoped" means that instances of the object are created, once per client request.
Singleton means, 'the same object, for every request'.
Transient means, 'new instance created for every request'.
The controller inherits from ControllerBase
A private field is defined (_repository).
When an instance of the contoller class is created, it recieves an instance of the IShoppingList repository.
This is set to the private field so the the methods inside the controller can access the properties and methods from the repository.
These methods, declared in the interface have to be defined in the controller.
The details of these methods can be seen in the next two screenshots.
They are both HttpGET except one method accepts an integer parameter.
Installed the Entity Framework Core packages.
Created DbContext file according tot he tutorial.
Created a "connection string".
Configured the connection string in the "Startup.cs" file.
Add data annotations to the properties in the model.
Set up EF migrations and create "Initial" migration to create database.
The Mock repository is replaced with "ShoppingRepo".
This means the API will now retrieve information from the database.
One reason that DTO's are used is to keep some of the properties of the internal model private.
This is useful if the model contains sensitive information (DOB, financial info, passwords, etc).
In my app, I do not include the "ShopName" property in the DTOs, so this information does not get sent to the client.
The id is used to get the shopping list object from the database.
If the id doesn't correspond to an item in the database then a NotFound message is returned.
Otherwise, the data from the new object (DTO) is copied to the object pulled from the database.
The repository method "UpdateShoppingList" is called but this does nothing. It is there for consistency.
The repository method "SaveChanges" is called.
A "No Conetent" status code is returned.
Item 8 (Twining Tea) has been updated.
The product name, quantity and price have been changed.