Files
Heimdall/src/Heimdall.Web/Controllers/GroupsController.cs
T

133 lines
5.9 KiB
C#
Raw Normal View History

using Heimdall.Application.DTOs;
using Heimdall.Application.Groups.Commands.CreateGroup;
using Heimdall.Application.Groups.Commands.UpdateGroup;
using Heimdall.Application.Groups.Commands.DeleteGroup;
using Heimdall.Application.Groups.Commands.AddUserToGroup;
using Heimdall.Application.Groups.Commands.RemoveUserFromGroup;
using Heimdall.Application.Groups.Queries.GetGroups;
using Heimdall.Application.Groups.Queries.GetGroup;
using Heimdall.Application.Groups.Queries.GetGroupMembers;
using Heimdall.Infrastructure.Security;
using MediatR;
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 IMediator _mediator;
public GroupsController(IMediator mediator) => _mediator = mediator;
/// <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 _mediator.Send(new GetGroupsQuery(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 _mediator.Send(new GetGroupQuery(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 command = new CreateGroupCommand(request.Name, request.Description);
var result = await _mediator.Send(command, 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 command = new UpdateGroupCommand(id, request.Name, request.Description);
var result = await _mediator.Send(command, 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 _mediator.Send(new DeleteGroupCommand(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 _mediator.Send(new AddUserToGroupCommand(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 _mediator.Send(new RemoveUserFromGroupCommand(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 _mediator.Send(new GetGroupMembersQuery(groupId), ct);
return result.IsSuccess
? Ok(result.Data)
: StatusCode(result.StatusCode ?? 400, new { error = result.Error });
}
}