72 lines
1.8 KiB
C#
72 lines
1.8 KiB
C#
using line_gestao_api.Data;
|
|
using line_gestao_api.Dtos;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace line_gestao_api.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("api/notifications")]
|
|
[Authorize]
|
|
public class NotificationsController : ControllerBase
|
|
{
|
|
private readonly AppDbContext _db;
|
|
|
|
public NotificationsController(AppDbContext db)
|
|
{
|
|
_db = db;
|
|
}
|
|
|
|
[HttpGet]
|
|
[HttpGet("/notifications")]
|
|
public async Task<ActionResult<List<NotificationDto>>> GetNotifications()
|
|
{
|
|
var query = _db.Notifications.AsNoTracking();
|
|
|
|
var items = await query
|
|
.OrderByDescending(n => n.Data)
|
|
.Select(n => new NotificationDto
|
|
{
|
|
Id = n.Id,
|
|
Tipo = n.Tipo,
|
|
Titulo = n.Titulo,
|
|
Mensagem = n.Mensagem,
|
|
Data = n.Data,
|
|
ReferenciaData = n.ReferenciaData,
|
|
DiasParaVencer = n.DiasParaVencer,
|
|
Lida = n.Lida,
|
|
LidaEm = n.LidaEm,
|
|
VigenciaLineId = n.VigenciaLineId,
|
|
Cliente = n.Cliente,
|
|
Linha = n.Linha
|
|
})
|
|
.ToListAsync();
|
|
|
|
return Ok(items);
|
|
}
|
|
|
|
[HttpPatch("{id:guid}/read")]
|
|
[HttpPatch("/notifications/{id:guid}/read")]
|
|
public async Task<IActionResult> MarkAsRead(Guid id)
|
|
{
|
|
var notification = await _db.Notifications
|
|
.FirstOrDefaultAsync(n => n.Id == id);
|
|
|
|
if (notification is null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
if (!notification.Lida)
|
|
{
|
|
notification.Lida = true;
|
|
notification.LidaEm = DateTime.UtcNow;
|
|
await _db.SaveChangesAsync();
|
|
}
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
}
|