Added AutoMapper for User related objects
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using AutoMapper;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Net;
|
||||
@ -12,14 +13,16 @@ namespace YABA.API.Controllers
|
||||
[Authorize, Route("api/v{version:apiVersion}/[controller]")]
|
||||
public class UsersController : ControllerBase
|
||||
{
|
||||
private readonly IMapper _mapper;
|
||||
private readonly IUserService _userService;
|
||||
|
||||
public UsersController(IUserService userService)
|
||||
public UsersController(IMapper mapper, IUserService userService)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_userService = userService;
|
||||
}
|
||||
|
||||
[HttpPost("Register")]
|
||||
[HttpPost]
|
||||
[ProducesResponseType(typeof(UserResponse), (int)HttpStatusCode.OK)]
|
||||
[ProducesResponseType((int)HttpStatusCode.NotFound)]
|
||||
[ProducesResponseType((int)HttpStatusCode.NoContent)]
|
||||
@ -34,7 +37,7 @@ namespace YABA.API.Controllers
|
||||
if (isRegistered) return NoContent();
|
||||
|
||||
var registedUser = _userService.RegisterUser(authProviderId);
|
||||
return Ok(new UserResponse(registedUser));
|
||||
return Ok(_mapper.Map<UserResponse>(registedUser));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Versioning;
|
||||
@ -5,6 +6,7 @@ using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using System.Security.Claims;
|
||||
using YABA.API.Middlewares;
|
||||
using YABA.API.Settings;
|
||||
using YABA.API.Settings.Swashbuckle;
|
||||
using YABA.Data.Configuration;
|
||||
@ -46,6 +48,15 @@ builder.Services.AddServiceProjectDependencyInjectionConfiguration(configuration
|
||||
builder.Services.AddDataProjectDependencyInjectionConfiguration(configuration);
|
||||
builder.Services.AddControllers().AddNewtonsoftJson();
|
||||
|
||||
// Add AutoMapper profiles
|
||||
var mapperConfiguration = new MapperConfiguration(mapperConfiguration =>
|
||||
{
|
||||
mapperConfiguration.AddProfile(new YABA.API.Settings.AutoMapperProfile());
|
||||
mapperConfiguration.AddProfile(new YABA.Service.Configuration.AutoMapperProfile());
|
||||
});
|
||||
|
||||
IMapper mapper = mapperConfiguration.CreateMapper();
|
||||
builder.Services.AddSingleton(mapper);
|
||||
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
@ -88,4 +99,7 @@ app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
// Add custom middlewares
|
||||
app.UseMiddleware<AddCustomClaimsMiddleware>();
|
||||
|
||||
app.Run();
|
||||
|
||||
14
YABA.API/Settings/AutoMapperProfile.cs
Normal file
14
YABA.API/Settings/AutoMapperProfile.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using AutoMapper;
|
||||
using YABA.API.ViewModels;
|
||||
using YABA.Common.DTOs;
|
||||
|
||||
namespace YABA.API.Settings
|
||||
{
|
||||
public class AutoMapperProfile : Profile
|
||||
{
|
||||
public AutoMapperProfile()
|
||||
{
|
||||
CreateMap<UserDTO, UserResponse>();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user