54 lines
1.8 KiB
C#
54 lines
1.8 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Nexus.Api.Data;
|
|
using Nexus.Api.Models;
|
|
using Nexus.Api.Services;
|
|
|
|
namespace Nexus.Api.Controllers;
|
|
|
|
[Authorize]
|
|
[ApiController]
|
|
[Route("api/dashboard/notifications")]
|
|
public class NotificationsController(INotificationService notificationService) : ControllerBase
|
|
{
|
|
[HttpGet]
|
|
public async Task<ActionResult<List<NotificationDto>>> GetNotifications(
|
|
[FromQuery] string forUser = "bao",
|
|
[FromQuery] int limit = 50,
|
|
[FromQuery] bool unreadOnly = false,
|
|
CancellationToken ct = default)
|
|
{
|
|
var notifications = await notificationService.GetForUserAsync(forUser, limit, unreadOnly, ct);
|
|
return Ok(notifications.Select(MapToDto).ToList());
|
|
}
|
|
|
|
[HttpGet("unread-count")]
|
|
public async Task<ActionResult<UnreadCountDto>> GetUnreadCount(
|
|
[FromQuery] string forUser = "bao",
|
|
CancellationToken ct = default)
|
|
{
|
|
var count = await notificationService.GetUnreadCountAsync(forUser, ct);
|
|
return Ok(new UnreadCountDto(count));
|
|
}
|
|
|
|
[HttpPatch("{id:guid}/read")]
|
|
public async Task<ActionResult> MarkAsRead(Guid id, CancellationToken ct = default)
|
|
{
|
|
var ok = await notificationService.MarkAsReadAsync(id, ct);
|
|
return ok ? NoContent() : NotFound(new { error = "Notification not found." });
|
|
}
|
|
|
|
[HttpPatch("read-all")]
|
|
public async Task<ActionResult> MarkAllAsRead(
|
|
[FromQuery] string forUser = "bao",
|
|
CancellationToken ct = default)
|
|
{
|
|
var count = await notificationService.MarkAllAsReadAsync(forUser, ct);
|
|
return Ok(new { marked = count });
|
|
}
|
|
|
|
private static NotificationDto MapToDto(Notification n) => new(
|
|
n.Id, n.Type, n.Title, n.Message,
|
|
n.ForUser, n.TaskId, n.IsRead, n.CreatedAt);
|
|
}
|