using Distribution.Infra.Authorization; using Microsoft.AspNetCore.Mvc; namespace Distribution.Features.Notifications; /// Every endpoint acts on the signed-in user's own notifications only. [ApiController] [Route("api/notifications")] public class NotificationController(NotificationService service, ICurrentUserService currentUser) : ControllerBase { [HttpGet] public async Task GetInbox( [FromQuery] bool unreadOnly = false, [FromQuery] int page = 1, [FromQuery] int pageSize = 20) { if (currentUser.UserId is not { } userId) return Unauthorized(); return Ok(await service.GetInboxAsync(userId, unreadOnly, page, pageSize)); } [HttpGet("unread-count")] public async Task GetUnreadCount() { if (currentUser.UserId is not { } userId) return Unauthorized(); return Ok(await service.GetUnreadCountAsync(userId)); } [HttpPatch("{id:guid}/read")] public async Task MarkRead(Guid id, CancellationToken ct) { if (currentUser.UserId is not { } userId) return Unauthorized(); try { await service.MarkReadAsync(userId, id, ct); return NoContent(); } catch (KeyNotFoundException ex) { return NotFound(new { error = ex.Message }); } } [HttpPatch("read-all")] public async Task MarkAllRead(CancellationToken ct) { if (currentUser.UserId is not { } userId) return Unauthorized(); await service.MarkAllReadAsync(userId, ct); return NoContent(); } [HttpDelete("{id:guid}")] public async Task Delete(Guid id, CancellationToken ct) { if (currentUser.UserId is not { } userId) return Unauthorized(); try { await service.DeleteAsync(userId, id, ct); return NoContent(); } catch (KeyNotFoundException ex) { return NotFound(new { error = ex.Message }); } } }