Handler.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using Application.Abstractions.Data;
  2. using Application.Abstractions.Messaging;
  3. using SharedKernel.Results;
  4. using Microsoft.EntityFrameworkCore;
  5. namespace Application.Features.Api.MyPage.UpdateReceiveSettings;
  6. internal sealed class Handler(IAppDbContext db) : ICommandHandler<Command, Result>
  7. {
  8. public async Task<Result> Handle(Command request, CancellationToken ct)
  9. {
  10. var approve = await db.MemberApprove.FirstOrDefaultAsync(a => a.MemberID == request.MemberID, ct);
  11. if (approve is null)
  12. {
  13. return Result.Failure(Error.NotFound("MyPage.MemberNotFound", "회원 정보를 찾을 수 없습니다."));
  14. }
  15. var now = DateTime.UtcNow;
  16. if (approve.IsReceiveSMS != request.IsReceiveSMS)
  17. {
  18. approve.IsReceiveSMS = request.IsReceiveSMS;
  19. approve.ReceiveSMSConsentAt = now;
  20. }
  21. if (approve.IsReceiveEmail != request.IsReceiveEmail)
  22. {
  23. approve.IsReceiveEmail = request.IsReceiveEmail;
  24. approve.ReceiveEmailConsentAt = now;
  25. }
  26. if (approve.IsReceiveNote != request.IsReceiveNote)
  27. {
  28. approve.IsReceiveNote = request.IsReceiveNote;
  29. approve.ReceiveNoteConsentAt = now;
  30. }
  31. await db.SaveChangesAsync(ct);
  32. return Result.Success();
  33. }
  34. }