Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Exercise: Finishing the Comic Book Artists Controller4:04 with James Churchill
Now that we have the ability for our users to create, read, update, and delete comic books, the top level "Comic Books" section of our web app is almost complete. That's your next exercise: finishing the pages that allow users to add and remove comic book artists.
To follow along committing your changes to this course, you'll need to fork the dotnet-comic-book-library-manager repo. Then you can clone, commit, and push your changes to your fork like this:
git clone <your-fork> cd dotnet-comic-book-library-manager git checkout tags/v2.7 -b exercise-finishing-the-comic-book-artists-controller
To finish the exercise, complete the following tasks:
- Add a private field for the database context and a default constructor to initialize the private field.
- Override the
Disposemethod and handle disposing the database context.
Add GET Action Method
- Write a query that retrieves a comic book with the
Seriesnavigation property eagerly loaded.
- Pass the context into the view model
Add POST Action Method
- Handle adding the comic book artist entity to the database.
- Prepare the view model for the view by writing a query to retrieve the comic book with the
Seriesnavigation property eagerly loaded and pass the context into the view model's
Hint: When you instantiate an instance of the ComicBookArtist entity to add to the database context, you'll need to set its
ComicBookId property value. You can get the ID of the comic book that we're adding an artist to using the view model's
Delete GET Action Method
- Write a query to retrieve the comic book artist with the
Rolenavigation properties eagerly loaded.
Delete POST Action Method
- Handle deleting the comic book artist entity from the database.
- Write a query that checks for the existence of a comic book artist entity that has the same
RoleIdproperty values as the passed entity.
- Add a parameter for the database context instance.
- Write a query to retrieve a list of artists.
- Write a query to retrieve a list of roles.
- Add a DbSet property for ComicBookArtist entities.
Now that we have the ability for our users to create, read, update and delete 0:00 comic books, the top level comic book section of our web app is almost complete. 0:05 We still need to use finish the pages that allow user's to add 0:10 remove comic book artist. 0:13 Luckily most of the work has already been done. 0:15 We just need to complete some TODOs in the ComicBookArtistController. 0:18 Sounds like a great exercise for you to do on your own. 0:23 Let's start with a quick review of the finished pages. 0:26 Here's the completed version of the Comic Book Library Manager web app. 0:29 To add an artist to a comic book, we need to first view the detail for a comic book. 0:33 I'll click on the Detail button for Borne number 3. 0:38 In the Artists on the Comic Book Detail page, 0:40 we can see the artists that are associated with this issue of Bone. 0:43 To add another artist, we can click the Add Artist button, 0:47 select an artist and role, and click the Save button. 0:51 And back on the Comic Book Detail page, here's our new artist. 0:58 To remove an artist, 1:03 click on the Delete button to the right of the artist you want to remove. 1:04 Click Delete to continue with the deletion, or 1:09 Cancel to return to the detail page. 1:12 I'll go ahead and click the Delete button to delete this artist. 1:14 And back on the Detail page, we can see that the artist has been removed. 1:19 The comic book artist add and 1:24 delete actions are both defined in the ComicBookArtistController class. 1:25 Let's review the TODO comments in this class. 1:30 Our first TODO comment is in the Add get action method. 1:34 We need to write a query that retrieves a comic book 1:37 with the Series navigation property eagerly loaded. 1:40 We've already written that query in the ComicBooksController, 1:43 so that should be easy to do. 1:47 A little further down in this method, we'll see our next TODO comment. 1:49 We need to pass the context into the viewModel.Init() method call. 1:52 This is necessary so the view model can retrieve the data for 1:59 the Artist and Role drop down lists that appear on the Add Comic Book Artist page. 2:03 Our next TODOs can be found in the Add post action method. 2:08 After we validate the comic book artist and 2:13 check that the model state is valid, we then need to add the comic book artist. 2:15 Here's a couple of hints for this TODO. 2:20 When you instantiate an instance of the ComicBookArtist entity, 2:22 you'll need to set its comicBookId property value. 2:26 You can get the ID of the comic book that we're adding an artist to using 2:29 the viewModel's comicBookId property. 2:32 To add the ComicBookArtist entity to the context, you might find it helpful to 2:35 add a DBSet property to the context class for that entity type. 2:40 Later on in this method, 2:44 there's a TODO reminding us to prepare the view model for the view. 2:46 There are two parts to this. 2:50 First, we need to retrieve the comic book with the Series navigation property, 2:51 eagerly loaded. 2:55 Second, we need to pass the context into the viewModel's Init method. 2:57 Now let's look at the Delete action methods. 3:01 In the get action method, 3:05 we need a query to retrieve the comic book artist that the user is wanting to delete. 3:06 Be sure to eagerly load the ComicBook.Series, Artist, and 3:11 Role navigation properties. 3:15 In the post action method, 3:17 we need to do the actual deletion of the comic book artist. 3:19 Notice that we're passed two parameters. 3:22 The first parameter is the ID for the comic book that we're removing the artist 3:25 from, and the second is the ID for the comic book artist itself. 3:28 And lastly, the ValidateComicBookArtist method, which checks that the comic book 3:33 artist that is being added doesn't already exist in the database. 3:37 To do that, you'll need to check for the existence of a ComicBookArtist entity 3:42 that has the same comic book ID, artist ID, and role Role ID property values. 3:47 And that's all of the TODOs. 3:52 Now it's your turn, go ahead and 3:54 try to implement as many of these TODOs as you can. 3:56 In the next video, I'll walk you through my solution. 4:00 See you then. 4:03
You need to sign up for Treehouse in order to download course files.Sign up