package workout import ( "errors" "rideaware/pkg/database" "time" "gorm.io/gorm" ) type Repository struct{} func NewRepository() *Repository { return &Repository{} } func (r *Repository) CreateWorkout(workout *Workout) error { return database.DB.Create(workout).Error } func (r *Repository) GetWorkoutByID(id, userID uint) (*Workout, error) { var workout Workout if err := database.DB.Where("id = ? AND user_id = ?", id, userID). First(&workout).Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return nil, errors.New("workout not found") } return nil, err } return &workout, nil } func (r *Repository) GetUserWorkouts(userID uint) ([]Workout, error) { var workouts []Workout if err := database.DB.Where("user_id = ?", userID). Order("scheduled_date DESC"). Find(&workouts).Error; err != nil { return nil, err } return workouts, nil } func (r *Repository) GetUserWorkoutsByTags(userID uint, tags []string) ([]Workout, error) { var workouts []Workout // Use PostgreSQL jsonb ?| operator to check if the tags array contains any of the given tags if err := database.DB.Where("user_id = ? AND tags ?| ?", userID, tags). Order("scheduled_date DESC"). Find(&workouts).Error; err != nil { return nil, err } return workouts, nil } func (r *Repository) GetWorkoutsByDateRange(userID uint, start, end time.Time) ([]Workout, error) { var workouts []Workout if err := database.DB.Where("user_id = ? AND scheduled_date BETWEEN ? AND ?", userID, start, end). Order("scheduled_date ASC"). Find(&workouts).Error; err != nil { return nil, err } return workouts, nil } func (r *Repository) GetWorkoutsByMonth(userID uint, year, month int) ([]Workout, error) { start := time.Date(year, time.Month(month), 1, 0, 0, 0, 0, time.UTC) end := start.AddDate(0, 1, 0).Add(-time.Second) return r.GetWorkoutsByDateRange(userID, start, end) } func (r *Repository) UpdateWorkout(workout *Workout) error { return database.DB.Model(workout).Updates(workout).Error } func (r *Repository) DeleteWorkout(id, userID uint) error { return database.DB.Where("id = ? AND user_id = ?", id, userID). Delete(&Workout{}).Error } func (r *Repository) RemoveDuplicates(userID uint) (int64, error) { // Find IDs to keep: the minimum ID for each (title, scheduled_date, duration) group // Delete all other workouts that are duplicates result := database.DB.Exec(` DELETE FROM workouts WHERE user_id = ? AND id NOT IN ( SELECT MIN(id) FROM workouts WHERE user_id = ? GROUP BY title, scheduled_date, duration ) `, userID, userID) if result.Error != nil { return 0, result.Error } return result.RowsAffected, nil } func (r *Repository) GetCompletedWorkoutOnDate(userID uint, date string) (*Workout, error) { var w Workout if err := database.DB.Where("user_id = ? AND status = 'completed' AND DATE(scheduled_date) = ?", userID, date). First(&w).Error; err != nil { return nil, err } return &w, nil } type EquipmentStat struct { EquipmentID uint `json:"equipment_id"` TotalRides int `json:"total_rides"` TotalDistance float64 `json:"total_distance"` TotalDuration int `json:"total_duration"` } func (r *Repository) GetEquipmentStats(userID uint) ([]EquipmentStat, error) { var stats []EquipmentStat if err := database.DB.Model(&Workout{}). Select("equipment_id, COUNT(*) as total_rides, COALESCE(SUM(distance), 0) as total_distance, COALESCE(SUM(duration), 0) as total_duration"). Where("user_id = ? AND equipment_id IS NOT NULL", userID). Group("equipment_id"). Scan(&stats).Error; err != nil { return nil, err } return stats, nil }