feat: extend equipment and workout models with service tracking
This commit is contained in:
@@ -62,4 +62,23 @@ func (r *Repository) UpdateWorkout(workout *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
|
||||
}
|
||||
Reference in New Issue
Block a user