feat: implement Phase 2 - Equipment Management and Training Zones

This commit is contained in:
Cipher Vance
2025-11-22 19:51:16 -06:00
parent c680333ef6
commit d6b91acdda
9 changed files with 548 additions and 17 deletions

View File

@@ -0,0 +1,171 @@
package equipment
import (
"encoding/json"
"net/http"
"strconv"
"rideaware/internal/config"
"rideaware/internal/middleware"
"rideaware/internal/user"
)
type Handler struct {
service *Service
userService *user.Service
}
func NewHandler() *Handler {
return &Handler{
service: NewService(),
userService: user.NewService(),
}
}
// CreateEquipment POST /api/equipment
func (h *Handler) CreateEquipment(w http.ResponseWriter, r *http.Request) {
claims := r.Context().Value(middleware.UserContextKey).(*config.CustomClaims)
var req struct {
Name string `json:"name"`
Type string `json:"type"`
Brand string `json:"brand"`
Model string `json:"model"`
Weight float64 `json:"weight"`
Notes string `json:"notes"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"error": "invalid request"})
return
}
equipment, err := h.service.CreateEquipment(
claims.UserID,
req.Name,
req.Type,
req.Brand,
req.Model,
req.Weight,
req.Notes,
)
if err != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(equipment)
}
// GetEquipment GET /api/equipment
func (h *Handler) GetEquipment(w http.ResponseWriter, r *http.Request) {
claims := r.Context().Value(middleware.UserContextKey).(*config.CustomClaims)
equipment, err := h.service.GetUserEquipment(claims.UserID)
if err != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]string{"error": "failed to fetch equipment"})
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(equipment)
}
// UpdateEquipment PUT /api/equipment
func (h *Handler) UpdateEquipment(w http.ResponseWriter, r *http.Request) {
claims := r.Context().Value(middleware.UserContextKey).(*config.CustomClaims)
idStr := r.URL.Query().Get("id")
id, err := strconv.ParseUint(idStr, 10, 32)
if err != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"error": "invalid equipment id"})
return
}
var req map[string]interface{}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"error": "invalid request"})
return
}
equipment, err := h.service.UpdateEquipment(uint(id), claims.UserID, req)
if err != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(equipment)
}
// DeleteEquipment DELETE /api/equipment
func (h *Handler) DeleteEquipment(w http.ResponseWriter, r *http.Request) {
claims := r.Context().Value(middleware.UserContextKey).(*config.CustomClaims)
idStr := r.URL.Query().Get("id")
id, err := strconv.ParseUint(idStr, 10, 32)
if err != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"error": "invalid equipment id"})
return
}
if err := h.service.DeleteEquipment(uint(id), claims.UserID); err != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNoContent)
}
// GetTrainingZones GET /api/zones
func (h *Handler) GetTrainingZones(w http.ResponseWriter, r *http.Request) {
claims := r.Context().Value(middleware.UserContextKey).(*config.CustomClaims)
profile, err := h.userService.GetUserByID(claims.UserID)
if err != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNotFound)
json.NewEncoder(w).Encode(map[string]string{"error": "user not found"})
return
}
// Check if profile exists
if profile == nil || profile.Profile == nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNotFound)
json.NewEncoder(w).Encode(map[string]string{"error": "user profile not found"})
return
}
zones := map[string]interface{}{}
if profile.Profile.MaxHR > 0 {
zones["hr_zones"] = h.service.CalculateHRZones(profile.Profile.MaxHR, profile.Profile.RestingHR)
}
if profile.Profile.FTP > 0 {
zones["power_zones"] = h.service.CalculatePowerZones(profile.Profile.FTP)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(zones)
}

View File

@@ -0,0 +1,43 @@
package equipment
import "time"
type Equipment struct {
ID uint `gorm:"primaryKey" json:"id"`
UserID uint `gorm:"not null;index" json:"user_id"`
Name string `gorm:"not null" json:"name"`
Type string `gorm:"not null" json:"type"` // "bike", "shoes", "helmet", etc.
Brand string `gorm:"default:''" json:"brand"`
Model string `gorm:"default:''" json:"model"`
Weight float64 `gorm:"default:0" json:"weight"` // grams
Notes string `gorm:"default:''" json:"notes"`
Active bool `gorm:"default:true" json:"active"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type TrainingZone struct {
ID uint `json:"id"`
Name string `json:"name"`
Min int `json:"min"`
Max int `json:"max"`
Color string `json:"color"`
}
type HRZones struct {
Zone1 TrainingZone `json:"zone_1"` // Recovery
Zone2 TrainingZone `json:"zone_2"` // Endurance
Zone3 TrainingZone `json:"zone_3"` // Tempo
Zone4 TrainingZone `json:"zone_4"` // Threshold
Zone5 TrainingZone `json:"zone_5"` // VO2 Max
}
type PowerZones struct {
Zone1 TrainingZone `json:"zone_1"` // Active Recovery
Zone2 TrainingZone `json:"zone_2"` // Endurance
Zone3 TrainingZone `json:"zone_3"` // Sweet Spot
Zone4 TrainingZone `json:"zone_4"` // Threshold
Zone5 TrainingZone `json:"zone_5"` // VO2 Max
Zone6 TrainingZone `json:"zone_6"` // Anaerobic
Zone7 TrainingZone `json:"zone_7"` // Neuromuscular
}

View File

@@ -0,0 +1,56 @@
package equipment
import (
"errors"
"rideaware/pkg/database"
"gorm.io/gorm"
)
type Repository struct{}
func NewRepository() *Repository {
return &Repository{}
}
func (r *Repository) CreateEquipment(equipment *Equipment) error {
return database.DB.Create(equipment).Error
}
func (r *Repository) GetEquipmentByID(id, userID uint) (*Equipment, error) {
var equipment Equipment
if err := database.DB.Where("id = ? AND user_id = ?", id, userID).
First(&equipment).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, errors.New("equipment not found")
}
return nil, err
}
return &equipment, nil
}
func (r *Repository) GetUserEquipment(userID uint) ([]Equipment, error) {
var equipment []Equipment
if err := database.DB.Where("user_id = ?", userID).
Find(&equipment).Error; err != nil {
return nil, err
}
return equipment, nil
}
func (r *Repository) GetActiveEquipment(userID uint) ([]Equipment, error) {
var equipment []Equipment
if err := database.DB.Where("user_id = ? AND active = ?", userID, true).
Find(&equipment).Error; err != nil {
return nil, err
}
return equipment, nil
}
func (r *Repository) UpdateEquipment(equipment *Equipment) error {
return database.DB.Save(equipment).Error
}
func (r *Repository) DeleteEquipment(id, userID uint) error {
return database.DB.Where("id = ? AND user_id = ?", id, userID).
Delete(&Equipment{}).Error
}

