Add Automapper
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
using System.Security.Claims;
|
||||
using AutoMapper;
|
||||
using Imprink.Application.Domains.Users;
|
||||
using Imprink.Application.Users.Dtos;
|
||||
using Imprink.Domain.Models;
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
@@ -9,30 +11,21 @@ namespace Imprink.WebApi.Controllers.Users;
|
||||
|
||||
[ApiController]
|
||||
[Route("/api/users")]
|
||||
public class UsersController(IMediator mediator) : ControllerBase
|
||||
public class UsersController(IMediator mediator, IMapper mapper) : ControllerBase
|
||||
{
|
||||
[Authorize]
|
||||
[HttpPost("me/sync")]
|
||||
public async Task<IActionResult> SyncMyProfile()
|
||||
{
|
||||
var claims = User.Claims as Claim[] ?? User.Claims.ToArray();
|
||||
|
||||
var auth0User = new Auth0User
|
||||
{
|
||||
Sub = claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value ?? string.Empty,
|
||||
Name = claims.FirstOrDefault(c => c.Type == "name")?.Value ?? string.Empty,
|
||||
Nickname = claims.FirstOrDefault(c => c.Type == "nickname")?.Value ?? string.Empty,
|
||||
Email = claims.FirstOrDefault(c => c.Type == ClaimTypes.Email)?.Value ?? string.Empty,
|
||||
EmailVerified = bool.TryParse(claims.FirstOrDefault(c => c.Type == "email_verified")?.Value, out var emailVerified) && emailVerified
|
||||
};
|
||||
var auth0User = mapper.Map<Auth0User>(User);
|
||||
|
||||
await mediator.Send(new SyncUserCommand(auth0User));
|
||||
return Ok("User profile synchronized.");
|
||||
return Ok("Synced");
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpGet("me/roles")]
|
||||
public async Task<IActionResult> GetMyRoles()
|
||||
public async Task<ActionResult<IEnumerable<RoleDto>>> GetMyRoles()
|
||||
{
|
||||
var sub = User.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? string.Empty;
|
||||
return Ok(await mediator.Send(new GetUserRolesCommand(sub)));
|
||||
@@ -40,21 +33,29 @@ public class UsersController(IMediator mediator) : ControllerBase
|
||||
|
||||
[Authorize]
|
||||
[HttpPut("me/phone")]
|
||||
public async Task<IActionResult> UpdateMyPhone([FromBody] SetUserPhoneCommand command)
|
||||
public async Task<ActionResult<UserDto?>> UpdateMyPhone([FromBody] SetUserPhoneCommand command)
|
||||
{
|
||||
return Ok(await mediator.Send(command));
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpPut("me/fullname")]
|
||||
public async Task<IActionResult> UpdateMyFullName([FromBody] SetUserFullNameCommand command)
|
||||
public async Task<ActionResult<UserDto?>> UpdateMyFullName([FromBody] SetUserFullNameCommand command)
|
||||
{
|
||||
return Ok(await mediator.Send(command));
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpGet("roles")]
|
||||
public async Task<ActionResult<UserRoleDto?>> GetAllRoles()
|
||||
{
|
||||
var command = new GetAllRolesCommand();
|
||||
return Ok(await mediator.Send(command));
|
||||
}
|
||||
|
||||
[Authorize(Roles = "Admin")]
|
||||
[HttpPut("{userId}/roles/{roleId:guid}")]
|
||||
public async Task<IActionResult> AddUserRole(string userId, Guid roleId)
|
||||
public async Task<ActionResult<UserRoleDto?>> AddUserRole(string userId, Guid roleId)
|
||||
{
|
||||
var command = new SetUserRoleCommand(userId, roleId);
|
||||
return Ok(await mediator.Send(command));
|
||||
@@ -62,7 +63,7 @@ public class UsersController(IMediator mediator) : ControllerBase
|
||||
|
||||
[Authorize(Roles = "Admin")]
|
||||
[HttpDelete("{userId}/roles/{roleId:guid}")]
|
||||
public async Task<IActionResult> RemoveUserRole(string userId, Guid roleId)
|
||||
public async Task<ActionResult<UserRoleDto?>> RemoveUserRole(string userId, Guid roleId)
|
||||
{
|
||||
var command = new DeleteUserRoleCommand(userId, roleId);
|
||||
return Ok(await mediator.Send(command));
|
||||
|
||||
Reference in New Issue
Block a user