102 lines
4.4 KiB
C#
102 lines
4.4 KiB
C#
using Heimdall.Application.DTOs;
|
|||
|
|
using Heimdall.Application.Notifications.Commands.MarkAllAsRead;
|
||
|
|
using Heimdall.Application.Notifications.Commands.MarkAsRead;
|
||
|
|
using Heimdall.Application.Notifications.Commands.UpdateConfig;
|
||
|
|
using Heimdall.Application.Notifications.Queries.GetConfig;
|
||
|
|
using Heimdall.Application.Notifications.Queries.GetNotifications;
|
||
|
|
using Heimdall.Application.Notifications.Queries.GetUnreadCount;
|
||
|
|
using Heimdall.Infrastructure.Security;
|
||
|
|
using MediatR;
|
||
|
|
using Microsoft.AspNetCore.Authorization;
|
||
|
|
using Microsoft.AspNetCore.Mvc;
|
||
|
|
using Microsoft.AspNetCore.RateLimiting;
|
||
|
|
using System.Security.Claims;
|
||
|
|
|
||
|
|
namespace Heimdall.Api.Controllers;
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// In-app notifications for users — list, mark read, configure notification preferences.
|
||
|
|
/// </summary>
|
||
|
|
[ApiController]
|
||
|
|
[Route("api/notifications")]
|
||
|
|
[EnableRateLimiting("Api")]
|
||
|
|
[Authorize(AuthenticationSchemes = "JwtBearer")]
|
||
|
|
public class NotificationsController : ControllerBase
|
||
|
|
{
|
||
|
|
private readonly IMediator _mediator;
|
||
|
|
|
||
|
|
public NotificationsController(IMediator mediator) => _mediator = mediator;
|
||
|
|
|
||
|
|
private Guid GetUserId() =>
|
||
|
|
Guid.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!);
|
||
|
|
|
||
|
|
/// <summary>List notifications for the current user.</summary>
|
||
|
|
[HttpGet]
|
||
|
|
[ProducesResponseType(typeof(PagedResult<NotificationDto>), StatusCodes.Status200OK)]
|
||
|
|
public async Task<IActionResult> GetNotifications(
|
||
|
|
[FromQuery] bool? isRead,
|
||
|
|
[FromQuery] int page = 1,
|
||
|
|
[FromQuery] int pageSize = 20,
|
||
|
|
CancellationToken ct = default)
|
||
|
|
{
|
||
|
|
var result = await _mediator.Send(new GetNotificationsQuery(GetUserId(), isRead, page, pageSize), ct);
|
||
|
|
return Ok(result);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Get unread notification count.</summary>
|
||
|
|
[HttpGet("unread-count")]
|
||
|
|
[ProducesResponseType(typeof(UnreadCountDto), StatusCodes.Status200OK)]
|
||
|
|
public async Task<IActionResult> GetUnreadCount(CancellationToken ct = default)
|
||
|
|
{
|
||
|
|
var result = await _mediator.Send(new GetUnreadCountQuery(GetUserId()), ct);
|
||
|
|
return result.IsSuccess ? Ok(result.Data) : StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Mark a single notification as read.</summary>
|
||
|
|
[HttpPost("{id:guid}/read")]
|
||
|
|
[ProducesResponseType(typeof(MarkAsReadResponse), StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
||
|
|
public async Task<IActionResult> MarkAsRead(Guid id, CancellationToken ct = default)
|
||
|
|
{
|
||
|
|
var result = await _mediator.Send(new MarkAsReadCommand(id, GetUserId()), ct);
|
||
|
|
return result.IsSuccess ? Ok(result.Data) : StatusCode(result.StatusCode ?? 404, new { error = result.Error });
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Mark all notifications as read.</summary>
|
||
|
|
[HttpPost("mark-all-read")]
|
||
|
|
[ProducesResponseType(typeof(MarkAsReadResponse), StatusCodes.Status200OK)]
|
||
|
|
public async Task<IActionResult> MarkAllAsRead(CancellationToken ct = default)
|
||
|
|
{
|
||
|
|
var result = await _mediator.Send(new MarkAllAsReadCommand(GetUserId()), ct);
|
||
|
|
return Ok(result.Data);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Get notification configuration for the current user.</summary>
|
||
|
|
[HttpGet("config")]
|
||
|
|
[ProducesResponseType(typeof(UserNotificationConfigDto), StatusCodes.Status200OK)]
|
||
|
|
public async Task<IActionResult> GetConfig(CancellationToken ct = default)
|
||
|
|
{
|
||
|
|
var result = await _mediator.Send(new GetNotificationConfigQuery(GetUserId()), ct);
|
||
|
|
return result.IsSuccess ? Ok(result.Data) : StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Update notification configuration for the current user.</summary>
|
||
|
|
[HttpPut("config")]
|
||
|
|
[ProducesResponseType(typeof(UserNotificationConfigDto), StatusCodes.Status200OK)]
|
||
|
|
public async Task<IActionResult> UpdateConfig([FromBody] UpdateNotificationConfigRequest request, CancellationToken ct = default)
|
||
|
|
{
|
||
|
|
var command = new UpdateNotificationConfigCommand(
|
||
|
|
GetUserId(),
|
||
|
|
request.EmailEnabled,
|
||
|
|
request.EmailAddress,
|
||
|
|
request.GotifyEnabled,
|
||
|
|
request.GotifyUrl,
|
||
|
|
request.GotifyToken,
|
||
|
|
request.Language,
|
||
|
|
request.DailyDigest,
|
||
|
|
request.WeeklyDigest,
|
||
|
|
request.SubscribedEvents);
|
||
|
|
var result = await _mediator.Send(command, ct);
|
||
|
|
return result.IsSuccess ? Ok(result.Data) : StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
||
|
|
}
|
||
|
|
}
|