View File

@@ -0,0 +1,190 @@
package equipment
import (
"errors"
"math"
)
type Service struct {
repo *Repository
}
func NewService() *Service {
return &Service{
repo: NewRepository(),
}
}
// Equipment methods
func (s *Service) CreateEquipment(userID uint, name, equipmentType, brand, model string, weight float64, notes string) (*Equipment, error) {
if name == "" || equipmentType == "" {
return nil, errors.New("name and type are required")
}
equipment := &Equipment{
UserID: userID,
Name: name,
Type: equipmentType,
Brand: brand,
Model: model,
Weight: weight,
Notes: notes,
Active: true,
}
if err := s.repo.CreateEquipment(equipment); err != nil {
return nil, err
}
return equipment, nil
}
func (s *Service) GetUserEquipment(userID uint) ([]Equipment, error) {
return s.repo.GetUserEquipment(userID)
}
func (s *Service) GetEquipmentByID(id, userID uint) (*Equipment, error) {
return s.repo.GetEquipmentByID(id, userID)
}
func (s *Service) UpdateEquipment(id, userID uint, updates map[string]interface{}) (*Equipment, error) {
equipment, err := s.repo.GetEquipmentByID(id, userID)
if err != nil {
return nil, err
}
if name, ok := updates["name"].(string); ok && name != "" {
equipment.Name = name
}
if brand, ok := updates["brand"].(string); ok {
equipment.Brand = brand
}
if model, ok := updates["model"].(string); ok {
equipment.Model = model
}
if weight, ok := updates["weight"].(float64); ok {
equipment.Weight = weight
}
if notes, ok := updates["notes"].(string); ok {
equipment.Notes = notes
}
if active, ok := updates["active"].(bool); ok {
equipment.Active = active
}
if err := s.repo.UpdateEquipment(equipment); err != nil {
return nil, err
}
return equipment, nil
}
func (s *Service) DeleteEquipment(id, userID uint) error {
return s.repo.DeleteEquipment(id, userID)
}
// Training Zones calculation
func (s *Service) CalculateHRZones(maxHR, restingHR int) *HRZones {
if maxHR <= 0 {
return nil
}
// Karvonen formula (Heart Rate Reserve)
hrr := maxHR - restingHR
return &HRZones{
Zone1: TrainingZone{
ID: 1,
Name: "Recovery",
Min: restingHR + int(math.Round(float64(hrr)*0.50)),
Max: restingHR + int(math.Round(float64(hrr)*0.60)),
Color: "#4285F4", // Blue
},
Zone2: TrainingZone{
ID: 2,
Name: "Endurance",
Min: restingHR + int(math.Round(float64(hrr)*0.60)),
Max: restingHR + int(math.Round(float64(hrr)*0.70)),
Color: "#34A853", // Green
},
Zone3: TrainingZone{
ID: 3,
Name: "Tempo",
Min: restingHR + int(math.Round(float64(hrr)*0.70)),
Max: restingHR + int(math.Round(float64(hrr)*0.80)),
Color: "#FBBC04", // Yellow
},
Zone4: TrainingZone{
ID: 4,
Name: "Threshold",
Min: restingHR + int(math.Round(float64(hrr)*0.80)),
Max: restingHR + int(math.Round(float64(hrr)*0.90)),
Color: "#EA4335", // Red
},
Zone5: TrainingZone{
ID: 5,
Name: "VO2 Max",
Min: restingHR + int(math.Round(float64(hrr)*0.90)),
Max: maxHR,
Color: "#A61C00", // Dark Red
},
}
}
func (s *Service) CalculatePowerZones(ftp int) *PowerZones {
if ftp <= 0 {
return nil
}
return &PowerZones{
Zone1: TrainingZone{
ID: 1,
Name: "Active Recovery",
Min: 0,
Max: int(math.Round(float64(ftp) * 0.55)),
Color: "#4285F4", // Blue
},
Zone2: TrainingZone{
ID: 2,
Name: "Endurance",
Min: int(math.Round(float64(ftp) * 0.55)),
Max: int(math.Round(float64(ftp) * 0.75)),
Color: "#34A853", // Green
},
Zone3: TrainingZone{
ID: 3,
Name: "Sweet Spot",
Min: int(math.Round(float64(ftp) * 0.75)),
Max: int(math.Round(float64(ftp) * 0.90)),
Color: "#FBBC04", // Yellow
},
Zone4: TrainingZone{
ID: 4,
Name: "Threshold",
Min: int(math.Round(float64(ftp) * 0.90)),
Max: int(math.Round(float64(ftp) * 1.05)),
Color: "#EA4335", // Red
},
Zone5: TrainingZone{
ID: 5,
Name: "VO2 Max",
Min: int(math.Round(float64(ftp) * 1.05)),
Max: int(math.Round(float64(ftp) * 1.20)),
Color: "#A61C00", // Dark Red
},
Zone6: TrainingZone{
ID: 6,
Name: "Anaerobic",
Min: int(math.Round(float64(ftp) * 1.20)),
Max: int(math.Round(float64(ftp) * 1.50)),
Color: "#800080", // Purple
},
Zone7: TrainingZone{
ID: 7,
Name: "Neuromuscular",
Min: int(math.Round(float64(ftp) * 1.50)),
Max: int(math.Round(float64(ftp) * 2.00)),
Color: "#FF1744", // Bright Red
},
}
}