Handler.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using Application.Abstractions.Data;
  2. using Application.Abstractions.Messaging;
  3. using Domain.Entities.Members.Logs;
  4. using SharedKernel.Results;
  5. using Microsoft.EntityFrameworkCore;
  6. namespace Application.Features.Api.MyPage.ChangeIntro;
  7. internal sealed class Handler(IAppDbContext db) : ICommandHandler<Command, Result>
  8. {
  9. public async Task<Result> Handle(Command request, CancellationToken ct)
  10. {
  11. var intro = request.Intro?.Trim();
  12. if (string.IsNullOrWhiteSpace(intro))
  13. {
  14. return Result.Failure(Error.Problem("MyPage.IntroRequired", "자기소개는 필수입니다."));
  15. }
  16. if (intro.Length > 3000)
  17. {
  18. return Result.Failure(Error.Problem("MyPage.IntroTooLong", "자기소개는 3000자 이하여야 합니다."));
  19. }
  20. var member = await db.Member.FirstOrDefaultAsync(m => m.ID == request.MemberID, ct);
  21. if (member is null)
  22. {
  23. return Result.Failure(Error.NotFound("MyPage.MemberNotFound", "회원 정보를 찾을 수 없습니다."));
  24. }
  25. var log = MemberIntroChangeLog.Create(request.MemberID, member.Intro, intro);
  26. await db.MemberIntroChangeLog.AddAsync(log, ct);
  27. member.SetIntro(intro);
  28. await db.SaveChangesAsync(ct);
  29. return Result.Success();
  30. }
  31. }