using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using StickyBoard.Api.Common; using StickyBoard.Core.DTOs.BoardsAndCards; using StickyBoard.Core.DTOs.Common; using StickyBoard.Core.Models; using StickyBoard.Core.Services.BoardsAndCards.Contracts; namespace StickyBoard.Api.Controllers; [ApiController] [Route("api/workspaces")] [Authorize] public sealed class WorkspacesController : ControllerBase { private readonly IWorkspaceService _service; public WorkspacesController(IWorkspaceService service) { _service = service; } // ------------------------------------------------------------ // CREATE // ------------------------------------------------------------ [HttpPost] public async Task>> Create( [FromBody] WorkspaceCreateDto dto, CancellationToken ct) { var userId = User.GetUserId(); if (userId == Guid.Empty) return Unauthorized(ApiResponseDto.Fail("Invalid or missing token.")); var result = await _service.CreateAsync(userId, dto, ct); return Ok(ApiResponseDto.Ok(result)); } // ------------------------------------------------------------ // GET FOR CURRENT USER // ------------------------------------------------------------ [HttpGet] public async Task>>> GetForUser(CancellationToken ct) { var userId = User.GetUserId(); if (userId == Guid.Empty) return Unauthorized(ApiResponseDto>.Fail("Invalid or missing token.")); var result = await _service.GetForUserAsync(userId, ct); return Ok(ApiResponseDto>.Ok(result)); } // ------------------------------------------------------------ // RENAME // ------------------------------------------------------------ [HttpPut("{id:guid}/name")] public async Task>> Rename( Guid id, [FromBody] WorkspaceRenameDto dto, CancellationToken ct) { await _service.RenameAsync(id, dto.Name, ct); return Ok(ApiResponseDto.Ok(new { success = true })); } // ------------------------------------------------------------ // ADD MEMBER // ------------------------------------------------------------ [HttpPost("{id:guid}/members")] public async Task>> AddMember( Guid id, [FromBody] WorkspaceAddMemberDto dto, CancellationToken ct) { await _service.AddMemberAsync(id, dto.UserId, dto.Role, ct); return Ok(ApiResponseDto.Ok(new { success = true })); } // ------------------------------------------------------------ // REMOVE MEMBER // ------------------------------------------------------------ [HttpDelete("{id:guid}/members/{userId:guid}")] public async Task>> RemoveMember( Guid id, Guid userId, CancellationToken ct) { await _service.RemoveMemberAsync(id, userId, ct); return Ok(ApiResponseDto.Ok(new { success = true })); } // ------------------------------------------------------------ // CHANGE ROLE // ------------------------------------------------------------ [HttpPut("{id:guid}/members/{userId:guid}/role")] public async Task>> ChangeRole( Guid id, Guid userId, [FromQuery] WorkspaceRole role, CancellationToken ct) { await _service.ChangeRoleAsync(id, userId, role, ct); return Ok(ApiResponseDto.Ok(new { success = true })); } // ------------------------------------------------------------ // GET MEMBERS // ------------------------------------------------------------ [HttpGet("{id:guid}/members")] public async Task>>> GetMembers( Guid id, CancellationToken ct) { var members = await _service.GetMembersAsync(id, ct); return Ok(ApiResponseDto>.Ok(members)); } // ------------------------------------------------------------ // GET USER ROLE (convenience) // ------------------------------------------------------------ [HttpGet("{id:guid}/members/{userId:guid}/role")] public async Task>> GetUserRole( Guid id, Guid userId, CancellationToken ct) { var role = await _service.GetUserRoleAsync(id, userId, ct); return role is null ? NotFound(ApiResponseDto.Fail("User is not a member of this workspace.")) : Ok(ApiResponseDto.Ok(role.Value)); } // ------------------------------------------------------------ // DELETE (soft) // ------------------------------------------------------------ [HttpDelete("{id:guid}")] public async Task>> Delete(Guid id, CancellationToken ct) { await _service.DeleteAsync(id, ct); return Ok(ApiResponseDto.Ok(new { success = true })); } }