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
comic books, the top level comic book section of our web app is almost complete.
We still need to use finish the pages that allow user's to add
remove comic book artist.
Luckily most of the work has already been done.
We just need to complete some TODOs in the ComicBookArtistController.
Sounds like a great exercise for you to do on your own.
Let's start with a quick review of the finished pages.
Here's the completed version of the Comic Book Library Manager web app.
To add an artist to a comic book, we need to first view the detail for a comic book.
I'll click on the Detail button for Borne number 3.
In the Artists on the Comic Book Detail page,
we can see the artists that are associated with this issue of Bone.
To add another artist, we can click the Add Artist button,
select an artist and role, and click the Save button.
And back on the Comic Book Detail page, here's our new artist.
To remove an artist,
click on the Delete button to the right of the artist you want to remove.
Click Delete to continue with the deletion, or
Cancel to return to the detail page.
I'll go ahead and click the Delete button to delete this artist.
And back on the Detail page, we can see that the artist has been removed.
The comic book artist add and
delete actions are both defined in the ComicBookArtistController class.
Let's review the TODO comments in this class.
Our first TODO comment is in the Add get action method.
We need to write a query that retrieves a comic book
with the Series navigation property eagerly loaded.
We've already written that query in the ComicBooksController,
so that should be easy to do.
A little further down in this method, we'll see our next TODO comment.
We need to pass the context into the viewModel.Init() method call.
This is necessary so the view model can retrieve the data for
the Artist and Role drop down lists that appear on the Add Comic Book Artist page.
Our next TODOs can be found in the Add post action method.
After we validate the comic book artist and
check that the model state is valid, we then need to add the comic book artist.
Here's a couple of hints for this TODO.
When you instantiate an instance of the ComicBookArtist entity,
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 viewModel's comicBookId property.
To add the ComicBookArtist entity to the context, you might find it helpful to
add a DBSet property to the context class for that entity type.
Later on in this method,
there's a TODO reminding us to prepare the view model for the view.
There are two parts to this.
First, we need to retrieve the comic book with the Series navigation property,
Second, we need to pass the context into the viewModel's Init method.
Now let's look at the Delete action methods.
In the get action method,
we need a query to retrieve the comic book artist that the user is wanting to delete.
Be sure to eagerly load the ComicBook.Series, Artist, and
Role navigation properties.
In the post action method,
we need to do the actual deletion of the comic book artist.
Notice that we're passed two parameters.
The first parameter is the ID for the comic book that we're removing the artist
from, and the second is the ID for the comic book artist itself.
And lastly, the ValidateComicBookArtist method, which checks that the comic book
artist that is being added doesn't already exist in the database.
To do that, you'll need to check for the existence of a ComicBookArtist entity
that has the same comic book ID, artist ID, and role Role ID property values.
And that's all of the TODOs.
Now it's your turn, go ahead and
try to implement as many of these TODOs as you can.
In the next video, I'll walk you through my solution.
See you then.
You need to sign up for Treehouse in order to download course files.Sign up