84 lines
2.4 KiB
Go
84 lines
2.4 KiB
Go
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) 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
|
|
}
|
|
|
|
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
|
|
} |