123 lines
5.2 KiB
C#
123 lines
5.2 KiB
C#
using Heimdall.Application.DTOs;
|
|||
|
|
using Heimdall.Application.Services;
|
||
|
|
using Heimdall.Infrastructure.Security;
|
||
|
|
using Microsoft.AspNetCore.Authorization;
|
||
|
|
using Microsoft.AspNetCore.Mvc;
|
||
|
|
using Microsoft.AspNetCore.RateLimiting;
|
||
|
|
|
||
|
|
namespace Heimdall.Api.Controllers;
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// CRUD management for user groups and their members (Admin only).
|
||
|
|
/// </summary>
|
||
|
|
[ApiController]
|
||
|
|
[Route("api/groups")]
|
||
|
|
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.AdminUsers)]
|
||
|
|
[EnableRateLimiting("Api")]
|
||
|
|
public class GroupsController : ControllerBase
|
||
|
|
{
|
||
|
|
private readonly IGroupService _groupService;
|
||
|
|
|
||
|
|
public GroupsController(IGroupService groupService) => _groupService = groupService;
|
||
|
|
|
||
|
|
/// <summary>List all groups (Admin only).</summary>
|
||
|
|
[HttpGet]
|
||
|
|
[ProducesResponseType(typeof(PagedResult<GroupResponse>), StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||
|
|
public async Task<IActionResult> List(
|
||
|
|
[FromQuery] int page = 1, [FromQuery] int pageSize = 20, CancellationToken ct = default)
|
||
|
|
{
|
||
|
|
var result = await _groupService.ListAsync(page, pageSize, ct);
|
||
|
|
if (!result.IsSuccess)
|
||
|
|
return StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
||
|
|
return Ok(result.Data);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Get a group by ID.</summary>
|
||
|
|
[HttpGet("{id:guid}")]
|
||
|
|
[ProducesResponseType(typeof(GroupResponse), StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
||
|
|
public async Task<IActionResult> GetById(Guid id, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var result = await _groupService.GetByIdAsync(id, ct);
|
||
|
|
return result.IsSuccess
|
||
|
|
? Ok(result.Data)
|
||
|
|
: NotFound(new { error = result.Error });
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Create a new group (Admin only).</summary>
|
||
|
|
[HttpPost]
|
||
|
|
[ProducesResponseType(typeof(GroupResponse), StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
||
|
|
public async Task<IActionResult> Create([FromBody] CreateGroupRequest request, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var result = await _groupService.CreateAsync(request, ct);
|
||
|
|
return result.IsSuccess
|
||
|
|
? Ok(result.Data)
|
||
|
|
: StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Update a group (Admin only).</summary>
|
||
|
|
[HttpPut("{id:guid}")]
|
||
|
|
[ProducesResponseType(typeof(GroupResponse), StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
||
|
|
public async Task<IActionResult> Update(
|
||
|
|
Guid id, [FromBody] UpdateGroupRequest request, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var result = await _groupService.UpdateAsync(id, request, ct);
|
||
|
|
return result.IsSuccess
|
||
|
|
? Ok(result.Data)
|
||
|
|
: StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Delete a group (soft-delete, Admin only).</summary>
|
||
|
|
[HttpDelete("{id:guid}")]
|
||
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
||
|
|
public async Task<IActionResult> Delete(Guid id, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var result = await _groupService.DeleteAsync(id, ct);
|
||
|
|
return result.IsSuccess
|
||
|
|
? Ok()
|
||
|
|
: StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Add a user to a group (Admin only).</summary>
|
||
|
|
[HttpPost("{groupId:guid}/users/{userId:guid}")]
|
||
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
||
|
|
public async Task<IActionResult> AddUser(Guid groupId, Guid userId, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var result = await _groupService.AddUserAsync(groupId, userId, ct);
|
||
|
|
return result.IsSuccess
|
||
|
|
? Ok()
|
||
|
|
: StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Remove a user from a group (Admin only).</summary>
|
||
|
|
[HttpDelete("{groupId:guid}/users/{userId:guid}")]
|
||
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
||
|
|
public async Task<IActionResult> RemoveUser(Guid groupId, Guid userId, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var result = await _groupService.RemoveUserAsync(groupId, userId, ct);
|
||
|
|
return result.IsSuccess
|
||
|
|
? Ok()
|
||
|
|
: StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Get all members of a group (Admin only).</summary>
|
||
|
|
[HttpGet("{groupId:guid}/users")]
|
||
|
|
[ProducesResponseType(typeof(IReadOnlyList<GroupMemberResponse>), StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
||
|
|
public async Task<IActionResult> GetMembers(Guid groupId, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var result = await _groupService.GetMembersAsync(groupId, ct);
|
||
|
|
return result.IsSuccess
|
||
|
|
? Ok(result.Data)
|
||
|
|
: StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
||
|
|
}
|
||
|
|
}
|