Dotnet
Update User

Solution - UpdateUserAsync

PRO
Outline

Solution: Add UpdateUserAsync

You're doing great!

In the Conduit.Api Project, modify the UserController (again, notice the singular "User")

Add the following method
  • Name: UpdateUserAsync
  • Return Type: IActionResult
  • Parameters: ServiceRequest<UpdateUser>
Add the following Attributes to the method
  • HttpPut
  • Authorize
Add the following code to the body of the method
User user = await AccountRepo.UpdateLoggedInUser(req.User);
return Ok(new { user });

Resolve any namespace errors you're seeing

Implement Exception Handling
  • Wrap the code in the body of the method with a try…catch block
  • Catch both:
    • DuplicateEmailException
    • DuplicateUserNameException

Hint: Review the code in the GetCurentUserAsync method

Here's the code for the UpdateUserAsync method in the UserController:

[HttpPut]
[Authorize]
public async Task<IActionResult> UpdateUserAsync([FromBody]UserRequest<UpdateUser> req)
{
    try
    {
      User user = await AccountRepo.UpdateLoggedInUserAsync(req.User);
      return Ok(new { user });
    }
    catch (DuplicateEmailException dEx)
    {
        Logger.LogWarning(dEx.Message, dEx);
        return StatusCode(422, dEx.ToDictionary());
    }
    catch (DuplicateUserNameException dupEx)
    {
        Logger.LogWarning(dupEx.Message, dupEx);
        return StatusCode(422, dupEx.ToDictionary());
    }
    catch (Exception ex)
    {
        Logger.LogError(ex.Message, ex);
        return StatusCode(500, ex);
    }
}
 

I finished! On to the next chapter