using LiteCharms.Extensions; using LiteCharms.Infrastructure.Database; using LiteCharms.Models; namespace LiteCharms.Features.Notifications.Queries.Handlers; public class GetNotificationQueryHandler(IDbContextFactory contextFactory) : IRequestHandler> { public async ValueTask> Handle(GetNotificationQuery request, CancellationToken cancellationToken) { try { await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); var notification = await context.Notifications.FirstOrDefaultAsync(n => n.Id == request.NotificationId, cancellationToken); return notification is not null ? Result.Ok(notification.ToModel()) : Result.Fail(new Error($"Notification with id {request.NotificationId} not found")); } catch (Exception ex) { return Result.Fail(new Error(ex.Message).CausedBy(ex)); } } }