Add user manager page (#22)
Reviewed-on: #22
This commit was merged in pull request #22.
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
using GameIdeas.Shared.Dto;
|
||||
using GameIdeas.Shared.Constants;
|
||||
using GameIdeas.Shared.Dto;
|
||||
using GameIdeas.WebAPI.Exceptions;
|
||||
using GameIdeas.WebAPI.Services.Users;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace GameIdeas.WebAPI.Controllers;
|
||||
@@ -8,7 +10,8 @@ namespace GameIdeas.WebAPI.Controllers;
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class UserController(
|
||||
IUserService userService,
|
||||
IUserReadService userReadService,
|
||||
IUserWriteService userWriteService,
|
||||
ILoggerFactory loggerFactory) : Controller
|
||||
{
|
||||
private readonly ILogger<UserController> logger = loggerFactory.CreateLogger<UserController>();
|
||||
@@ -18,7 +21,7 @@ public class UserController(
|
||||
{
|
||||
try
|
||||
{
|
||||
return Ok(await userService.Login(model));
|
||||
return Ok(await userReadService.Login(model));
|
||||
}
|
||||
catch (UserInvalidException e)
|
||||
{
|
||||
@@ -36,4 +39,82 @@ public class UserController(
|
||||
return StatusCode(500, e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
[Authorize(Roles = GlobalConstants.ADMINISTRATOR)]
|
||||
[HttpGet("Roles")]
|
||||
public async Task<ActionResult<IEnumerable<RoleDto>>> GetRoles()
|
||||
{
|
||||
try
|
||||
{
|
||||
return Ok(await userReadService.GetRoles());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.LogError(e, "Internal error while get roles");
|
||||
return StatusCode(500, e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
[Authorize(Roles = GlobalConstants.ADMINISTRATOR)]
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<UserListDto>> GetUsers([FromQuery] UserFilterDto filter)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Ok(await userReadService.GetUsers(filter));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.LogError(e, "Internal error while get users");
|
||||
return StatusCode(500, e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
[Authorize(Roles = GlobalConstants.ADMINISTRATOR)]
|
||||
[HttpPost("Create")]
|
||||
public async Task<ActionResult<IdDto>> CreateUser([FromBody] UserDto user)
|
||||
{
|
||||
try
|
||||
{
|
||||
var id = new IdDto() { Id = await userWriteService.CreateUser(user) };
|
||||
return Created("/Create", id);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.LogError(e, "Internal error while create user");
|
||||
return StatusCode(500, e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
[Authorize(Roles = GlobalConstants.ADMINISTRATOR)]
|
||||
[HttpPut("Update/{userId}")]
|
||||
public async Task<ActionResult<IdDto>> UpdateUser(string userId, [FromBody] UserDto user)
|
||||
{
|
||||
try
|
||||
{
|
||||
var id = new IdDto() { Id = await userWriteService.UpdateUser(userId, user) };
|
||||
return Created("/Update", id);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.LogError(e, "Internal error while update user");
|
||||
return StatusCode(500, e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
[Authorize(Roles = GlobalConstants.ADMINISTRATOR)]
|
||||
[HttpDelete("Delete/{userId}")]
|
||||
public async Task<ActionResult<IdDto>> DeleteUser(string userId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var id = new IdDto() { Id = await userWriteService.DeleteUser(userId) };
|
||||
return Created("/Delete", id);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.LogError(e, "Internal error while delete user");
|
||||
return StatusCode(500, e.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user