Handler.cs 1.1 KB

12345678910111213141516171819202122232425262728
  1. using Application.Abstractions.Data;
  2. using Application.Abstractions.Messaging;
  3. using SharedKernel.Results;
  4. using Microsoft.EntityFrameworkCore;
  5. namespace Application.Features.Api.DonationRemote.MarkDelivered;
  6. internal sealed class Handler(IAppDbContext db) : ICommandHandler<Command, Result<Response>>
  7. {
  8. public async Task<Result<Response>> Handle(Command r, CancellationToken ct)
  9. {
  10. var alert = await db.DonationAlert.Include(a => a.Donation).ThenInclude(d => d!.Channel).FirstOrDefaultAsync(a => a.ID == r.AlertID, ct);
  11. if (alert is null || alert.Donation is null || alert.Donation.Channel is null)
  12. {
  13. return Result.Failure<Response>(Error.NotFound("DonationAlert.NotFound", "알림을 찾을 수 없습니다."));
  14. }
  15. if (alert.Donation.Channel.MemberID != r.RequesterMemberID)
  16. {
  17. return Result.Failure<Response>(Error.Forbidden("Remote.Forbidden", "권한이 없습니다."));
  18. }
  19. alert.MarkDelivered();
  20. await db.SaveChangesAsync(ct);
  21. return Result.Success(new Response(alert.ID, alert.Donation.Channel.WidgetToken!));
  22. }
  23. }