store.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package store
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "time"
  7. "git.linuxforward.com/byom/byom-trends/common"
  8. "github.com/google/uuid"
  9. "gorm.io/gorm"
  10. )
  11. type DataStore struct {
  12. db *gorm.DB
  13. }
  14. func NewDataStore(db *gorm.DB) *DataStore {
  15. // Add auto-migrations
  16. if err := db.AutoMigrate(
  17. &common.TrendAnalysis{},
  18. &common.SocialProfile{},
  19. &common.AudienceInsight{},
  20. &common.GoogleTrend{},
  21. ); err != nil {
  22. panic(fmt.Sprintf("failed to migrate database: %v", err))
  23. }
  24. return &DataStore{db: db}
  25. }
  26. // TrendAnalysis operations
  27. func (s *DataStore) CreateTrendAnalysis(ctx context.Context, analysis *common.TrendAnalysis) error {
  28. result := s.db.Create(analysis)
  29. if result.Error != nil {
  30. return fmt.Errorf("create trend analysis: %w", result.Error)
  31. }
  32. return nil
  33. }
  34. func (s *DataStore) GetTrendAnalysis(ctx context.Context, id uuid.UUID) (*common.TrendAnalysis, error) {
  35. var analysis common.TrendAnalysis
  36. result := s.db.First(&analysis, "id = ?", id)
  37. if result.Error != nil {
  38. if result.Error == gorm.ErrRecordNotFound {
  39. return nil, nil
  40. }
  41. return nil, fmt.Errorf("get trend analysis: %w", result.Error)
  42. }
  43. return &analysis, nil
  44. }
  45. // SocialProfile operations
  46. func (s *DataStore) CreateSocialProfile(ctx context.Context, profile *common.SocialProfile) error {
  47. result := s.db.Create(profile)
  48. if result.Error != nil {
  49. return fmt.Errorf("create social profile: %w", result.Error)
  50. }
  51. return nil
  52. }
  53. func (s *DataStore) GetSocialProfile(ctx context.Context, id uuid.UUID) (*common.SocialProfile, error) {
  54. var profile common.SocialProfile
  55. result := s.db.First(&profile, "id = ?", id)
  56. if result.Error != nil {
  57. if result.Error == gorm.ErrRecordNotFound {
  58. return nil, nil
  59. }
  60. return nil, fmt.Errorf("get social profile: %w", result.Error)
  61. }
  62. return &profile, nil
  63. }
  64. // AudienceInsight operations
  65. func (s *DataStore) CreateAudienceInsight(ctx context.Context, insight *common.AudienceInsight) error {
  66. result := s.db.Create(insight)
  67. if result.Error != nil {
  68. return fmt.Errorf("create audience insight: %w", result.Error)
  69. }
  70. return nil
  71. }
  72. func (s *DataStore) GetAudienceInsight(ctx context.Context, id uuid.UUID) (*common.AudienceInsight, error) {
  73. var insight common.AudienceInsight
  74. result := s.db.First(&insight, "id = ?", id)
  75. if result.Error != nil {
  76. if result.Error == gorm.ErrRecordNotFound {
  77. return nil, nil
  78. }
  79. return nil, fmt.Errorf("get audience insight: %w", result.Error)
  80. }
  81. return &insight, nil
  82. }
  83. // GoogleTrend operations
  84. func (s *DataStore) CreateGoogleTrend(ctx context.Context, trend *common.GoogleTrend) error {
  85. result := s.db.Create(trend)
  86. if result.Error != nil {
  87. return fmt.Errorf("create google trend: %w", result.Error)
  88. }
  89. return nil
  90. }
  91. func (s *DataStore) GetGoogleTrend(ctx context.Context, id uuid.UUID) (*common.GoogleTrend, error) {
  92. var trend common.GoogleTrend
  93. result := s.db.First(&trend, "id = ?", id)
  94. if result.Error != nil {
  95. if result.Error == gorm.ErrRecordNotFound {
  96. return nil, nil
  97. }
  98. return nil, fmt.Errorf("get google trend: %w", result.Error)
  99. }
  100. return &trend, nil
  101. }
  102. // GetDB returns the underlying database connection
  103. func (s *DataStore) GetDB() *gorm.DB {
  104. return s.db
  105. }
  106. func (s *DataStore) GetTrendsByUsername(ctx context.Context, username string) ([]common.ContentTrend, error) {
  107. var analysis common.TrendAnalysis
  108. result := s.db.Where("data->>'username' = ?", username).First(&analysis)
  109. if result.Error != nil {
  110. if result.Error == gorm.ErrRecordNotFound {
  111. return nil, nil
  112. }
  113. return nil, fmt.Errorf("get trends by username: %w", result.Error)
  114. }
  115. var trends []common.ContentTrend
  116. if err := json.Unmarshal(analysis.Data, &trends); err != nil {
  117. return nil, fmt.Errorf("unmarshal trend data: %w", err)
  118. }
  119. return trends, nil
  120. }
  121. // SaveSuggestions stores product suggestions in the database
  122. func (s *DataStore) SaveSuggestions(ctx context.Context, userID string, suggestions []common.ProductSuggestion) error {
  123. for i := range suggestions {
  124. suggestions[i].ID = uuid.New()
  125. suggestions[i].UserID = userID
  126. suggestions[i].CreatedAt = time.Now()
  127. suggestions[i].UpdatedAt = time.Now()
  128. if err := s.db.Create(&suggestions[i]).Error; err != nil {
  129. return fmt.Errorf("create suggestion: %w", err)
  130. }
  131. }
  132. return nil
  133. }
  134. // GetSuggestionsByUserID retrieves all suggestions for a user
  135. func (s *DataStore) GetSuggestionsByUserID(ctx context.Context, userID string) ([]common.ProductSuggestion, error) {
  136. var suggestions []common.ProductSuggestion
  137. if err := s.db.Where("user_id = ?", userID).Find(&suggestions).Error; err != nil {
  138. return nil, fmt.Errorf("get suggestions: %w", err)
  139. }
  140. return suggestions, nil
  141. }
  142. // GetSuggestionByID retrieves a specific suggestion
  143. func (s *DataStore) GetSuggestionByID(ctx context.Context, id uuid.UUID) (*common.ProductSuggestion, error) {
  144. var suggestion common.ProductSuggestion
  145. if err := s.db.First(&suggestion, "id = ?", id).Error; err != nil {
  146. if err == gorm.ErrRecordNotFound {
  147. return nil, nil
  148. }
  149. return nil, fmt.Errorf("get suggestion: %w", err)
  150. }
  151. return &suggestion, nil
  152. }