Outline

Solution: Follow and Unfollow

In the Conduit.Api Project, modify the ProfilesController by adding the following methods:

Method #1
  • Name: FollowAsync
  • Return Type: IActionResult
  • Parameters: [FromRoute] string userName
Add the following Attribute to the method:

HttpPost("{userName}/follow")

Add the following code to the body of the method:
    UserProfile userProfile = await ProfileRepo.FollowAsync(userName);
    return Ok(new { userProfile });

Add the try…catch block to catch ProfileNotFoundException & Exception

Here's the code for the Follow Api Endpoint:

[HttpPost("{userName}/follow")]
public async Task<IActionResult> FollowAsync([FromRoute]string userName)
{
    try
    {
        UserProfile userProfile = await ProfileRepo.FollowAsync(userName);
        return Ok(new { userProfile });
    }
    catch (ProfileNotFoundException e)
    {
        Logger.LogWarning(e.Message, e);
        return StatusCode(422, e.ToDictionary());
    }
    catch (Exception ex)
    {
        Logger.LogError(ex.Message, ex);
        return StatusCode(500, ex);
    }
}
Method #2
  • Name: UnfollowAsync
  • Return Type: IActionResult
  • Parameters: [FromRoute] string userName
Add the following Attributes to the method:

HttpDelete("{userName}/follow")

Add the following code to the body of the method:
    UserProfile userProfile = await ProfileRepo.UnfollowAsync(userName);
    return Ok(new { userProfile });

Add the try…catch block to catch ProfileNotFoundException & Exception.

Here's the code for the Unfollow Api Endpoint:

[HttpPost("{userName}/follow")]
public async Task<IActionResult> UnfollowAsync([FromRoute] string userName)
{
    try
    {
        UserProfile userProfile = await ProfileRepo.UnfollowAsync(userName);
        return Ok(new { userProfile });
    }
    catch (ProfileNotFoundException e)
    {
        Logger.LogWarning(e.Message, e);
        return StatusCode(422, e.ToDictionary());
    }
    catch (Exception ex)
    {
        Logger.LogError(ex.Message, ex);
        return StatusCode(500, ex);
    }
}
 

I finished! On to the next chapter