12.3 Assignment

Overview

12.3 Assignment

In this assignment, you will create an API using the provided database or a database of your choosing and create calls to test the API through Postman. Use the 12.3 Lab and assigned readings as reference.

Work through the stories in order. Use the acceptance criteria to check each feature, and complete the nested tasks using the specified names, values, and scenarios.

Checkpoint reference
Operation Checkpoint response
GET 200 OK with the requested data.
POST 200 OK with the created result, as required here.
PUT (12.4 onward) 200 OK after the update.
DELETE (12.5) 204 No Content after deletion.
Create the service and database
1.In Visual Studio, create a new ASP.NET Core Web Application named 12.3AssignmentYourLastName. Select the Web API template.…

In Visual Studio, create a new ASP.NET Core Web Application named 12.3AssignmentYourLastName. Select the Web API template. Make sure Authentication is set to No Authentication.

A In Visual Studio, create a new ASP.NET Core Web Application named 12.3AssignmentYourLastName. Select the Web API template.…

In Visual Studio, create a new ASP.NET Core Web Application named 12.3AssignmentYourLastName. Select the Web API template. Make sure Authentication is set to No Authentication.

2.Download DB Browser for SQLite.

Download DB Browser for SQLite

Download DB Browser for SQLite
A Download DB Browser for SQLite.
3.Create a database using SQLite DB Browser to connect your API to. You can download the Chinook database below, or you can…

Create a database using SQLite DB Browser to connect your API to. You can download the Chinook database below, or you can use a database of your own choosing. Save the database file in the same location as your .csproj file.

chinook.db

chinook.db
A Familiarize yourself with the database. Select 4-5 tables that you will utilize in your API application.

The tables that you select should have some relation to one another.

Map and access the database
Acceptance Criteria
Acceptance criteria
  • Models and DbSet mappings represent the chosen database; a custom database contains 4–5 related tables.
  • The SQLite connection and context registration agree with the project’s framework and package versions.
  • The initial migration and database update complete, and DataService provides the required CRUD access.
Instructions
4.Create the Models that will be used to represent the data (tables) in the application's database, these Model classes should…

Create the Models that will be used to represent the data (tables) in the application's database, these Model classes should be stored in a new Models folder.

A Create the Models that will be used to represent the data (tables) in the application's database, these Model classes should…

Create the Models that will be used to represent the data (tables) in the application's database, these Model classes should be stored in a new Models folder.

5.Create the DataContext class in a Data folder to map the model entities over the database connection.
A Create a constructor that initializes a new instance of the DataContext class by referencing the connection and mapping source.
B Add properties of type DBSet for each of the models created in the Models folder
C Override the OnModelCreating method to configure the models to the data context, and create the necessary relationships…

Override the OnModelCreating method to configure the models to the data context, and create the necessary relationships between each entity in the overridden method.

6.Create a connection with SQLite Server and your chosen database by configuring the ConnectionStrings object in the…

Create a connection with SQLite Server and your chosen database by configuring the ConnectionStrings object in the appsettings.json file to point to your database.

A In your appsettings.json file have your ConnectionString set the Data Source to the database's file path, similar to what is…

In your appsettings.json file have your ConnectionString set the Data Source to the database's file path, similar to what is shown below.

"ConnectionStrings": {
"DefaultConnection": "Data Source=./NetflixTitles.db"
}
B Install the Microsoft.EntityFrameworkCore.Design and Microsoft.EntityFrameworkCore.Sqlite packages
7.Add the DataContext to the application services in the StartUp class by configuring it to the SQLite database.
_ = services.AddDbContext<DataContext>
(options => options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));
A Add the DataContext to the application services in the StartUp class by configuring it to the SQLite database.
8.Execute the initial EF migration

We go into more detail on Entity Framework in Software Architecture, but it is essentially a library that automates SQL interactions from your application. If you have any questions, please let your instructor know! If you have not yet taken Software Architecture, please refer to the following articles:

MSDN Getting Started with EF Core

MSDN Getting Started with EF Core

ASP.NET Core MVC with EF Core - tutorial series

ASP.NET Core MVC with EF Core - tutorial series
A Install the Entity Framework CLI tools.
dotnet tool install --global dotnet-ef --version 9.0.0install --global dotnet-ef --version 9.0.0

Warning: If the command does not work, you may need to run it as an administrator.

B Run the EF command to create/generate the initial migration
dotnet ef migrations add InitialCreatemigrations add InitialCreate
C Run the EF command to execute the migration
dotnet ef database updatedatabase update
9.Create the DataService class to define the basic CRUD operations in a folder named Services.
A Create a constructor to initialize the instance of the DataService class. Give it a parameter named dataContext of type…

