Demo: Update Article

Just like before we'll start in the Conduit.Api Project and go into the Controller folder and edit the Articles Controller. We'll expand the Update Article method and go into the implementation of the UpdateArticleAsync Method.

I just noticed that I am missing the slug parameter when I call UpdateArticleAsync. Let's add that in and fix the method. We'll let Visual Studio do some work for us by adding the parameter to the interface class for us.

The problem is that when we go to the implementation it won't find the method. We need to go directly to the Article Repository class and implement the interface. We can remove the code for the UpdateArticleAsync with the single parameter - we won't that one.

The first thing we'll need to do is to ask the Context to fetch that Editorial from the table where the slug matches. We'll need to fix the error by making the method async. There we go… Now it's happy!

Now we check each property to see if it's been updated. We'll do that with an if statement checking for a null title. If the title is null, it means that value was not provided. Remember that some fields are optional. Only provide them if you plan to update them.

If the Title is provide we need to generate a new slug. We do this for every time the title is updated.

The Description and the Body are straight forward where we just check to see if it was provided and updated then we just simply replace the values. Nothing exciting there.

We'll ask the Context to update the Editorial with the changes and we save the changes to the database.

Finally, we reuse the GetArticleBySlugAsync method to return the updated Article.

And that's it! Bada-bing-bada-boom! Easy-peazy. Not a whole lot to it.

Let's try it out and see if it works.

I'll need to login so I can get a copy of the JSON Web Token to authorize me. You'll notice that the first call to the API is slow in comparison to subsequent calls. That's because the JIT Compiler is compiling the IL Code into Machine Code. Calls made after this initial call are lightening fast.

Let's get the slug for any article you may have so we can update it. You go ahead and update the article with any information you desire. I'll just put some updates into my article so I can test it.

Everything seems to be working fine except for the fact that we're not getting any tags with the articles.

In the next video we'll explore a little bit of Language Integrated Query or better known as Linq to see how we can resolve the issue with the missing tags.

I'll see you there!


I finished! On to the next chapter