Adding User Controller and Delete endpoint

This commit is contained in:
hz
2025-05-13 21:42:17 -05:00
parent a923dfadc2
commit 98cb301acd
2 changed files with 46 additions and 0 deletions
+40
View File
@@ -0,0 +1,40 @@
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);
}
// 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.");
}
}
+6
View File
@@ -23,6 +23,12 @@ public class UserService
public async Task<List<User>> GetAllAsync() =>
await _users.Find(_ => true).ToListAsync();
public async Task<bool> DeleteAsync(string username)
{
var result = await _users.DeleteOneAsync(u => u.Username == username);
return result.DeletedCount > 0;
}
public async Task CreateAsync(User user) =>
await _users.InsertOneAsync(user);