using System.Security.Claims; using LctMonolith.Services; using LctMonolith.Services.Contracts; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace LctMonolith.Controllers; /// /// In-app user notifications API. /// [ApiController] [Route("api/notifications")] [Authorize] public class NotificationsController : ControllerBase { private readonly INotificationService _notifications; public NotificationsController(INotificationService notifications) { _notifications = notifications; } private Guid GetUserId() => Guid.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!); /// Get up to 100 unread notifications. [HttpGet("unread")] public async Task GetUnread(CancellationToken ct) { var list = await _notifications.GetUnreadAsync(GetUserId(), ct); return Ok(list); } /// Get recent notifications (paged by take). [HttpGet] public async Task GetAll([FromQuery] int take = 100, CancellationToken ct = default) { var list = await _notifications.GetAllAsync(GetUserId(), take, ct); return Ok(list); } /// Mark a notification as read. [HttpPost("mark/{id:guid}")] public async Task MarkRead(Guid id, CancellationToken ct) { await _notifications.MarkReadAsync(GetUserId(), id, ct); return NoContent(); } /// Mark all notifications as read. [HttpPost("mark-all")] public async Task MarkAll(CancellationToken ct) { var cnt = await _notifications.MarkAllReadAsync(GetUserId(), ct); return Ok(new { updated = cnt }); } }