Outline
- ASP.NET Web API with Victor Campos
- Setting Up The Solution
- NuGet Package Manager
-
Authentication and Authorization - Registration Part 1
- Introduction
- Security
- Creating The Users Controller
- Exercise - Create Controllers
- Solution - Create Controllers
- Generics
- Create Generic Class
- Create Register Request
- Create User Response
- Create Register User Endpoint
- Interfaces
- Extracting Interfaces
- Injecting Account Repository
- Dependency Injection
- Implementing Dependency Injection
- 400 vs 422
- Modify Model Validation Behavior
-
Authentication and Authorization - Registration Part 2
- DB Context
- Add Person Domain
- Add Account Domain
- Add DB Context
- Add Connection String
- Configure Dependency Injection for DB Context
- Initial Data Migrations
- Async Process
- Implement Async Process
- Extension Methods
- Implement Extension Methods
- Cryptography
- Implement Password Encryption
- Complete Create Account
- Exercise - Install Auto Mapper
- Solution - Install Auto Mapper
- Configure Auto Mapper
- Exercise - Install JWT Bearer
- Solution - Install JWT Bearer
- Configure JWT
- Exercise - Add Configuration DI
- Solution - Add Configuration DI
- Exercise - Auto Mapper DI
- Solution - Auto Mapper DI
- Implement Create User
- Implement Create Token
- Test Register API Endpoint
- Authentication and Authorization - Login
-
Current User
- Introduction
- Exercise - Invalid Credentials Exception
- Solution - Invalid Credentials Exception
- Exercise - IAccountRepository Add Method Signatures
- Solution - IAccountRepository Add Method Signatures
- Exercise - AccountRepository Implement Interface
- Solution - AccountRepository Implement Interface
- Exercise - IAccountRepository Dependency Injection
- Solution - IAccountRepository Dependency Injection
- Exercise - Add CurrentUserAsync
- Solution - Add CurrentUserAsync
- Overview ASP.NET Core Authentication
- Configure Authentication
- Configure OpenAPI
- Implement Swagger Authorization
- Overview HttpContext
- HttpContext Dependency Injection
- Implement GetLoggedInUser
- Test CurrentUser API Endpoint
- Summary
-
Update User
- Introduction
- Exercise - Exception Classes
- Solution - Exception Classes
- Exercise - Implement Interface
- Solution - Implement Interface
- Exercise - UpdateUserAsync
- Solution - UpdateUserAsync
- Exercise - Implement UpdateLoggedInUserAsync
- Solution - Implement UpdateLoggedInUserAsync
- Test UpdateUser API Endpoint
- Summary
-
User Profile
- Introduction
- Exercise - Add Followers
- Solution - Add Followers
- Follows Data Migration
- Exercise - Add Models And Exceptions
- Solution - Add Models And Exceptions
- Exercise - Create Profile Repository
- Solution - Create Profile Repository
- Exercise - Modify ProfilesController
- Solution - Modify ProfilesController
- Implement GetProfileAsync
- Implement GetUserProfileAsync
- Exercise - Follow And Unfollow
- Solution - Follow And Unfollow
- Implement Follow And Unfollow
- Test Follow And Unfollow
- Summary
-
Articles Part 1
- Introduction
- Exercise - Modify Conduit Context
- Solution - Modify Conduit Context Part 1
- Solution - Modify Conduit Context Part 2
- Solution - Modify Conduit Context Part 3
- Solution - Modify Conduit Context Part 4
- Exercise - Create Custom Models
- Solution - Create Custom Models
- API Routes With Variables
- Exercise - Articles API Endpoints
- Solution - Articles API Endpoints
- Exercise - Articles Repository
- Solution - Articles Repository
- Implement Articles Controller
- Exercise - Implement Articles Controller
- Solution - Implement Articles Controller
-
Articles Part 2
- Recap Visualizing The Layers
- Implement Articles Repository Part 1
- Implement Articles Repository Part 2
- Solution - Implement Articles Repository
- Test GetArticles API Endpoint
- Articles Feed
- Articles By Slug
- Create Article
- Debugging
- Update Article
- Include Article Tags Using Linq
- Delete Article
- Add Comments to Article
- Solution - Add Comments To Article
- Get Comments For Article
- Delete Comment From Article
- Favorite And Unfavorite Article
- Tags
Outline
- ASP.NET Web API with Victor Campos
- Setting Up The Solution
- NuGet Package Manager
-
Authentication and Authorization - Registration Part 1
- Introduction
- Security
- Creating The Users Controller
- Exercise - Create Controllers
- Solution - Create Controllers
- Generics
- Create Generic Class
- Create Register Request
- Create User Response
- Create Register User Endpoint
- Interfaces
- Extracting Interfaces
- Injecting Account Repository
- Dependency Injection
- Implementing Dependency Injection
- 400 vs 422
- Modify Model Validation Behavior
-
Authentication and Authorization - Registration Part 2
- DB Context
- Add Person Domain
- Add Account Domain
- Add DB Context
- Add Connection String
- Configure Dependency Injection for DB Context
- Initial Data Migrations
- Async Process
- Implement Async Process
- Extension Methods
- Implement Extension Methods
- Cryptography
- Implement Password Encryption
- Complete Create Account
- Exercise - Install Auto Mapper
- Solution - Install Auto Mapper
- Configure Auto Mapper
- Exercise - Install JWT Bearer
- Solution - Install JWT Bearer
- Configure JWT
- Exercise - Add Configuration DI
- Solution - Add Configuration DI
- Exercise - Auto Mapper DI
- Solution - Auto Mapper DI
- Implement Create User
- Implement Create Token
- Test Register API Endpoint
- Authentication and Authorization - Login
-
Current User
- Introduction
- Exercise - Invalid Credentials Exception
- Solution - Invalid Credentials Exception
- Exercise - IAccountRepository Add Method Signatures
- Solution - IAccountRepository Add Method Signatures
- Exercise - AccountRepository Implement Interface
- Solution - AccountRepository Implement Interface
- Exercise - IAccountRepository Dependency Injection
- Solution - IAccountRepository Dependency Injection
- Exercise - Add CurrentUserAsync
- Solution - Add CurrentUserAsync
- Overview ASP.NET Core Authentication
- Configure Authentication
- Configure OpenAPI
- Implement Swagger Authorization
- Overview HttpContext
- HttpContext Dependency Injection
- Implement GetLoggedInUser
- Test CurrentUser API Endpoint
- Summary
-
Update User
- Introduction
- Exercise - Exception Classes
- Solution - Exception Classes
- Exercise - Implement Interface
- Solution - Implement Interface
- Exercise - UpdateUserAsync
- Solution - UpdateUserAsync
- Exercise - Implement UpdateLoggedInUserAsync
- Solution - Implement UpdateLoggedInUserAsync
- Test UpdateUser API Endpoint
- Summary
-
User Profile
- Introduction
- Exercise - Add Followers
- Solution - Add Followers
- Follows Data Migration
- Exercise - Add Models And Exceptions
- Solution - Add Models And Exceptions
- Exercise - Create Profile Repository
- Solution - Create Profile Repository
- Exercise - Modify ProfilesController
- Solution - Modify ProfilesController
- Implement GetProfileAsync
- Implement GetUserProfileAsync
- Exercise - Follow And Unfollow
- Solution - Follow And Unfollow
- Implement Follow And Unfollow
- Test Follow And Unfollow
- Summary
-
Articles Part 1
- Introduction
- Exercise - Modify Conduit Context
- Solution - Modify Conduit Context Part 1
- Solution - Modify Conduit Context Part 2
- Solution - Modify Conduit Context Part 3
- Solution - Modify Conduit Context Part 4
- Exercise - Create Custom Models
- Solution - Create Custom Models
- API Routes With Variables
- Exercise - Articles API Endpoints
- Solution - Articles API Endpoints
- Exercise - Articles Repository
- Solution - Articles Repository
- Implement Articles Controller
- Exercise - Implement Articles Controller
- Solution - Implement Articles Controller
-
Articles Part 2
- Recap Visualizing The Layers
- Implement Articles Repository Part 1
- Implement Articles Repository Part 2
- Solution - Implement Articles Repository
- Test GetArticles API Endpoint
- Articles Feed
- Articles By Slug
- Create Article
- Debugging
- Update Article
- Include Article Tags Using Linq
- Delete Article
- Add Comments to Article
- Solution - Add Comments To Article
- Get Comments For Article
- Delete Comment From Article
- Favorite And Unfavorite Article
- Tags
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.
Conclusion…
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!