Handler.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. using Application.Abstractions.Data;
  2. using Application.Abstractions.Hub;
  3. using Application.Abstractions.Messaging;
  4. using Application.Abstractions.YouTube;
  5. using Microsoft.EntityFrameworkCore;
  6. using SharedKernel.Results;
  7. namespace Application.Features.Admin.Channel.YouTubePubSub.ForceOffline;
  8. internal sealed class Handler(
  9. IAppDbContext db,
  10. IYouTubeLiveStateStore liveStateStore,
  11. IChannelStatusBroadcaster broadcaster
  12. ) : ICommandHandler<Command, Result>
  13. {
  14. public async Task<Result> Handle(Command request, CancellationToken ct)
  15. {
  16. if (string.IsNullOrWhiteSpace(request.YouTubeChannelID))
  17. {
  18. return Result.Failure(Error.Problem("ForceOffline.Invalid", "YouTube Channel ID 가 필요합니다."));
  19. }
  20. var channel = await db.Channel.AsNoTracking()
  21. .FirstOrDefaultAsync(c => c.YouTubeChannelID == request.YouTubeChannelID, ct);
  22. if (channel is null)
  23. {
  24. return Result.Failure(Error.NotFound("ForceOffline.ChannelNotFound", "내부 Channel 을 찾을 수 없습니다."));
  25. }
  26. await liveStateStore.ClearLiveAsync(request.YouTubeChannelID);
  27. await broadcaster.BroadcastAsync(channel.SID, isLive: false, viewerCount: 0, videoId: null, ct);
  28. return Result.Success();
  29. }
  30. }