Outline

Demo: Favorite/Unfavorite Article

By now you probably already know where to find the code that needs to be modified. But for the sake of habit and repetition I'll follow the methods to where I need to go to begin modifying the code.

The only way to get to this method is login. That requirement is enforced in the controller. The request won't make it this far if the user is not logged in. Without say, we'll need the account information of the logged in User.

We'll also need the Editorial from the Database so we'll have the Context get it for us.

If the logged in User hasn't Favorited the Article we'll go ahead and create a new Favorite, ask the Context to add it and Save the changes.

We'll return the newly Favorited Article by reusing our infamous method Get Article by Slug.

Now for the Unfavorite Article Method, it's pretty much the same code except for a couple of reverse changes. Let's copy this code and paste it into the Unfavorite Article Method.

First thing's first, let's fix the error by making the method Async.

Second, we'll need to reverse the logic on the if statement. Since we're Unfavoriting the Article we need to check if it's been Favorited.

Third, we already have the Favorite Object as part of the Editorial. We can just grab it from there.

Last but definitely not least, we need to have the Context Remove it and save the changes.

To close it all up, return the Unfavorited Article.

Let's run it so we can test it.

Login with your favorite user (no pun intended).

Let's get a list of Articles so we can grab a Slug. We'll scroll down to the Favorite Article Endpoint and let's plug in the Slug. Excellent! Looks like it's working as expected.

Let's try the Unfavorite Article. And Excellent! That one works as well.

It's all looking good.

We have one more final piece to put together and we'll be doing that thru an exercise. In the next video I'll provide the details. See you there!

Tag Name: working-article-api

 

I finished! On to the next tutorial