Handler.cs 1.1 KB

1234567891011121314151617181920212223242526272829
  1. using Application.Abstractions.Data;
  2. using Application.Abstractions.Messaging;
  3. using Microsoft.EntityFrameworkCore;
  4. using SharedKernel.Results;
  5. namespace Application.Features.Api.ChannelTitle.DeleteChannelTitle;
  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. // 관련 선택 기록의 참조는 SetNull FK로 자동 처리됨
  20. db.ChannelTitle.Remove(title);
  21. await db.SaveChangesAsync(ct);
  22. return Result.Success();
  23. }
  24. }