using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using StickyBoard.Api.Common; using StickyBoard.Core.DTOs.Common; using StickyBoard.Core.DTOs.SocialAndMessaging; using StickyBoard.Core.Services.SocialAndMessaging; using StickyBoard.Core.Services.SocialAndMessaging.Contracts; namespace StickyBoard.Api.Controllers; [ApiController] [Route("api/[controller]")] [Authorize] public sealed class MessagesController : ControllerBase { private readonly IMessageService _messages; public MessagesController(MessageService messages) { _messages = messages; } // ------------------------------------------------------------ // CREATE // ------------------------------------------------------------ [HttpPost] public async Task>> Create(MessageCreateDto dto, CancellationToken ct) { var userId = User.GetUserId(); if (userId == Guid.Empty) return Unauthorized(ApiResponseDto.Fail("Invalid or missing token.")); var msg = await _messages.CreateAsync(userId, dto, ct); return Ok(ApiResponseDto.Ok(msg)); } // ------------------------------------------------------------ // UPDATE (sender only) // ------------------------------------------------------------ [HttpPut("{id:guid}")] public async Task>> Update(Guid id, MessageUpdateDto dto, CancellationToken ct) { var userId = User.GetUserId(); if (userId == Guid.Empty) return Unauthorized(ApiResponseDto.Fail("Invalid or missing token.")); var ok = await _messages.UpdateAsync(id, userId, dto, ct); return ok ? Ok(ApiResponseDto.Ok(new { success = true })) : NotFound(ApiResponseDto.Fail("Message not found or not your message.")); } // ------------------------------------------------------------ // DELETE // ------------------------------------------------------------ [HttpDelete("{id:guid}")] public async Task>> Delete(Guid id, CancellationToken ct) { var ok = await _messages.DeleteAsync(id, ct); return ok ? Ok(ApiResponseDto.Ok(new { success = true })) : NotFound(ApiResponseDto.Fail("Message not found.")); } // ------------------------------------------------------------ // GET BY BOARD // ------------------------------------------------------------ [HttpGet("board/{boardId:guid}")] public async Task>>> GetByBoard(Guid boardId, CancellationToken ct) { var list = await _messages.GetByBoardAsync(boardId, ct); return Ok(ApiResponseDto>.Ok(list)); } // ------------------------------------------------------------ // GET BY VIEW // ------------------------------------------------------------ [HttpGet("view/{viewId:guid}")] public async Task>>> GetByView(Guid viewId, CancellationToken ct) { var list = await _messages.GetByViewAsync(viewId, ct); return Ok(ApiResponseDto>.Ok(list)); } // ------------------------------------------------------------ // GET BY ID // ------------------------------------------------------------ [HttpGet("{id:guid}")] public async Task>> Get(Guid id, CancellationToken ct) { var msg = await _messages.GetAsync(id, ct); return msg is not null ? Ok(ApiResponseDto.Ok(msg)) : NotFound(ApiResponseDto.Fail("Message not found.")); } }