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; /// /// CRUD management for user groups and their members (Admin only). /// [ApiController] [Route("api/groups")] [Authorize(AuthenticationSchemes = "JwtBearer,Cookies", Policy = AppPermissions.AdminUsers)] [EnableRateLimiting("Api")] public class GroupsController : ControllerBase { private readonly IMediator _mediator; public GroupsController(IMediator mediator) => _mediator = mediator; /// List all groups (Admin only). [HttpGet] [ProducesResponseType(typeof(PagedResult), StatusCodes.Status200OK)] [ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)] public async Task 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); } /// Get a group by ID. [HttpGet("{id:guid}")] [ProducesResponseType(typeof(GroupResponse), StatusCodes.Status200OK)] [ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)] public async Task 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 }); } /// Create a new group (Admin only). [HttpPost] [ProducesResponseType(typeof(GroupResponse), StatusCodes.Status200OK)] [ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)] public async Task 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 }); } /// Update a group (Admin only). [HttpPut("{id:guid}")] [ProducesResponseType(typeof(GroupResponse), StatusCodes.Status200OK)] [ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)] [ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)] public async Task 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 }); } /// Delete a group (soft-delete, Admin only). [HttpDelete("{id:guid}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)] public async Task 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 }); } /// Add a user to a group (Admin only). [HttpPost("{groupId:guid}/users/{userId:guid}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)] [ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)] public async Task 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 }); } /// Remove a user from a group (Admin only). [HttpDelete("{groupId:guid}/users/{userId:guid}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)] [ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)] public async Task 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 }); } /// Get all members of a group (Admin only). [HttpGet("{groupId:guid}/users")] [ProducesResponseType(typeof(IReadOnlyList), StatusCodes.Status200OK)] [ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)] public async Task 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 }); } }