CrewWidgetConfigByToken.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using Application.Abstractions.Data;
  2. using Web.Api.Common;
  3. using Microsoft.EntityFrameworkCore;
  4. namespace Web.Api.Endpoints.Widget;
  5. /// <summary>WidgetToken + configID로 CrewWidgetConfig 조회 (위젯 스타일 적용용, 인증 불필요).
  6. /// configID 미지정 시 활성 첫 번째 자동 선택.</summary>
  7. internal sealed class CrewWidgetConfigByToken : IEndpoint
  8. {
  9. public void MapEndpoint(IEndpointRouteBuilder app)
  10. {
  11. app.MapGet("api/widget/crew/config/{widgetToken}", async (
  12. string widgetToken,
  13. int? configID,
  14. IAppDbContext db,
  15. CancellationToken ct
  16. ) => {
  17. var channelID = await db.Channel.AsNoTracking()
  18. .Where(c => c.WidgetToken == widgetToken && c.IsActive)
  19. .Select(c => c.ID)
  20. .FirstOrDefaultAsync(ct);
  21. if (channelID == 0)
  22. {
  23. return ApiResponse.Ok((object?)null);
  24. }
  25. var query = db.CrewWidgetConfig.AsNoTracking()
  26. .Where(w => w.ChannelID == channelID);
  27. if (configID.HasValue)
  28. {
  29. query = query.Where(w => w.ID == configID.Value);
  30. }
  31. else
  32. {
  33. query = query.Where(w => w.IsActive);
  34. }
  35. var config = await query
  36. .OrderByDescending(w => w.ID)
  37. .Select(w => new
  38. {
  39. id = w.ID,
  40. crewID = w.CrewID,
  41. crewName = w.Crew != null ? w.Crew.Name : "",
  42. title = w.Title,
  43. theme = (int)w.Theme,
  44. period = (int)w.Period,
  45. maxDisplayCount = w.MaxDisplayCount,
  46. isShowAmount = w.IsShowAmount,
  47. isShowDonationCount = w.IsShowDonationCount,
  48. isShowContributionRate = w.IsShowContributionRate,
  49. isShowMemberIcon = w.IsShowMemberIcon,
  50. isActive = w.IsActive,
  51. bgColor = w.BgColor,
  52. titleFontFamily = w.TitleFontFamily,
  53. titleFontSizePx = w.TitleFontSizePx,
  54. titleFontColor = w.TitleFontColor,
  55. rank1FontFamily = w.Rank1FontFamily,
  56. rank1FontSizePx = w.Rank1FontSizePx,
  57. rank1FontColor = w.Rank1FontColor,
  58. rank2FontFamily = w.Rank2FontFamily,
  59. rank2FontSizePx = w.Rank2FontSizePx,
  60. rank2FontColor = w.Rank2FontColor,
  61. rank3FontFamily = w.Rank3FontFamily,
  62. rank3FontSizePx = w.Rank3FontSizePx,
  63. rank3FontColor = w.Rank3FontColor,
  64. rowFontFamily = w.RowFontFamily,
  65. rowFontSizePx = w.RowFontSizePx,
  66. rowFontColor = w.RowFontColor
  67. })
  68. .FirstOrDefaultAsync(ct);
  69. return ApiResponse.Ok((object?)config);
  70. })
  71. .WithTags("Widget")
  72. .AllowAnonymous();
  73. }
  74. }