Dotnet
Update User

Solution - Implement Interface

PRO
Outline

Solution: Add UpdateLoggedInUserAsync Method

In the Conduit.Repositories Project

Modify IAccountRepository.cs by adding the following Method Signatures:
  • Name: UpdateLoggedInUserAsync
  • Return Type: User
  • Parameters: UpdateUser
Modify AccountRepository.cs by Implementing the IAccountRepository Interface
  • Hover over the error on IAccountRepository
  • Select [Show Potential Fixes]
  • Select [Implement interface]

We'll come back and implement that method after we complete the other exercises.

Here's the code to the IAccountRepository.cs file:

using Conduit.Data;
using Conduit.Models.Requests;
using Conduit.Models.Responses;
using System.Threading.Tasks;

namespace Conduit.Repositories
{
    public interface IAccountRepository
    {
        Task<User> RegisterUserAsync(Register register);
        Task<User> LoginAsync(Login user);
        Task<User> GetCurrentUserAsync();
        Task<Account> GetLoggedInUserAsync();
        Task<User> UpdateLoggedInUserAsync(UpdateUser model);
    }
}

Here's the code to theUpdateLoggedInUserAsync method in the AccountRepository.cs file:

        public async Task<User> UpdateLoggedInUserAsync(UpdateUser model)
        {
            bool genToken = false;
            Account account = await GetLoggedInUserAsync();
            if (!string.IsNullOrWhiteSpace(model.Email)
                && account.Email != model.Email)
            {
                if (!await EmailIsUniqueAsync(model.Email))
                    throw new DuplicateEmailException($"Email {model.Email} is already in use.");

                account.Email = model.Email;
                genToken = true;
            }

            if (!string.IsNullOrWhiteSpace(model.Password)
                && !VerifyPassword(model.Password, account.PasswordHash, account.PasswordSalt))
            {
                byte[] passwordHash, passwordSalt;
                CreatePasswordHash(model.Password, out passwordHash, out passwordSalt);
                account.PasswordHash = passwordHash;
                account.PasswordSalt = passwordSalt;
                genToken = true;
            }

            if (!string.IsNullOrWhiteSpace(model.UserName)
                && account.Person.UserName != model.UserName)
            {
                if (!await UserNameIsUniqueAsync(model.UserName))
                    throw new DuplicateUserNameException($"User name {model.UserName} is already in use.");

                account.Person.UserName = model.UserName;
            }

            if (!string.IsNullOrWhiteSpace(model.Image))
                account.Person.Image = model.Image;
            if (!string.IsNullOrWhiteSpace(model.Bio))
                account.Person.Bio = model.Bio;

            await Context.SaveChangesAsync();
            User user = CreateUser(account);
            if (genToken)
            {
                user.Token = CreateToken(user);
            }
            return user;
        }
 

I finished! On to the next chapter