123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- package store
- import (
- "context"
- "encoding/json"
- "fmt"
- "time"
- "git.linuxforward.com/byom/byom-trends/common"
- "github.com/google/uuid"
- "gorm.io/gorm"
- )
- type DataStore struct {
- db *gorm.DB
- }
- func NewDataStore(db *gorm.DB) *DataStore {
- // Add auto-migrations
- if err := db.AutoMigrate(
- &common.TrendAnalysis{},
- &common.SocialProfile{},
- &common.AudienceInsight{},
- &common.GoogleTrend{},
- ); err != nil {
- panic(fmt.Sprintf("failed to migrate database: %v", err))
- }
- return &DataStore{db: db}
- }
- // TrendAnalysis operations
- func (s *DataStore) CreateTrendAnalysis(ctx context.Context, analysis *common.TrendAnalysis) error {
- result := s.db.Create(analysis)
- if result.Error != nil {
- return fmt.Errorf("create trend analysis: %w", result.Error)
- }
- return nil
- }
- func (s *DataStore) GetTrendAnalysis(ctx context.Context, id uuid.UUID) (*common.TrendAnalysis, error) {
- var analysis common.TrendAnalysis
- result := s.db.First(&analysis, "id = ?", id)
- if result.Error != nil {
- if result.Error == gorm.ErrRecordNotFound {
- return nil, nil
- }
- return nil, fmt.Errorf("get trend analysis: %w", result.Error)
- }
- return &analysis, nil
- }
- // SocialProfile operations
- func (s *DataStore) CreateSocialProfile(ctx context.Context, profile *common.SocialProfile) error {
- result := s.db.Create(profile)
- if result.Error != nil {
- return fmt.Errorf("create social profile: %w", result.Error)
- }
- return nil
- }
- func (s *DataStore) GetSocialProfile(ctx context.Context, id uuid.UUID) (*common.SocialProfile, error) {
- var profile common.SocialProfile
- result := s.db.First(&profile, "id = ?", id)
- if result.Error != nil {
- if result.Error == gorm.ErrRecordNotFound {
- return nil, nil
- }
- return nil, fmt.Errorf("get social profile: %w", result.Error)
- }
- return &profile, nil
- }
- // AudienceInsight operations
- func (s *DataStore) CreateAudienceInsight(ctx context.Context, insight *common.AudienceInsight) error {
- result := s.db.Create(insight)
- if result.Error != nil {
- return fmt.Errorf("create audience insight: %w", result.Error)
- }
- return nil
- }
- func (s *DataStore) GetAudienceInsight(ctx context.Context, id uuid.UUID) (*common.AudienceInsight, error) {
- var insight common.AudienceInsight
- result := s.db.First(&insight, "id = ?", id)
- if result.Error != nil {
- if result.Error == gorm.ErrRecordNotFound {
- return nil, nil
- }
- return nil, fmt.Errorf("get audience insight: %w", result.Error)
- }
- return &insight, nil
- }
- // GoogleTrend operations
- func (s *DataStore) CreateGoogleTrend(ctx context.Context, trend *common.GoogleTrend) error {
- result := s.db.Create(trend)
- if result.Error != nil {
- return fmt.Errorf("create google trend: %w", result.Error)
- }
- return nil
- }
- func (s *DataStore) GetGoogleTrend(ctx context.Context, id uuid.UUID) (*common.GoogleTrend, error) {
- var trend common.GoogleTrend
- result := s.db.First(&trend, "id = ?", id)
- if result.Error != nil {
- if result.Error == gorm.ErrRecordNotFound {
- return nil, nil
- }
- return nil, fmt.Errorf("get google trend: %w", result.Error)
- }
- return &trend, nil
- }
- // GetDB returns the underlying database connection
- func (s *DataStore) GetDB() *gorm.DB {
- return s.db
- }
- func (s *DataStore) GetTrendsByUsername(ctx context.Context, username string) ([]common.ContentTrend, error) {
- var analysis common.TrendAnalysis
- result := s.db.Where("data->>'username' = ?", username).First(&analysis)
- if result.Error != nil {
- if result.Error == gorm.ErrRecordNotFound {
- return nil, nil
- }
- return nil, fmt.Errorf("get trends by username: %w", result.Error)
- }
- var trends []common.ContentTrend
- if err := json.Unmarshal(analysis.Data, &trends); err != nil {
- return nil, fmt.Errorf("unmarshal trend data: %w", err)
- }
- return trends, nil
- }
- // SaveSuggestions stores product suggestions in the database
- func (s *DataStore) SaveSuggestions(ctx context.Context, userID string, suggestions []common.ProductSuggestion) error {
- for i := range suggestions {
- suggestions[i].ID = uuid.New()
- suggestions[i].UserID = userID
- suggestions[i].CreatedAt = time.Now()
- suggestions[i].UpdatedAt = time.Now()
- if err := s.db.Create(&suggestions[i]).Error; err != nil {
- return fmt.Errorf("create suggestion: %w", err)
- }
- }
- return nil
- }
- // GetSuggestionsByUserID retrieves all suggestions for a user
- func (s *DataStore) GetSuggestionsByUserID(ctx context.Context, userID string) ([]common.ProductSuggestion, error) {
- var suggestions []common.ProductSuggestion
- if err := s.db.Where("user_id = ?", userID).Find(&suggestions).Error; err != nil {
- return nil, fmt.Errorf("get suggestions: %w", err)
- }
- return suggestions, nil
- }
- // GetSuggestionByID retrieves a specific suggestion
- func (s *DataStore) GetSuggestionByID(ctx context.Context, id uuid.UUID) (*common.ProductSuggestion, error) {
- var suggestion common.ProductSuggestion
- if err := s.db.First(&suggestion, "id = ?", id).Error; err != nil {
- if err == gorm.ErrRecordNotFound {
- return nil, nil
- }
- return nil, fmt.Errorf("get suggestion: %w", err)
- }
- return &suggestion, nil
- }
|