Handler.cs 883 B

1234567891011121314151617181920212223242526272829
  1. using Application.Abstractions.Data;
  2. using Application.Abstractions.Messaging;
  3. using Microsoft.EntityFrameworkCore;
  4. namespace Application.Features.Api.Notification.MarkRead;
  5. internal sealed class Handler(IAppDbContext db) : ICommandHandler<Command>
  6. {
  7. public async Task Handle(Command request, CancellationToken ct)
  8. {
  9. if (request.NotificationID.HasValue)
  10. {
  11. var notification = await db.Notification.FirstOrDefaultAsync(n => n.ID == request.NotificationID.Value && n.MemberID == request.MemberID, ct);
  12. notification?.MarkRead();
  13. }
  14. else
  15. {
  16. var unread = await db.Notification.Where(n => n.MemberID == request.MemberID && !n.IsRead).ToListAsync(ct);
  17. foreach (var n in unread)
  18. {
  19. n.MarkRead();
  20. }
  21. }
  22. await db.SaveChangesAsync(ct);
  23. }
  24. }