GoalConfigByToken.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using Application.Abstractions.Data;
  2. using Web.Api.Common;
  3. using Microsoft.EntityFrameworkCore;
  4. namespace Web.Api.Endpoints.Widget;
  5. /// <summary>WidgetToken + configID로 GoalConfig 조회 (위젯 스타일 적용용, 인증 불필요).
  6. /// configID 미지정 시 활성 첫 번째 자동 선택.</summary>
  7. internal sealed class GoalConfigByToken : IEndpoint
  8. {
  9. public void MapEndpoint(IEndpointRouteBuilder app)
  10. {
  11. app.MapGet("api/widget/goal/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.DonationGoalConfig.AsNoTracking()
  26. .Where(g => g.ChannelID == channelID);
  27. if (configID.HasValue)
  28. {
  29. query = query.Where(g => g.ID == configID.Value);
  30. }
  31. else
  32. {
  33. query = query.Where(g => g.IsActive);
  34. }
  35. var config = await query
  36. .OrderByDescending(g => g.ID)
  37. .Select(g => new
  38. {
  39. id = g.ID,
  40. title = g.Title,
  41. style = (int)g.Style,
  42. startAmount = g.StartAmount,
  43. targetAmount = g.TargetAmount,
  44. isShowPercent = g.IsShowPercent,
  45. barColor = g.BarColor,
  46. barBackgroundColor = g.BarBackgroundColor,
  47. barHeightPx = g.BarHeightPx,
  48. titleFontSizePx = g.TitleFontSizePx,
  49. titleFontColor = g.TitleFontColor,
  50. amountFontSizePx = g.AmountFontSizePx,
  51. amountFontColor = g.AmountFontColor,
  52. titleFontFamily = g.TitleFontFamily,
  53. amountFontFamily = g.AmountFontFamily,
  54. period = (int)g.Period,
  55. isActive = g.IsActive
  56. })
  57. .FirstOrDefaultAsync(ct);
  58. return ApiResponse.Ok((object?)config);
  59. })
  60. .WithTags("Widget")
  61. .AllowAnonymous();
  62. }
  63. }