Handler.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 Domain.Entities.Store.ValueObject;
  7. using SharedKernel.Results;
  8. using Microsoft.EntityFrameworkCore;
  9. namespace Application.Features.Api.MyPage.ChangeIntro;
  10. internal sealed class Handler(
  11. IAppDbContext db,
  12. ICacheService cache
  13. ) : ICommandHandler<Command, Result>
  14. {
  15. public async Task<Result> Handle(Command request, CancellationToken ct)
  16. {
  17. var intro = request.Intro?.Trim();
  18. if (string.IsNullOrWhiteSpace(intro))
  19. {
  20. return Result.Failure(Error.Problem("MyPage.IntroRequired", "자기소개는 필수입니다."));
  21. }
  22. if (intro.Length > 3000)
  23. {
  24. return Result.Failure(Error.Problem("MyPage.IntroTooLong", "자기소개는 3000자 이하여야 합니다."));
  25. }
  26. var member = await db.Member.FirstOrDefaultAsync(m => m.ID == request.MemberID, ct);
  27. if (member is null)
  28. {
  29. return Result.Failure(Error.NotFound("MyPage.MemberNotFound", "회원 정보를 찾을 수 없습니다."));
  30. }
  31. // 자기소개 변경 주기 확인
  32. var accountConfig = await AccountConfigLoader.GetAccountConfigAsync(cache, db, ct);
  33. if (accountConfig.ChangeIntroDay is > 0 && member.LastIntroChangedAt.HasValue)
  34. {
  35. var nextChangeDate = member.LastIntroChangedAt.Value.AddDays(accountConfig.ChangeIntroDay.Value);
  36. if (DateTime.UtcNow < nextChangeDate)
  37. {
  38. // 쿨다운 내 — 오늘의 한마디 변경권 있으면 소모하여 즉시 변경 허용
  39. if (!await MemberItemConsumer.TryConsumeAsync(db, request.MemberID, ItemKind.TaglineChange, DateTime.UtcNow, "오늘의 한마디 변경권 사용", ct))
  40. {
  41. return Result.Failure(Error.Problem("MyPage.IntroChangeTooSoon",
  42. $"자기소개는 {accountConfig.ChangeIntroDay}일마다 변경 가능합니다. (변경권으로 즉시 변경 가능)"));
  43. }
  44. }
  45. }
  46. var log = MemberIntroChangeLog.Create(request.MemberID, member.Intro, intro);
  47. await db.MemberIntroChangeLog.AddAsync(log, ct);
  48. member.SetIntro(intro);
  49. await db.SaveChangesAsync(ct);
  50. return Result.Success();
  51. }
  52. }