card_store.go 4.1 KB

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