Outline

Demo: Implement Articles Controller

In the Conduit.Api Project, let's open the ArticlesController so we can start implementing the methods.

The first thing we'll need to do is inject the IArticlesRepository interface. We'll do that by creating a - Private IArticlesRepository and we'll call it ArticlesRepo - Let's bring in the namespace as well

Next we'll use the constructor to inject the IArticlesRepository as a parameter. Finally we'll map it in the body of the constructor.

Get Articles The first method we'll implement the GetArticles. We'll need to return an array of Articles and we'll do so by calling: - await ArticlesRepo.GetArticlesAsync

And we'll pass all the parameter that came thru from the query: - Tag - Author - Favorited - Limit - Offset

We'll need to bring the namespace for the Article Response Model, make the controller's method async and we'll need to ask Visual Studio to generate the GetArticlesAsync method for in the interface class.

Let's also clean up the method signature by removing the explicit namespace and bringing via the using statement.

Finally we'll send back a new object with the Articles array and the Articles count.

Let's save and move on to the next.

Exercise Methods I don't want to take all the fun away from developing so I'll be demo'ing a few more methods. I want to leave an exercise for you where you get to implement a few of these methods.

Create Article With that said, let me demo the implementation for the CreateArticle Method. This method will only return a single Article by calling await ArticlesRepo.CreateArticleAsync. We'll pass in the ArticleCreateRequest on req.Article.

Let's make the method async and we'll have Visual Studio create the method for us.

To finish off this method, we'll return a new object with the Article in it.

Add Comment To Article In this last demo, before I have you implement the rest of the methods as an exercise I'll implement the AddCommentToArticle method.

This method returns a Comment and we'll do so by calling await ArticleRepo.AddCommentToArticleAsync. In this case we'll pass in 2 parameters. The first is the slug while the second is the CommentAddRequest which we can find in the req.Comment.

We'll make the method async and we'll have Visual Studio generate the method for us.

We'll return a new object with the new Comment in it.

In this method, we want to catch another exception called ArticleNotFoundException

 

I finished! On to the next chapter