lots of stuff, don't truly remember
This commit is contained in:
@@ -39,6 +39,17 @@ func (r *Repository) GetUserWorkouts(userID uint) ([]Workout, error) {
|
||||
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).
|
||||
@@ -64,6 +75,33 @@ func (r *Repository) DeleteWorkout(id, userID uint) error {
|
||||
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"`
|
||||
@@ -81,4 +119,4 @@ func (r *Repository) GetEquipmentStats(userID uint) ([]EquipmentStat, error) {
|
||||
return nil, err
|
||||
}
|
||||
return stats, nil
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user