Exercise - Run the app

Completed

In this unit, you'll use the HttpRepl tool to test the API. The API will use your code to interact with the database.

Run the app and connect

  1. From the terminal, run the following command:

    dotnet tool install -g Microsoft.dotnet-httprepl
    

    This command installs the HttpRepl tool.

  2. Run the app with the following command:

    dotnet run --urls=https://localhost:5101
    

    This command launches the app and specifies the listening port to 5101.

  3. Inspect the output from running the app.

    • EF Core echoes SQL commands as info log events as they execute.
    • If the database does not already exist, then the tables and indexes are defined with CREATE SQL commands.
    • If the database has not yet been seeded, then INSERT commands are executed to add the seed data.
    • For security, the parameter values are not echoed to the console. This can be changed using EnableSensitiveDataLogging
  4. Use SQLite Explorer to explore the seeded database. Each table has data.

  5. Since the terminal is blocked by the running app, open another terminal (Ctrl+Shift+`) to test the app.

  6. In the new terminal, run the following command:

    httprepl https://localhost:5101
    

    HttpRepl connects to the running API and uses the OpenAPI specification to discover the available endpoints.

Test database operations

Now that you've connected with HttpRepl, let's try out our app. After each CRUD operation, inspect the database in SQLite Explorer to see the changes as they happen.

  1. At the HttpRepl command prompt, run the following command to list the discovered endpoints.

    ls
    
  2. Switch to the Pizza endpoint.

    cd Pizza
    
  3. Run the following command to get the list of all pizzas.

    get
    

    The API returns the list of pizzas as JSON.

    Note

    Why are the sauce and toppings properties null? Remember in the PizzaService.GetAll method, you didn't use the Include extension method to specify that the navigation properties should be loaded.

  4. Run the following command to get a single pizza.

    get 2
    

    The API returns the "Hawaiian" pizza. Notice that the sauce and toppings properties are populated because the PizzaService.GetById method uses the Include extension method.

  5. Run the following command to add a new pizza.

    post
    

    Visual Studio Code opens a new tab with a temporary file. The file contains a template for posting a new pizza. Paste in the following JSON, save, and close the tab.

    {
      "name": "BBQ Beef",
      "sauce": {
        "name": "BBQ",
        "isVegan": false
      },
      "toppings": [
        {
          "name": "Smoked Beef Brisket",
          "calories": 250
        }
      ]
    }
    
  6. Run the following command to add another topping to the new BBQ Beef pizza.

    put 4/addtopping?toppingId=5 --no-body
    
  7. Run the following command to change the sauce on the BBQ Beef pizza.

    put 4/updatesauce?sauceId=2 --no-body
    
  8. Run the following command to view the current BBQ Beef pizza.

    get 4
    
  9. You've realized a smoked brisket pizza with Alfredo sauce and pineapple is a terrible idea. Delete it with the following command:

    delete 4
    
  10. Enter exit to leave HttpRepl.

  11. In the terminal with the running app, press Ctrl+C to stop the running app.

Tip

You may experiment with the app as you wish. Whenever you'd like to start with a fresh database, stop the app and delete the ContosoPizza.db, .db-shm, and .db-wal files, and then run the app again.

Great work! The app is working with your database as expected! In the next unit, you'll scaffold entity models from an existing database.