Articles Part 2

Implement Articles Repository Part 1


Implement IArticlesRepository

In the Conduit.Repository Project let's open the Articles Repository Class. We'll need to implement the IArticlesRepository Interface. We'll do so by adding it with the Inheritance Syntax. Let's hover over and [Show Potential Fixes] and select [Implement Interface].

[CTRL+M, O] will collapse all the methods.

For housekeeping purposes, let's move the constructor to the bottom, below all the methods. Don't forget to save your changes.

Implement Get Articles Async

Now that we have all the methods in place, we can go to the Article Controller and we can start from the top to begin implementing each method in the Articles Repository.

We can Right-Click and Go To Definition but that will take us to the Interface Class. Instead we'll Right-Click and Go To Implementation and you'll see that it takes us directly to where we need to begin writing code.

The method signature specifies that we need to return an Array with Articles but first we need to grab the records from the Editorials Table in the Database. So let's create an empty Generic List of Editorials.

The nice thing about the API is that we only need to meet one condition at a time. So let's start by checking if the tag parameter is not null. If it's not null we can call a method called GetEditorialsByTagAsync. We'll pass in the tag, limit and the offset as parameters).

Let's have Visual Studio generate that method for us and since it's private we'll move it to the above the public methods. This brings some organization to our class.

Let's make the method async and we'll return await Editorials from the Context Where…

Let's hover over Where and Show Potential Fixes. Let's bring in the System.Linq Namespace. Where it takes a function which accepts a parameter. Using the fat arrow function we'll call the Any method on the Tags.

Any also takes a function that accepts a parameter. Again using the fat arrow function we'll check if the Tag DisplayName equals the tag.

We'll need to Include a few other entities so let's bring in that Namespace Microsoft.Entity.FrameworkCore. Let's include Favorites, Person and Tags.

We'll call the method OrderbyDescending that takes a function which accepts a parameter and we'll order by CreatedAt.

Then we just Skip based on the offset parameter, Take based on the limit parameter and send it off ToListAsync.

Let's go back to our calling method where we'll create a Generic List of Articles by calling ArticleListMapper and pass in the Generic List of Editorials as a parameter.

Let's have Visual Studio create that method for us. Let's move this method up above the public method in this class.

In the ArticleListMapper we'll create another Generic List of Articles and using the foreach syntax we'll iterate thru the editorials to map single Articles. You'll see what I mean in a moment.

Let's make our method async and generate the method.

In the ArticleMapper we'll create a single Article and we'll initialize the Author. We can use the Mapper to Map Editorial to an Article and Person to Author.

We'll need to determine if the logged user has favorited this article. Let's create a method so we can make that determination.

The first check we'll make is if no one has favorited the article we'll return false. Otherwise, we'll get the logged in user. Let's make this method async while we're here. If the there is no logged in user we'll return false here as well.

Otherwise, we'll perform a check to see if any person who favorited this article is the logged in user id.

Going back to our ArticleMapper, let's fix the error by making the method async.

So let's go back to our ArticleMapper to add some code where we determine if the logged in user is following the Author. This will be new code we add to the Profile Repository. Let Visual Studio generate that method for us.

If we try to Right-Click and Go To Implementation we'll get an error because that method doesn't exist yet. Instead, if we need to open the ProfileRepository file directly. Let's fix the error by hovering over it, select Show Potential Fixes and select Implement Interface.

We'll start by getting the Author, in this case it's the Person Class. We'll do that by reusing an existing method we previously created called GetPersonAndFollowersAsync and forward the parameter userName along to the it. Make the method async.

Next we'll need to get the logged in user.

We'll perform a check to see if the logged in user is following the Author. The ternary check is like an if statement. If no one is logged in we'll return zero otherwise return the Account Id for comparison.

We'll save this and go back to the calling method.

Finally, we'll return the article, return the Generic List of Articles and finally return an array of Articles.


This is most of the heavy lifting we'll need for now. The rest of the solution should be much shorter and we'll be reusing a lot more methods.

I'll see you in the next video!


I finished! On to the next chapter