Exercise - Run the app
In the previous exercise, you wrote code for your team's API to implement database operations. In this exercise, you test the API that you connected to the database.
Run the API
In the terminal pane, run the app:
dotnet run
Inspect the output from running the app, and note the following information:
- EF Core echoes SQL commands as
info
log events when they execute. - If the database doesn't already exist, the tables and indexes are defined by using SQL
CREATE
commands. - If the database isn't seeded yet,
INSERT
commands are executed to add the seed data. - For security, the parameter values aren't echoed to the console. You can change this setting by using EnableSensitiveDataLogging.
- EF Core echoes SQL commands as
Use SQLite Explorer to explore the seeded database. Each table has data.
Go to Swagger
Now that the API is running, test the API to see if the operations work as expected. The API is configured to use Swagger to provide a test UI. Swagger is a tool that helps you design, build, document, and consume RESTful web services.
In the output that appears after you run the app, find the HTTP URL where the app listens. The output looks similar to the following example:
info: Microsoft.Hosting.Lifetime[14] Now listening on: http://localhost:5200
To open the URL, select it while holding Ctrl. The browser opens to the
/
location for the API, which returns the textContoso Pizza management API. Go to /swagger to open the Swagger test UI.
In the browser's address bar, add
/swagger
to the end of the URL and select Enter.
Test CRUD operations
In the following steps, you use the Swagger UI to test each of the API's operations the way that a client application would. After each operation, inspect the database in SQLite Explorer to see the database changes as they happen.
Request the full list of pizzas:
- Under the Pizza heading, expand the GET /Pizza operation and select the Try it out button.
- Select the Execute button.
The API returns the list of pizzas as JSON (under Response body).
[ { "id": 1, "name": "Meat Lovers", "sauce": null, "toppings": null }, { "id": 2, "name": "Hawaiian", "sauce": null, "toppings": null }, { "id": 3, "name": "Alfredo Chicken", "sauce": null, "toppings": null } ]
Tip
Why are the
sauce
andtoppings
properties null? This result is expected, because in thePizzaService.GetAll
method, you didn't use theInclude
extension method to specify that the navigation properties should be loaded.Request a single pizza:
- Scroll down to the GET /Pizza/{id} operation and expand it, then select the Try it out button.
- In the id field, enter 2, then select Execute.
The API returns the "Hawaiian" pizza. Notice that the
sauce
andtoppings
properties are populated because thePizzaService.GetById
method uses theInclude
extension method.Add a new pizza:
Scroll up to the POST /Pizza operation (located between the GET operations you used) and expand it, then select the Try it out button.
In the Request body text box, paste the following JSON:
{ "name": "BBQ Beef", "sauce": { "name": "BBQ", "isVegan": false }, "toppings": [ { "name": "Smoked Beef Brisket", "calories": 250 } ] }
Select Execute.
The API returns the new pizza with the
id
property populated.Add another topping to the BBQ Beef pizza:
- Scroll down to the PUT /Pizza/{id}/addtopping operation and expand it, then select the Try it out button.
- In the id field, enter 4.
- In the toppingId field, enter 5.
- Select Execute.
The API updates the pizza and returns a success code. In the database, a record is added to
PizzaTopping
to associate the pizza with the topping.Change the sauce on the BBQ Beef pizza:
- Scroll down to the PUT /Pizza/{id}/updatesauce operation and expand it, then select the Try it out button.
- In the id field, enter 4.
- In the sauceId field, enter 2.
- Select Execute.
The API updates the pizza and returns a success code. In the database, the
Pizza
record is updated to associate the pizza with the new sauce.Return to the GET /Pizza/{id} operation and request the BBQ Beef pizza by setting the id field to 4. Then, select Execute. Notice that the
sauce
andtoppings
properties are populated.{ "id": 4, "name": "BBQ Beef", "sauce": { "id": 2, "name": "Alfredo", "isVegan": false }, "toppings": [ { "id": 5, "name": "Pineapple", "calories": 75 }, { "id": 6, "name": "Smoked Beef Brisket", "calories": 250 } ] }
You just realized a smoked brisket pizza with Alfredo sauce and pineapple is a terrible idea.
Delete the pizza:
- Find the DELETE /Pizza/{id} operation and expand it, then select the Try it out button.
- In the id field, enter 4.
- Select Execute.
The API deletes the pizza and returns a success code. In the database, the
Pizza
record and the associated records inPizzaTopping
are deleted.In the terminal with the running app, select Ctrl+C to stop the running app.
Tip
You can experiment with the app. Whenever you'd like to start with a fresh database, stop the app and delete the ContosoPizza.db, .db-shm, and .db-wal files. Then, run the app again.
Great work! The app is working with your database as expected! In the next unit, you scaffold entity models from an existing database.