Create a constructor to initialize the instance of the DataService class. Give it a parameter named dataContext of type DataContext, and set the dataContext parameter to the dataContext field.

Return structured data through GET endpoints
Acceptance Criteria
Acceptance criteria
  • APIResponse initializes Status to 1 and exposes Payload.
  • The controller inherits Controller, uses the configured service, and sets successful responses to Status 0.
  • At least three GET requests return verified database data in Postman.
  • DTOs and Newtonsoft.Json configuration produce the required client-facing JSON.
Instructions
10.Create an API Response class to configure the responses from the API. Give it auto properties named Status of type int and…

Create an API Response class to configure the responses from the API. Give it auto properties named Status of type int and Payload of type object. Set the Status property to 1 in the APIResponse constructor.

A Create an API Response class to configure the responses from the API. Give it auto properties named Status of type int and…

Create an API Response class to configure the responses from the API. Give it auto properties named Status of type int and Payload of type object. Set the Status property to 1 in the APIResponse constructor.

11.Create an APIController that implements the Controller class in a folder named Controllers.
A Add a Route attribute at the class level to configure the URL for the API calls.
B Create a private readonly field named _dataService of type DataService.
C Create a constructor that takes a parameter of type DataContext named dataContext.
1 In the constructor, create a new instance of the DataService class, passing in the dataContext parameter. Set the results…

In the constructor, create a new instance of the DataService class, passing in the dataContext parameter. Set the results equal to the _dataService field.

12.Create any necessary DTO classes that will be used to display the data to the client.
A Create any necessary DTO classes that will be used to display the data to the client.
13.Configure at least 3 GET requests.
A In the DataService class, create the methods needed to return the database entities.
B In the ApiController class, create methods that will call the DataService's methods used to get the database entities.
1 In each method

Add a GET attribute that configures the URL to point to that specific API call.

Create a new instance of the APIResponse class.

Retrieve the data by calling the corresponding method in the DataService class.

Set the apiResponse's Status property to 0 after retrieving the data.

Set the retrieved data to the APIResponse's payload.

Return the APIResponse object as JSON.

Utilize the DTO classes if necessary.

14.Set the returned Json Settings, by adding the services.AddControllers().AddNewtonsoftJson() call to the…

Set the returned Json Settings, by adding the services.AddControllers().AddNewtonsoftJson() call to the services.AddControllers call, in the ConfigureServices method in the startup class.

A Set the returned Json Settings, by adding the services.AddControllers().AddNewtonsoftJson() call to the…

Set the returned Json Settings, by adding the services.AddControllers().AddNewtonsoftJson() call to the services.AddControllers call, in the ConfigureServices method in the startup class.

15.Check and verify that each GET request retrieves data from the database table in the formatted JSON response in Postman.

Don't forget to start your application before sending the requests.

A Check and verify that each GET request retrieves data from the database table in the formatted JSON response in Postman.
Create records through POST
Acceptance Criteria
Acceptance criteria
  • At least one POST endpoint accepts the required fields and adds a database record.
  • A successful POST returns HTTP 200 as required for this checkpoint.
  • A subsequent GET verifies the stored record.
Instructions
16.Create at least 1 POST Request.
A In the DataService class, create the method/methods needed to add to the database entities.
B In the ApiController class, create method/methods that will call the DataService's method/methods used to add data to the…

In the ApiController class, create method/methods that will call the DataService's method/methods used to add data to the database entities.

1 In the method/methods

Add an HTTPPOST attribute that configures the URL to point to that specific API call.

Call the corresponding method in the DataService class to add the data to the database.

After adding the data to the database, return a new StatusCodeResult passing in 200.

Utilize the DTO classes if necessary.

17.Check your work in Postman and verify that each POST call adds data to the database table, and each GET call retrieves data…

Check your work in Postman and verify that each POST call adds data to the database table, and each GET call retrieves data from the database tables.

Don't forget to start your application before sending the requests.

A Check your work in Postman and verify that each POST call adds data to the database table, and each GET call retrieves data…

Check your work in Postman and verify that each POST call adds data to the database table, and each GET call retrieves data from the database tables.

Submit the service and request evidence
18.Submit a zipped folder containing your completed application and a Word Document with snippets of successful responses of…

Submit a zipped folder containing your completed application and a Word Document with snippets of successful responses of each of the API calls in Postman.

A Submit a zipped folder containing your completed application and a Word Document with snippets of successful responses of…

Submit a zipped folder containing your completed application and a Word Document with snippets of successful responses of each of the API calls in Postman.

Completion review
19.Review the feature acceptance criteria
A Confirm the required results

Return to each story and verify its acceptance criteria. Preserve the submission format and destination stated in the activity; a reading, setup guide, lab, video demonstration, and oral final may require different evidence.

Map and access the database Return structured data through GET endpoints Create records through POST