card_store.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package postgres
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. )
  7. type Card struct {
  8. ID string
  9. PublicID string
  10. CardType string
  11. Title string
  12. Subtitle *string
  13. CoverURL *string
  14. DisplaySlot string
  15. DisplayPriority int
  16. IsDefaultExperience bool
  17. StartsAt *time.Time
  18. EndsAt *time.Time
  19. EntryChannelID *string
  20. EventPublicID *string
  21. EventDisplayName *string
  22. EventSummary *string
  23. EventStatus *string
  24. EventCurrentReleasePubID *string
  25. EventConfigLabel *string
  26. EventRouteCode *string
  27. EventReleasePayloadJSON *string
  28. EventRuntimeBindingID *string
  29. EventPresentationID *string
  30. EventPresentationName *string
  31. EventPresentationType *string
  32. EventPresentationSchemaJSON *string
  33. EventContentBundleID *string
  34. EventContentBundleName *string
  35. EventContentEntryURL *string
  36. EventContentAssetRootURL *string
  37. EventContentMetadataJSON *string
  38. HTMLURL *string
  39. }
  40. func (s *Store) ListCardsForEntry(ctx context.Context, tenantID string, entryChannelID *string, slot string, now time.Time, limit int) ([]Card, error) {
  41. if limit <= 0 || limit > 100 {
  42. limit = 20
  43. }
  44. if slot == "" {
  45. slot = "home_primary"
  46. }
  47. rows, err := s.pool.Query(ctx, `
  48. SELECT
  49. c.id,
  50. c.card_public_id,
  51. c.card_type,
  52. c.title,
  53. c.subtitle,
  54. c.cover_url,
  55. c.display_slot,
  56. c.display_priority,
  57. c.is_default_experience,
  58. c.starts_at,
  59. c.ends_at,
  60. c.entry_channel_id,
  61. e.event_public_id,
  62. e.display_name,
  63. e.summary,
  64. e.status,
  65. er.release_public_id,
  66. er.config_label,
  67. er.route_code,
  68. er.payload_jsonb::text,
  69. mrb.runtime_binding_public_id,
  70. ep.presentation_public_id,
  71. ep.name,
  72. ep.presentation_type,
  73. ep.schema_jsonb::text,
  74. cb.content_bundle_public_id,
  75. cb.name,
  76. cb.entry_url,
  77. cb.asset_root_url,
  78. cb.metadata_jsonb::text,
  79. c.html_url
  80. FROM cards c
  81. LEFT JOIN events e ON e.id = c.event_id
  82. LEFT JOIN event_releases er ON er.id = e.current_release_id
  83. LEFT JOIN map_runtime_bindings mrb ON mrb.id = er.runtime_binding_id
  84. LEFT JOIN event_presentations ep ON ep.id = er.presentation_id
  85. LEFT JOIN content_bundles cb ON cb.id = er.content_bundle_id
  86. WHERE c.tenant_id = $1
  87. AND ($2::uuid IS NULL OR c.entry_channel_id = $2 OR c.entry_channel_id IS NULL)
  88. AND c.display_slot = $3
  89. AND c.status = 'active'
  90. AND (c.starts_at IS NULL OR c.starts_at <= $4)
  91. AND (c.ends_at IS NULL OR c.ends_at >= $4)
  92. ORDER BY
  93. CASE WHEN $2::uuid IS NOT NULL AND c.entry_channel_id = $2 THEN 0 ELSE 1 END,
  94. c.display_priority DESC,
  95. c.created_at ASC
  96. LIMIT $5
  97. `, tenantID, entryChannelID, slot, now, limit)
  98. if err != nil {
  99. return nil, fmt.Errorf("list cards for entry: %w", err)
  100. }
  101. defer rows.Close()
  102. var cards []Card
  103. for rows.Next() {
  104. var card Card
  105. if err := rows.Scan(
  106. &card.ID,
  107. &card.PublicID,
  108. &card.CardType,
  109. &card.Title,
  110. &card.Subtitle,
  111. &card.CoverURL,
  112. &card.DisplaySlot,
  113. &card.DisplayPriority,
  114. &card.IsDefaultExperience,
  115. &card.StartsAt,
  116. &card.EndsAt,
  117. &card.EntryChannelID,
  118. &card.EventPublicID,
  119. &card.EventDisplayName,
  120. &card.EventSummary,
  121. &card.EventStatus,
  122. &card.EventCurrentReleasePubID,
  123. &card.EventConfigLabel,
  124. &card.EventRouteCode,
  125. &card.EventReleasePayloadJSON,
  126. &card.EventRuntimeBindingID,
  127. &card.EventPresentationID,
  128. &card.EventPresentationName,
  129. &card.EventPresentationType,
  130. &card.EventPresentationSchemaJSON,
  131. &card.EventContentBundleID,
  132. &card.EventContentBundleName,
  133. &card.EventContentEntryURL,
  134. &card.EventContentAssetRootURL,
  135. &card.EventContentMetadataJSON,
  136. &card.HTMLURL,
  137. ); err != nil {
  138. return nil, fmt.Errorf("scan card: %w", err)
  139. }
  140. cards = append(cards, card)
  141. }
  142. if err := rows.Err(); err != nil {
  143. return nil, fmt.Errorf("iterate cards: %w", err)
  144. }
  145. return cards, nil
  146. }