Handler.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using Application.Abstractions.Data;
  2. using Application.Abstractions.Cache;
  3. using Application.Abstractions.Messaging;
  4. using Application.Helpers;
  5. using Domain.Entities.Members.Logs;
  6. using SharedKernel.Results;
  7. using Microsoft.EntityFrameworkCore;
  8. namespace Application.Features.Api.MyPage.ChangeIntro;
  9. internal sealed class Handler(
  10. IAppDbContext db,
  11. ICacheService cache
  12. ) : ICommandHandler<Command, Result>
  13. {
  14. public async Task<Result> Handle(Command request, CancellationToken ct)
  15. {
  16. var intro = request.Intro?.Trim();
  17. if (string.IsNullOrWhiteSpace(intro))
  18. {
  19. return Result.Failure(Error.Problem("MyPage.IntroRequired", "자기소개는 필수입니다."));
  20. }
  21. if (intro.Length > 3000)
  22. {
  23. return Result.Failure(Error.Problem("MyPage.IntroTooLong", "자기소개는 3000자 이하여야 합니다."));
  24. }
  25. var member = await db.Member.FirstOrDefaultAsync(m => m.ID == request.MemberID, ct);
  26. if (member is null)
  27. {
  28. return Result.Failure(Error.NotFound("MyPage.MemberNotFound", "회원 정보를 찾을 수 없습니다."));
  29. }
  30. // 자기소개 변경 주기 확인
  31. var accountConfig = await AccountConfigLoader.GetAccountConfigAsync(cache, db, ct);
  32. if (accountConfig.ChangeIntroDay is > 0 && member.LastIntroChangedAt.HasValue)
  33. {
  34. var nextChangeDate = member.LastIntroChangedAt.Value.AddDays(accountConfig.ChangeIntroDay.Value);
  35. if (DateTime.UtcNow < nextChangeDate)
  36. {
  37. return Result.Failure(Error.Problem("MyPage.IntroChangeTooSoon",
  38. $"자기소개는 {accountConfig.ChangeIntroDay}일마다 변경 가능합니다."));
  39. }
  40. }
  41. var log = MemberIntroChangeLog.Create(request.MemberID, member.Intro, intro);
  42. await db.MemberIntroChangeLog.AddAsync(log, ct);
  43. member.SetIntro(intro);
  44. await db.SaveChangesAsync(ct);
  45. return Result.Success();
  46. }
  47. }