55 lines
1.5 KiB
C#
55 lines
1.5 KiB
C#
using GameAuthApi.Models;
|
|
using GameAuthApi.Services;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace GameAuthApi.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class UserController : ControllerBase
|
|
{
|
|
private readonly UserService _userService;
|
|
|
|
public UserController(UserService userService)
|
|
{
|
|
_userService = userService;
|
|
}
|
|
|
|
// GET: /api/User
|
|
[HttpGet]
|
|
[Authorize(Roles = "SUPER")]
|
|
public async Task<IActionResult> GetAllUsers()
|
|
{
|
|
var users = await _userService.GetAllAsync();
|
|
return Ok(users);
|
|
}
|
|
|
|
[HttpPut]
|
|
[Authorize(Roles = "SUPER")]
|
|
public async Task<IActionResult> UpdateUser([FromQuery] string username, [FromBody] UpdateUserRequest req)
|
|
{
|
|
var user = await _userService.GetByUsernameAsync(username);
|
|
if (user == null)
|
|
return NotFound($"User '{username}' not found.");
|
|
|
|
user.Role = req.Role;
|
|
user.Email = req.Email;
|
|
await _userService.UpdateAsync(user);
|
|
return Ok(user);
|
|
}
|
|
|
|
// DELETE: /api/User?username=foo
|
|
[HttpDelete]
|
|
[Authorize(Roles = "SUPER")]
|
|
public async Task<IActionResult> DeleteUser([FromQuery] string username)
|
|
{
|
|
var deleted = await _userService.DeleteAsync(username);
|
|
if (!deleted)
|
|
return NotFound($"User '{username}' not found.");
|
|
|
|
return Ok($"User '{username}' deleted successfully.");
|
|
}
|
|
}
|