Handler.cs 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. using Application.Abstractions.Messaging;
  2. using Application.Abstractions.Data;
  3. using Microsoft.EntityFrameworkCore;
  4. namespace Application.Features.Admin.Crypto.Board.LinkBoard
  5. {
  6. public sealed class Handler(IAppDbContext db) : ICommandHandler<Command>
  7. {
  8. public async Task Handle(Command request, CancellationToken ct)
  9. {
  10. if (!await db.Coin.AnyAsync(c => c.ID == request.CoinID, ct))
  11. {
  12. throw new KeyNotFoundException("코인을 찾을 수 없습니다.");
  13. }
  14. var board = await db.Board.FirstOrDefaultAsync(b => b.ID == request.BoardID, ct);
  15. if (board is null)
  16. {
  17. throw new KeyNotFoundException("게시판을 찾을 수 없습니다.");
  18. }
  19. if (board.CoinID is not null)
  20. {
  21. throw new InvalidOperationException("이미 다른 코인에 연결된 게시판입니다.");
  22. }
  23. board.CoinID = request.CoinID;
  24. await db.SaveChangesAsync(ct);
  25. }
  26. }
  27. }