67 lines
1.7 KiB
Go
67 lines
1.7 KiB
Go
package ai
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
"rideaware/pkg/database"
|
|
)
|
|
|
|
type Repository struct{}
|
|
|
|
func NewRepository() *Repository {
|
|
return &Repository{}
|
|
}
|
|
|
|
// SaveRecommendation stores the AI recommendation
|
|
func (r *Repository) SaveRecommendation(
|
|
userID uint,
|
|
userCtx UserContext,
|
|
params GenerateRequest,
|
|
aiResponse string,
|
|
generated *GenerateResponse,
|
|
) (uint, error) {
|
|
|
|
contextJSON, _ := json.Marshal(userCtx)
|
|
paramsJSON, _ := json.Marshal(params)
|
|
workoutsJSON, _ := json.Marshal(generated.Workouts)
|
|
|
|
rec := &AIRecommendation{
|
|
UserID: userID,
|
|
PromptContext: JSONB{Data: contextJSON},
|
|
AIResponse: aiResponse,
|
|
GeneratedWorkouts: JSONB{Data: workoutsJSON},
|
|
Parameters: JSONB{Data: paramsJSON},
|
|
Status: "generated",
|
|
}
|
|
|
|
if err := database.DB.Create(rec).Error; err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
return rec.ID, nil
|
|
}
|
|
|
|
// GetRecommendation retrieves a recommendation by ID
|
|
func (r *Repository) GetRecommendation(id uint, userID uint) (*AIRecommendation, error) {
|
|
var rec AIRecommendation
|
|
err := database.DB.Where("id = ? AND user_id = ?", id, userID).First(&rec).Error
|
|
return &rec, err
|
|
}
|
|
|
|
// GetUserRecommendations fetches recent recommendations for a user
|
|
func (r *Repository) GetUserRecommendations(userID uint, limit int) ([]AIRecommendation, error) {
|
|
var recs []AIRecommendation
|
|
err := database.DB.Where("user_id = ?", userID).
|
|
Order("created_at DESC").
|
|
Limit(limit).
|
|
Find(&recs).Error
|
|
return recs, err
|
|
}
|
|
|
|
// UpdateRecommendationStatus marks a recommendation as scheduled/rejected
|
|
func (r *Repository) UpdateRecommendationStatus(id uint, status string) error {
|
|
return database.DB.Model(&AIRecommendation{}).
|
|
Where("id = ?", id).
|
|
Update("status", status).Error
|
|
}
|