40 lines
1.1 KiB
C#
40 lines
1.1 KiB
C#
using Socialize.Infrastructure.Security;
|
|
using Socialize.Modules.Notifications.Data;
|
|
|
|
namespace Socialize.Modules.Notifications.Handlers;
|
|
|
|
public class MarkNotificationAsReadHandler(
|
|
AppDbContext dbContext,
|
|
AccessScopeService accessScopeService)
|
|
: EndpointWithoutRequest
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Post("/api/notifications/{id}/read");
|
|
Options(o => o.WithTags("Notifications"));
|
|
}
|
|
|
|
public override async Task HandleAsync(CancellationToken ct)
|
|
{
|
|
Guid id = Route<Guid>("id");
|
|
|
|
NotificationEvent? notificationEvent = await dbContext.NotificationEvents.SingleOrDefaultAsync(candidate => candidate.Id == id, ct);
|
|
if (notificationEvent is null)
|
|
{
|
|
await SendNotFoundAsync(ct);
|
|
return;
|
|
}
|
|
|
|
if (!accessScopeService.CanAccessWorkspace(User, notificationEvent.WorkspaceId))
|
|
{
|
|
await SendForbiddenAsync(ct);
|
|
return;
|
|
}
|
|
|
|
notificationEvent.ReadAt = notificationEvent.ReadAt ?? DateTimeOffset.UtcNow;
|
|
await dbContext.SaveChangesAsync(ct);
|
|
|
|
await SendNoContentAsync(ct);
|
|
}
|
|
}
|