Outline

Demo: Implement GetProfileAsync

So in the past demo's we had all the methods implemented in the Repository prior to implementing the Controller. In this demo going to Implement the Controller prior to putting any code into the Repository class.

We'll employ the help of Visual Studio to help generate some code that we can then fill out with the appropriate logic.

Here's the code for the ProfilesController.cs file:

using Conduit.Models.Exceptions;
using Conduit.Models.Responses;
using Conduit.Repositories;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;

namespace Conduit.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ProfilesController : ControllerBase
    {
        private IProfileRepository ProfileRepo;
        private readonly ILogger<ProfilesController> Logger;

        [HttpGet("{userName}")]
        public async Task<IActionResult> GetProfileAsync([FromRoute] string userName)
        {
            try
            {
                UserProfile profile = await ProfileRepo.GetUserProfile(userName);
                return Ok(new { profile });
            }
            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.Message);
            }
        }

        public ProfilesController(IProfileRepository profileRepo, ILogger<ProfilesController> logger)
        {
            ProfileRepo = profileRepo;
            Logger = logger;
        }
    }
}

Use Visual Studio [Potential Fixes] to generate the method signatures in the IProfileRepository Interface class.

 

I finished! On to the next chapter