Return all notifications

This commit is contained in:
Eduardo Lopes 2026-01-22 16:18:20 -03:00
parent e2337ba8fe
commit 7e9d373d68
1 changed files with 2 additions and 41 deletions

View File

@ -1,5 +1,3 @@
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using line_gestao_api.Data;
using line_gestao_api.Dtos;
using Microsoft.AspNetCore.Authorization;
@ -24,19 +22,7 @@ public class NotificationsController : ControllerBase
[HttpGet("/notifications")]
public async Task<ActionResult<List<NotificationDto>>> GetNotifications()
{
var (userId, userName) = GetUserContext();
if (userId is null && string.IsNullOrWhiteSpace(userName))
{
return Unauthorized();
}
var userNameNormalized = userName?.Trim();
var query = _db.Notifications.AsNoTracking()
.Where(n =>
(userId != null && n.UserId == userId) ||
(userNameNormalized != null && n.Usuario != null &&
EF.Functions.ILike(n.Usuario, userNameNormalized)));
var query = _db.Notifications.AsNoTracking();
var items = await query
.OrderByDescending(n => n.Data)
@ -64,19 +50,8 @@ public class NotificationsController : ControllerBase
[HttpPatch("/notifications/{id:guid}/read")]
public async Task<IActionResult> MarkAsRead(Guid id)
{
var (userId, userName) = GetUserContext();
if (userId is null && string.IsNullOrWhiteSpace(userName))
{
return Unauthorized();
}
var userNameNormalized = userName?.Trim();
var notification = await _db.Notifications
.FirstOrDefaultAsync(n => n.Id == id &&
((userId != null && n.UserId == userId) ||
(userNameNormalized != null && n.Usuario != null &&
EF.Functions.ILike(n.Usuario, userNameNormalized))));
.FirstOrDefaultAsync(n => n.Id == id);
if (notification is null)
{
@ -93,18 +68,4 @@ public class NotificationsController : ControllerBase
return NoContent();
}
private (Guid? UserId, string? UserName) GetUserContext()
{
var userIdRaw = User.FindFirstValue(JwtRegisteredClaimNames.Sub)
?? User.FindFirstValue(ClaimTypes.NameIdentifier);
Guid? userId = null;
if (Guid.TryParse(userIdRaw, out var parsed))
{
userId = parsed;
}
var userName = User.FindFirstValue("name");
return (userId, userName);
}
}