Handler.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using Application.Abstractions.Data;
  2. using Application.Abstractions.Messaging;
  3. using Microsoft.EntityFrameworkCore;
  4. using SharedKernel.Results;
  5. namespace Application.Features.Api.ChannelTitle.UpdateChannelTitle;
  6. internal sealed class Handler(IAppDbContext db) : ICommandHandler<Command, Result>
  7. {
  8. public async Task<Result> Handle(Command request, CancellationToken ct)
  9. {
  10. var title = await db.ChannelTitle.Include(t => t.Channel).FirstOrDefaultAsync(t => t.ID == request.TitleID, ct);
  11. if (title is null)
  12. {
  13. return Result.Failure(Error.NotFound("ChannelTitle.NotFound", "칭호를 찾을 수 없습니다."));
  14. }
  15. if (title.Channel is null || title.Channel.MemberID != request.MemberID)
  16. {
  17. return Result.Failure(Error.Forbidden("ChannelTitle.Forbidden", "본인 채널의 칭호만 수정할 수 있습니다."));
  18. }
  19. if (request.MinAmount < 0)
  20. {
  21. return Result.Failure(Error.Problem("ChannelTitle.InvalidAmount", "최소 금액은 0원 이상이어야 합니다."));
  22. }
  23. var duplicateAmount = await db.ChannelTitle.AnyAsync(t => t.ChannelID == title.ChannelID && t.ID != request.TitleID && t.MinAmount == request.MinAmount, ct);
  24. if (duplicateAmount)
  25. {
  26. return Result.Failure(Error.Conflict("ChannelTitle.DuplicateAmount", "같은 최소 금액의 칭호가 이미 존재합니다."));
  27. }
  28. title.Update(request.Name, request.Description, request.MinAmount, request.Color, request.IconUrl, request.IsActive);
  29. await db.SaveChangesAsync(ct);
  30. return Result.Success();
  31. }
  32. }