Outline

Demo: Delete Article

Just like we did with any other Article Controller Methods let's open the Conduit.Api Project, expand Controllers, open the Articles Controller and expand the Delete Article method.

Once you expand the method we'll Right-Click on the DeleteArticleAsync method of the Article Repository and Go To Implementation.

We'll start by having the Context retrieve the Editorial from the database and it will do so by calling Context, Editorials, FirstOrDefaultAsync. This method takes a function and this function accepts a parameter which represents and Editorial and returns the First where it matches the slug. Or returns the Default which in this case would be a null value.

Let's fix the error by making the method async.

We'll also need the Account information for the User that is currently Logged in.

Next, if the Editorial is null or the Person Id on the Editorial doesn't match the logged in user's id we'll Throw an Article Not Found Exception.

Otherwise, let's have the Context Remove the Editorial and Save the changes.

Another straight forward method that doesn't have a whole lot of complexity to it. Well, relatively speaking.

In case you forgot what users you have in your Accounts Table we can take a quick peak to see what's in there. Let's expand the SQL Server Object Explorer, expand SQL Server, expand (localdb), Databases, Conduit and Tables.

You can Right-Click on the Accounts table and View Data. Take note of the users you have so we can switch accounts while testing.

Let's fire it up and watch it work.

Login with an identity that hasn't created any articles. Next we'll get a list of Articles so we can get the slug from one of them. We'll plug in the slug into the Delete Endpoint and watch it give us a 422 Error with a message that says, "the article with slug … was not found."

This is exactly what we're expecting because this article doesn't belong to the identity that is currently logged in.

Let's login with the appropriate identity to delete this article. You'll see that it now allows you to successfully delete the article and returns a 204 No Content.

Next up, we begin adding Comments to Articles.

 

I finished! On to the next chapter