Outline

Demo: Implement GetUserProfileAsync

The final piece to get the GetUserProfileAsync to work is to implement the Repository class. Lets see how can accomplish that.

Add the following code to the body of GetUserProfileAsync:
public Task<UserProfile> GetUserProfileAsync(string userName)
{
    Person person = await GetPersonAndFollowersAsync(userName);
    if (person is null)
        throw new ProfileNotFoundException($"Profile for [{userName}] is not found.");

    UserProfile profile = await CreateProfileAsync(person);
    return profile;
}
  • Add the async keyword to the method signature
  • Bring in the namespace for ProfileNotFoundException

You'll notice that we have 2 private methods that don't exist yet:

  • GetPersonAndFollowersAsync
  • CreateProfileAsync
Hover over the errors

Select [Show Potential Fixes] and Select [Generate method]

Add the following code to the body of GetPersonAndFollowsAsync method:
private async Task<Person> GetPersonAndFollowersAsync(string userName)
{
    return await Context
        .People
        .Where(p => p.UserName == userName)
        .Include(p => p.FollowingNavigations)
        .FirstOrDefaultAsync();
}
  • Ad the async keyword to the method signature
  • Bring the necessary namespaces to eliminate all the errors
Add the following code to the body of CreateProfileAsync method:
private async Task<UserProfile> CreateProfileAsync(Person person)
{
    int loggedInUserId = 0;
    Account loggedInUser = await AccountRepo.GetLoggedInUserAsync();

    if (loggedInUser is not null)
        loggedInUserId = loggedInUser.Id;

    UserProfile profile = new UserProfile
    {
        Following = person.FollowingNavigations.Any(u => u.Follower == loggedInUserId)
    };
    Mapper.Map(person, profile);
    return profile;
}
  • Add the async keyword to the method signature
 

I finished! On to the next chapter