CrewSessionStart.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using Application.Abstractions.Hub;
  2. using Application.Abstractions.Notification;
  3. using Domain.Entities.Notifications.ValueObject;
  4. using Infrastructure.Hubs;
  5. using Web.Api.Common;
  6. using MediatR;
  7. using Microsoft.AspNetCore.SignalR;
  8. namespace Web.Api.Endpoints.Donation;
  9. /// <summary>크루 방송 시작 — 세션 생성 + 크루원에게 초대 알림</summary>
  10. internal sealed class CrewSessionStart : IEndpoint
  11. {
  12. public void MapEndpoint(IEndpointRouteBuilder app)
  13. {
  14. app.MapPost("api/crew/session/start", async (
  15. Application.Features.Api.Crew.StartSession.Command body,
  16. ISender sender,
  17. IHubContext<AppHub, IAppHubClient> appHub,
  18. INotificationService notificationService,
  19. Application.Abstractions.Data.IAppDbContext db,
  20. CancellationToken ct
  21. ) => {
  22. var data = await sender.Send(body, ct);
  23. // 크루원들에게 초대 알림 발송
  24. var consents = await Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions
  25. .ToListAsync(
  26. Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions
  27. .AsNoTracking(db.CrewSessionConsent)
  28. .Where(c => c.CrewSessionID == data.CrewSessionID)
  29. .Join(db.CrewMember, c => c.CrewMemberID, m => m.ID, (c, m) => new { m.MemberID, m.Nickname }),
  30. ct
  31. );
  32. foreach (var member in consents)
  33. {
  34. await notificationService.SendAsync(
  35. member.MemberID,
  36. NotificationType.CrewInvitation,
  37. "크루 방송 초대",
  38. $"크루 방송 '{body.Title}'에 초대되었습니다. 참여 동의를 해주세요.",
  39. $"/crew/consent/{data.CrewSessionID}",
  40. "CrewSession", data.CrewSessionID, null, ct
  41. );
  42. // AppHub 실시간 푸시
  43. await appHub.Clients.Group($"member:{member.MemberID}").ReceiveCrewInvitation(new
  44. {
  45. CrewSessionID = data.CrewSessionID,
  46. Title = body.Title,
  47. CrewID = body.CrewID
  48. });
  49. }
  50. return ApiResponse.Ok(data);
  51. })
  52. .WithTags("Crew")
  53. .RequireAuthorization();
  54. }
  55. }