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
Solution: Adding Followers
In the Conduit.Data Project
Add a new class named Follow with the following properties:
namespace Conduit.Data
{
public class Follow
{
public int Follower { get; set; }
public int Following { get; set; }
public virtual Person FollowerNavigation { get; set; }
public virtual Person FollowingNavigation { get; set; }
}
}
The second file you'll modify is Person class by adding the following properties and create a constructor then add the following lines of code:
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Conduit.Data
{
[Table("People")]
public class Person
{
[Key]
public int Id { get; set; }
[Required]
[StringLength(50)]
public string UserName { get; set; }
[StringLength(200)]
public string Bio { get; set; }
[Required]
[StringLength(200)]
public string Image { get; set; }
public Account Account { get; set; }
public virtual ICollection<Follow> FollowerNavigations { get; set; }
public virtual ICollection<Follow> FollowingNavigations { get; set; }
public Person()
{
FollowerNavigations = new HashSet<Follow>();
FollowingNavigations = new HashSet<Follow>();
}
}
}
Finally the third file in the Conduit.Data Project
Modify ConduitContext by adding the following property:
See code below
In the
OnModelCreating
method, add the following code immediately after modelBuilder.Entity<Person>()
configuration call.
using Microsoft.EntityFrameworkCore;
namespace Conduit.Data
{
public class ConduitContext : DbContext
{
public DbSet<Account> Accounts { get; set; }
public DbSet<Person> People { get; set; }
public DbSet<Follow> Follows { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Account>(entity =>...);
modelBuilder.Entity<Person>(entity =>...);
modelBuilder.Entity<Follow>(entity =>
{
entity.HasKey(e => new { e.Follower, e.Following });
entity.HasOne(d => d.FollowerNavigation)
.WithMany(p => p.FollowerNavigations)
.HasForeignKey(d => d.Follower)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_Follower_Profiles");
entity.HasOne(d => d.FollowingNavigation)
.WithMany(p => p.FollowingNavigations)
.HasForeignKey(d => d.Following)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_Following_Profiles");
});
base.OnModelCreating(modelBuilder);
}
public ConduitContext(DbContextOptions<ConduitContext> options) : base(options)...
}
}
Let's go thru the code in the ConduitContext to get a better understanding of what it's doing.
This is the code that we use to configure the entity. The Entity meaning the Follow Class. We know that the People & Follow Class are related. We see that from the properties we set.
The configuration code we put in the ConduitContext Class is to build the structure in the database.
So let's go thru it line by line:
- We know that "Entity" represents the Follow class. That's represented by the Generic type.
- Therefore, entity is a Follow class which HasOne FollowerNavigation
- The FollowerNavigation is a Person. We know that because the property type is Person.
- That Person has many FollowingNavigations. We know that because the property is a Generic ICollection
. - The many that are return is the Follow Class and it has a Foreign Key that is specified by the Following property.
- Whenever it's deleted from code it deletes it on the database.
- And finally we give it a constraint name that the database can use.
This is the configuration used to create the tables in the database.