Return all notifications
This commit is contained in:
parent
e2337ba8fe
commit
7e9d373d68
|
|
@ -1,5 +1,3 @@
|
||||||
using System.IdentityModel.Tokens.Jwt;
|
|
||||||
using System.Security.Claims;
|
|
||||||
using line_gestao_api.Data;
|
using line_gestao_api.Data;
|
||||||
using line_gestao_api.Dtos;
|
using line_gestao_api.Dtos;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
|
@ -24,19 +22,7 @@ public class NotificationsController : ControllerBase
|
||||||
[HttpGet("/notifications")]
|
[HttpGet("/notifications")]
|
||||||
public async Task<ActionResult<List<NotificationDto>>> GetNotifications()
|
public async Task<ActionResult<List<NotificationDto>>> GetNotifications()
|
||||||
{
|
{
|
||||||
var (userId, userName) = GetUserContext();
|
var query = _db.Notifications.AsNoTracking();
|
||||||
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 items = await query
|
var items = await query
|
||||||
.OrderByDescending(n => n.Data)
|
.OrderByDescending(n => n.Data)
|
||||||
|
|
@ -64,19 +50,8 @@ public class NotificationsController : ControllerBase
|
||||||
[HttpPatch("/notifications/{id:guid}/read")]
|
[HttpPatch("/notifications/{id:guid}/read")]
|
||||||
public async Task<IActionResult> MarkAsRead(Guid id)
|
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
|
var notification = await _db.Notifications
|
||||||
.FirstOrDefaultAsync(n => n.Id == id &&
|
.FirstOrDefaultAsync(n => n.Id == id);
|
||||||
((userId != null && n.UserId == userId) ||
|
|
||||||
(userNameNormalized != null && n.Usuario != null &&
|
|
||||||
EF.Functions.ILike(n.Usuario, userNameNormalized))));
|
|
||||||
|
|
||||||
if (notification is null)
|
if (notification is null)
|
||||||
{
|
{
|
||||||
|
|
@ -93,18 +68,4 @@ public class NotificationsController : ControllerBase
|
||||||
return NoContent();
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue