Outline

Solution: Modify ProfilesController

In the Conduit.Api Project

Modify the ProfilesController by adding the following method
  • Name: GetProfileAsync
  • Return Type: IActionResult
  • Parameters: [FromRoute] string userName
Add the following Attributes to the method:

HttpGet("{userName}")

Finally, inject the following:

IProfileRepository

Here's the code for the method GetProfileAsync in the ProfilesController.cs file:

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

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

        [HttpGet("{userName}")]
        public async Task<IActionResult> GetProfileAsync([FromRoute]string userName)
        {

        }

        public ProfilesController(IProfileRepository profileRepo, ILogger<ProfilesController> logger)
        {
            ProfileRepo = profileRepo;
            Logger = logger;
        }
    }
}
Modify the Startup.cs file

Configure IProfileRepository for Dependency Injection

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

using ...

namespace Conduit.Api
{
    public class Startup
    {
        public Startup(IConfiguration configuration)...

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var key = Encoding.ASCII.GetBytes(Configuration.GetSection("AppSettings:Token").Value);
            services
                .AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                .AddJwtBearer(options =>...);
            services.AddDbContext<ConduitContext>(options =>...);
            services.AddAutoMapper(cfg =>...);
            services.AddHttpContextAccessor();
            services.AddScoped<IAccountRepository, AccountRepository>();
            services.AddScoped<IProfileRepository, ProfileRepository>(); // <-- Dependency Injection Configuration
            services.AddControllers();
            services.Configure<ApiBehaviorOptions>(options =>...);
            services.AddSwaggerGen(c =>...);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)...
    }
}
 

I finished! On to the next chapter