feat: Add data models and PostgreSQL storage layer

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Jan Novak
2026-01-19 19:49:59 +01:00
parent a59ef0f3f5
commit cd435a6569
5 changed files with 766 additions and 0 deletions

View File

@@ -0,0 +1,114 @@
package models
import (
"time"
)
// ExerciseType represents the type of exercise
type ExerciseType string
const (
ExerciseTypeStrength ExerciseType = "strength"
ExerciseTypeCardio ExerciseType = "cardio"
)
// Exercise represents an exercise definition
type Exercise struct {
ID int64 `json:"id"`
Name string `json:"name"`
Type ExerciseType `json:"type"`
MuscleGroup string `json:"muscle_group,omitempty"`
Description string `json:"description,omitempty"`
CreatedAt time.Time `json:"created_at"`
}
// TrainingPlan represents a training plan template
type TrainingPlan struct {
ID int64 `json:"id"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
Exercises []PlanExercise `json:"exercises,omitempty"`
CreatedAt time.Time `json:"created_at"`
}
// PlanExercise represents an exercise within a training plan
type PlanExercise struct {
ID int64 `json:"id"`
PlanID int64 `json:"plan_id"`
ExerciseID int64 `json:"exercise_id"`
Exercise Exercise `json:"exercise,omitempty"`
Sets int `json:"sets,omitempty"`
Reps int `json:"reps,omitempty"`
Duration int `json:"duration,omitempty"` // in seconds
Order int `json:"order"`
}
// Session represents a completed training session
type Session struct {
ID int64 `json:"id"`
PlanID *int64 `json:"plan_id,omitempty"`
Plan *TrainingPlan `json:"plan,omitempty"`
Date time.Time `json:"date"`
Notes string `json:"notes,omitempty"`
Entries []SessionEntry `json:"entries,omitempty"`
CreatedAt time.Time `json:"created_at"`
}
// SessionEntry represents an individual exercise entry in a session
type SessionEntry struct {
ID int64 `json:"id"`
SessionID int64 `json:"session_id"`
ExerciseID int64 `json:"exercise_id"`
Exercise *Exercise `json:"exercise,omitempty"`
Weight float64 `json:"weight,omitempty"` // in kg
Reps int `json:"reps,omitempty"` // for strength
SetsCompleted int `json:"sets_completed,omitempty"`
Duration int `json:"duration,omitempty"` // in seconds, for cardio
Distance float64 `json:"distance,omitempty"` // in km, for cardio
HeartRate int `json:"heart_rate,omitempty"` // average HR
Notes string `json:"notes,omitempty"`
CreatedAt time.Time `json:"created_at"`
}
// CreateExerciseRequest represents the request to create an exercise
type CreateExerciseRequest struct {
Name string `json:"name"`
Type ExerciseType `json:"type"`
MuscleGroup string `json:"muscle_group,omitempty"`
Description string `json:"description,omitempty"`
}
// CreatePlanRequest represents the request to create a training plan
type CreatePlanRequest struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Exercises []PlanExerciseInput `json:"exercises,omitempty"`
}
// PlanExerciseInput represents exercise input for plan creation
type PlanExerciseInput struct {
ExerciseID int64 `json:"exercise_id"`
Sets int `json:"sets,omitempty"`
Reps int `json:"reps,omitempty"`
Duration int `json:"duration,omitempty"`
Order int `json:"order"`
}
// CreateSessionRequest represents the request to create a session
type CreateSessionRequest struct {
PlanID *int64 `json:"plan_id,omitempty"`
Date time.Time `json:"date"`
Notes string `json:"notes,omitempty"`
}
// CreateSessionEntryRequest represents the request to add an entry to a session
type CreateSessionEntryRequest struct {
ExerciseID int64 `json:"exercise_id"`
Weight float64 `json:"weight,omitempty"`
Reps int `json:"reps,omitempty"`
SetsCompleted int `json:"sets_completed,omitempty"`
Duration int `json:"duration,omitempty"`
Distance float64 `json:"distance,omitempty"`
HeartRate int `json:"heart_rate,omitempty"`
Notes string `json:"notes,omitempty"`
}

View File

@@ -0,0 +1,109 @@
package storage
import (
"context"
"database/sql"
"fmt"
"os"
_ "github.com/lib/pq"
)
type DB struct {
*sql.DB
}
func NewDB(ctx context.Context) (*DB, error) {
host := getEnv("DB_HOST", "localhost")
port := getEnv("DB_PORT", "5432")
user := getEnv("DB_USER", "postgres")
password := getEnv("DB_PASSWORD", "postgres")
dbname := getEnv("DB_NAME", "training_tracker")
connStr := fmt.Sprintf(
"host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
host, port, user, password, dbname,
)
db, err := openDB(connStr)
if err != nil {
return nil, err
}
if err := db.PingContext(ctx); err != nil {
return nil, fmt.Errorf("failed to ping database: %w", err)
}
return &DB{db}, nil
}
func openDB(connStr string) (*sql.DB, error) {
db, err := sql.Open("postgres", connStr)
if err != nil {
return nil, fmt.Errorf("failed to open database: %w", err)
}
return db, nil
}
func (db *DB) Migrate(ctx context.Context) error {
migrations := []string{
`CREATE TABLE IF NOT EXISTS exercises (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
type VARCHAR(50) NOT NULL,
muscle_group VARCHAR(100),
description TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)`,
`CREATE TABLE IF NOT EXISTS training_plans (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
description TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)`,
`CREATE TABLE IF NOT EXISTS plan_exercises (
id SERIAL PRIMARY KEY,
plan_id INTEGER REFERENCES training_plans(id) ON DELETE CASCADE,
exercise_id INTEGER REFERENCES exercises(id) ON DELETE CASCADE,
sets INTEGER,
reps INTEGER,
duration INTEGER,
sort_order INTEGER DEFAULT 0
)`,
`CREATE TABLE IF NOT EXISTS sessions (
id SERIAL PRIMARY KEY,
plan_id INTEGER REFERENCES training_plans(id) ON DELETE SET NULL,
date TIMESTAMP NOT NULL,
notes TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)`,
`CREATE TABLE IF NOT EXISTS session_entries (
id SERIAL PRIMARY KEY,
session_id INTEGER REFERENCES sessions(id) ON DELETE CASCADE,
exercise_id INTEGER REFERENCES exercises(id) ON DELETE CASCADE,
weight DECIMAL(10, 2),
reps INTEGER,
sets_completed INTEGER,
duration INTEGER,
distance DECIMAL(10, 2),
heart_rate INTEGER,
notes TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)`,
}
for _, migration := range migrations {
if _, err := db.ExecContext(ctx, migration); err != nil {
return fmt.Errorf("migration failed: %w", err)
}
}
return nil
}
func getEnv(key, defaultValue string) string {
if value := os.Getenv(key); value != "" {
return value
}
return defaultValue
}

View File

@@ -0,0 +1,121 @@
package storage
import (
"context"
"database/sql"
"fmt"
"training-tracker/internal/models"
)
type ExerciseRepository struct {
db *DB
}
func NewExerciseRepository(db *DB) *ExerciseRepository {
return &ExerciseRepository{db: db}
}
func (r *ExerciseRepository) List(ctx context.Context) ([]models.Exercise, error) {
query := `SELECT id, name, type, muscle_group, description, created_at
FROM exercises ORDER BY name`
rows, err := r.db.QueryContext(ctx, query)
if err != nil {
return nil, fmt.Errorf("failed to query exercises: %w", err)
}
defer rows.Close()
var exercises []models.Exercise
for rows.Next() {
var e models.Exercise
var muscleGroup, description sql.NullString
if err := rows.Scan(&e.ID, &e.Name, &e.Type, &muscleGroup, &description, &e.CreatedAt); err != nil {
return nil, fmt.Errorf("failed to scan exercise: %w", err)
}
e.MuscleGroup = muscleGroup.String
e.Description = description.String
exercises = append(exercises, e)
}
return exercises, nil
}
func (r *ExerciseRepository) GetByID(ctx context.Context, id int64) (*models.Exercise, error) {
query := `SELECT id, name, type, muscle_group, description, created_at
FROM exercises WHERE id = $1`
var e models.Exercise
var muscleGroup, description sql.NullString
err := r.db.QueryRowContext(ctx, query, id).Scan(&e.ID, &e.Name, &e.Type, &muscleGroup, &description, &e.CreatedAt)
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("failed to get exercise: %w", err)
}
e.MuscleGroup = muscleGroup.String
e.Description = description.String
return &e, nil
}
func (r *ExerciseRepository) Create(ctx context.Context, req *models.CreateExerciseRequest) (*models.Exercise, error) {
query := `INSERT INTO exercises (name, type, muscle_group, description)
VALUES ($1, $2, $3, $4)
RETURNING id, name, type, muscle_group, description, created_at`
var e models.Exercise
var muscleGroup, description sql.NullString
err := r.db.QueryRowContext(ctx, query, req.Name, req.Type, nullString(req.MuscleGroup), nullString(req.Description)).
Scan(&e.ID, &e.Name, &e.Type, &muscleGroup, &description, &e.CreatedAt)
if err != nil {
return nil, fmt.Errorf("failed to create exercise: %w", err)
}
e.MuscleGroup = muscleGroup.String
e.Description = description.String
return &e, nil
}
func (r *ExerciseRepository) Update(ctx context.Context, id int64, req *models.CreateExerciseRequest) (*models.Exercise, error) {
query := `UPDATE exercises
SET name = $1, type = $2, muscle_group = $3, description = $4
WHERE id = $5
RETURNING id, name, type, muscle_group, description, created_at`
var e models.Exercise
var muscleGroup, description sql.NullString
err := r.db.QueryRowContext(ctx, query, req.Name, req.Type, nullString(req.MuscleGroup), nullString(req.Description), id).
Scan(&e.ID, &e.Name, &e.Type, &muscleGroup, &description, &e.CreatedAt)
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("failed to update exercise: %w", err)
}
e.MuscleGroup = muscleGroup.String
e.Description = description.String
return &e, nil
}
func (r *ExerciseRepository) Delete(ctx context.Context, id int64) error {
query := `DELETE FROM exercises WHERE id = $1`
result, err := r.db.ExecContext(ctx, query, id)
if err != nil {
return fmt.Errorf("failed to delete exercise: %w", err)
}
rows, _ := result.RowsAffected()
if rows == 0 {
return sql.ErrNoRows
}
return nil
}
func nullString(s string) sql.NullString {
if s == "" {
return sql.NullString{}
}
return sql.NullString{String: s, Valid: true}
}

View File

@@ -0,0 +1,183 @@
package storage
import (
"context"
"database/sql"
"fmt"
"training-tracker/internal/models"
)
type PlanRepository struct {
db *DB
}
func NewPlanRepository(db *DB) *PlanRepository {
return &PlanRepository{db: db}
}
func (r *PlanRepository) List(ctx context.Context) ([]models.TrainingPlan, error) {
query := `SELECT id, name, description, created_at FROM training_plans ORDER BY name`
rows, err := r.db.QueryContext(ctx, query)
if err != nil {
return nil, fmt.Errorf("failed to query plans: %w", err)
}
defer rows.Close()
var plans []models.TrainingPlan
for rows.Next() {
var p models.TrainingPlan
var description sql.NullString
if err := rows.Scan(&p.ID, &p.Name, &description, &p.CreatedAt); err != nil {
return nil, fmt.Errorf("failed to scan plan: %w", err)
}
p.Description = description.String
plans = append(plans, p)
}
return plans, nil
}
func (r *PlanRepository) GetByID(ctx context.Context, id int64) (*models.TrainingPlan, error) {
query := `SELECT id, name, description, created_at FROM training_plans WHERE id = $1`
var p models.TrainingPlan
var description sql.NullString
err := r.db.QueryRowContext(ctx, query, id).Scan(&p.ID, &p.Name, &description, &p.CreatedAt)
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("failed to get plan: %w", err)
}
p.Description = description.String
exerciseQuery := `
SELECT pe.id, pe.plan_id, pe.exercise_id, pe.sets, pe.reps, pe.duration, pe.sort_order,
e.id, e.name, e.type, e.muscle_group, e.description, e.created_at
FROM plan_exercises pe
JOIN exercises e ON pe.exercise_id = e.id
WHERE pe.plan_id = $1
ORDER BY pe.sort_order`
rows, err := r.db.QueryContext(ctx, exerciseQuery, id)
if err != nil {
return nil, fmt.Errorf("failed to query plan exercises: %w", err)
}
defer rows.Close()
for rows.Next() {
var pe models.PlanExercise
var sets, reps, duration sql.NullInt64
var muscleGroup, exerciseDesc sql.NullString
if err := rows.Scan(
&pe.ID, &pe.PlanID, &pe.ExerciseID, &sets, &reps, &duration, &pe.Order,
&pe.Exercise.ID, &pe.Exercise.Name, &pe.Exercise.Type, &muscleGroup, &exerciseDesc, &pe.Exercise.CreatedAt,
); err != nil {
return nil, fmt.Errorf("failed to scan plan exercise: %w", err)
}
pe.Sets = int(sets.Int64)
pe.Reps = int(reps.Int64)
pe.Duration = int(duration.Int64)
pe.Exercise.MuscleGroup = muscleGroup.String
pe.Exercise.Description = exerciseDesc.String
p.Exercises = append(p.Exercises, pe)
}
return &p, nil
}
func (r *PlanRepository) Create(ctx context.Context, req *models.CreatePlanRequest) (*models.TrainingPlan, error) {
tx, err := r.db.BeginTx(ctx, nil)
if err != nil {
return nil, fmt.Errorf("failed to begin transaction: %w", err)
}
defer tx.Rollback()
query := `INSERT INTO training_plans (name, description)
VALUES ($1, $2)
RETURNING id, name, description, created_at`
var p models.TrainingPlan
var description sql.NullString
err = tx.QueryRowContext(ctx, query, req.Name, nullString(req.Description)).
Scan(&p.ID, &p.Name, &description, &p.CreatedAt)
if err != nil {
return nil, fmt.Errorf("failed to create plan: %w", err)
}
p.Description = description.String
for _, pe := range req.Exercises {
exerciseQuery := `INSERT INTO plan_exercises (plan_id, exercise_id, sets, reps, duration, sort_order)
VALUES ($1, $2, $3, $4, $5, $6)`
_, err = tx.ExecContext(ctx, exerciseQuery, p.ID, pe.ExerciseID, nullInt(pe.Sets), nullInt(pe.Reps), nullInt(pe.Duration), pe.Order)
if err != nil {
return nil, fmt.Errorf("failed to create plan exercise: %w", err)
}
}
if err := tx.Commit(); err != nil {
return nil, fmt.Errorf("failed to commit transaction: %w", err)
}
return r.GetByID(ctx, p.ID)
}
func (r *PlanRepository) Update(ctx context.Context, id int64, req *models.CreatePlanRequest) (*models.TrainingPlan, error) {
tx, err := r.db.BeginTx(ctx, nil)
if err != nil {
return nil, fmt.Errorf("failed to begin transaction: %w", err)
}
defer tx.Rollback()
query := `UPDATE training_plans SET name = $1, description = $2 WHERE id = $3`
result, err := tx.ExecContext(ctx, query, req.Name, nullString(req.Description), id)
if err != nil {
return nil, fmt.Errorf("failed to update plan: %w", err)
}
rows, _ := result.RowsAffected()
if rows == 0 {
return nil, nil
}
_, err = tx.ExecContext(ctx, `DELETE FROM plan_exercises WHERE plan_id = $1`, id)
if err != nil {
return nil, fmt.Errorf("failed to delete plan exercises: %w", err)
}
for _, pe := range req.Exercises {
exerciseQuery := `INSERT INTO plan_exercises (plan_id, exercise_id, sets, reps, duration, sort_order)
VALUES ($1, $2, $3, $4, $5, $6)`
_, err = tx.ExecContext(ctx, exerciseQuery, id, pe.ExerciseID, nullInt(pe.Sets), nullInt(pe.Reps), nullInt(pe.Duration), pe.Order)
if err != nil {
return nil, fmt.Errorf("failed to create plan exercise: %w", err)
}
}
if err := tx.Commit(); err != nil {
return nil, fmt.Errorf("failed to commit transaction: %w", err)
}
return r.GetByID(ctx, id)
}
func (r *PlanRepository) Delete(ctx context.Context, id int64) error {
query := `DELETE FROM training_plans WHERE id = $1`
result, err := r.db.ExecContext(ctx, query, id)
if err != nil {
return fmt.Errorf("failed to delete plan: %w", err)
}
rows, _ := result.RowsAffected()
if rows == 0 {
return sql.ErrNoRows
}
return nil
}
func nullInt(i int) sql.NullInt64 {
if i == 0 {
return sql.NullInt64{}
}
return sql.NullInt64{Int64: int64(i), Valid: true}
}

View File

@@ -0,0 +1,239 @@
package storage
import (
"context"
"database/sql"
"fmt"
"training-tracker/internal/models"
)
type SessionRepository struct {
db *DB
}
func NewSessionRepository(db *DB) *SessionRepository {
return &SessionRepository{db: db}
}
func (r *SessionRepository) List(ctx context.Context) ([]models.Session, error) {
query := `
SELECT s.id, s.plan_id, s.date, s.notes, s.created_at,
p.id, p.name, p.description, p.created_at
FROM sessions s
LEFT JOIN training_plans p ON s.plan_id = p.id
ORDER BY s.date DESC`
rows, err := r.db.QueryContext(ctx, query)
if err != nil {
return nil, fmt.Errorf("failed to query sessions: %w", err)
}
defer rows.Close()
var sessions []models.Session
for rows.Next() {
var s models.Session
var planID sql.NullInt64
var notes sql.NullString
var planIDVal, planName, planDesc sql.NullString
var planCreatedAt sql.NullTime
if err := rows.Scan(
&s.ID, &planID, &s.Date, &notes, &s.CreatedAt,
&planIDVal, &planName, &planDesc, &planCreatedAt,
); err != nil {
return nil, fmt.Errorf("failed to scan session: %w", err)
}
s.Notes = notes.String
if planID.Valid {
pid := planID.Int64
s.PlanID = &pid
s.Plan = &models.TrainingPlan{
ID: pid,
Name: planName.String,
Description: planDesc.String,
CreatedAt: planCreatedAt.Time,
}
}
sessions = append(sessions, s)
}
return sessions, nil
}
func (r *SessionRepository) GetByID(ctx context.Context, id int64) (*models.Session, error) {
query := `
SELECT s.id, s.plan_id, s.date, s.notes, s.created_at,
p.id, p.name, p.description, p.created_at
FROM sessions s
LEFT JOIN training_plans p ON s.plan_id = p.id
WHERE s.id = $1`
var s models.Session
var planID sql.NullInt64
var notes sql.NullString
var planIDVal, planName, planDesc sql.NullString
var planCreatedAt sql.NullTime
err := r.db.QueryRowContext(ctx, query, id).Scan(
&s.ID, &planID, &s.Date, &notes, &s.CreatedAt,
&planIDVal, &planName, &planDesc, &planCreatedAt,
)
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("failed to get session: %w", err)
}
s.Notes = notes.String
if planID.Valid {
pid := planID.Int64
s.PlanID = &pid
s.Plan = &models.TrainingPlan{
ID: pid,
Name: planName.String,
Description: planDesc.String,
CreatedAt: planCreatedAt.Time,
}
}
entriesQuery := `
SELECT se.id, se.session_id, se.exercise_id, se.weight, se.reps, se.sets_completed,
se.duration, se.distance, se.heart_rate, se.notes, se.created_at,
e.id, e.name, e.type, e.muscle_group, e.description, e.created_at
FROM session_entries se
JOIN exercises e ON se.exercise_id = e.id
WHERE se.session_id = $1
ORDER BY se.created_at`
rows, err := r.db.QueryContext(ctx, entriesQuery, id)
if err != nil {
return nil, fmt.Errorf("failed to query session entries: %w", err)
}
defer rows.Close()
for rows.Next() {
var se models.SessionEntry
var weight, distance sql.NullFloat64
var reps, setsCompleted, duration, heartRate sql.NullInt64
var entryNotes, muscleGroup, exerciseDesc sql.NullString
var exercise models.Exercise
if err := rows.Scan(
&se.ID, &se.SessionID, &se.ExerciseID, &weight, &reps, &setsCompleted,
&duration, &distance, &heartRate, &entryNotes, &se.CreatedAt,
&exercise.ID, &exercise.Name, &exercise.Type, &muscleGroup, &exerciseDesc, &exercise.CreatedAt,
); err != nil {
return nil, fmt.Errorf("failed to scan session entry: %w", err)
}
se.Weight = weight.Float64
se.Reps = int(reps.Int64)
se.SetsCompleted = int(setsCompleted.Int64)
se.Duration = int(duration.Int64)
se.Distance = distance.Float64
se.HeartRate = int(heartRate.Int64)
se.Notes = entryNotes.String
exercise.MuscleGroup = muscleGroup.String
exercise.Description = exerciseDesc.String
se.Exercise = &exercise
s.Entries = append(s.Entries, se)
}
return &s, nil
}
func (r *SessionRepository) Create(ctx context.Context, req *models.CreateSessionRequest) (*models.Session, error) {
query := `INSERT INTO sessions (plan_id, date, notes)
VALUES ($1, $2, $3)
RETURNING id, plan_id, date, notes, created_at`
var s models.Session
var planID sql.NullInt64
var notes sql.NullString
var reqPlanID sql.NullInt64
if req.PlanID != nil {
reqPlanID = sql.NullInt64{Int64: *req.PlanID, Valid: true}
}
err := r.db.QueryRowContext(ctx, query, reqPlanID, req.Date, nullString(req.Notes)).
Scan(&s.ID, &planID, &s.Date, &notes, &s.CreatedAt)
if err != nil {
return nil, fmt.Errorf("failed to create session: %w", err)
}
s.Notes = notes.String
if planID.Valid {
pid := planID.Int64
s.PlanID = &pid
}
return &s, nil
}
func (r *SessionRepository) Update(ctx context.Context, id int64, req *models.CreateSessionRequest) (*models.Session, error) {
var reqPlanID sql.NullInt64
if req.PlanID != nil {
reqPlanID = sql.NullInt64{Int64: *req.PlanID, Valid: true}
}
query := `UPDATE sessions SET plan_id = $1, date = $2, notes = $3 WHERE id = $4`
result, err := r.db.ExecContext(ctx, query, reqPlanID, req.Date, nullString(req.Notes), id)
if err != nil {
return nil, fmt.Errorf("failed to update session: %w", err)
}
rows, _ := result.RowsAffected()
if rows == 0 {
return nil, nil
}
return r.GetByID(ctx, id)
}
func (r *SessionRepository) Delete(ctx context.Context, id int64) error {
query := `DELETE FROM sessions WHERE id = $1`
result, err := r.db.ExecContext(ctx, query, id)
if err != nil {
return fmt.Errorf("failed to delete session: %w", err)
}
rows, _ := result.RowsAffected()
if rows == 0 {
return sql.ErrNoRows
}
return nil
}
func (r *SessionRepository) AddEntry(ctx context.Context, sessionID int64, req *models.CreateSessionEntryRequest) (*models.SessionEntry, error) {
query := `INSERT INTO session_entries (session_id, exercise_id, weight, reps, sets_completed, duration, distance, heart_rate, notes)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
RETURNING id, session_id, exercise_id, weight, reps, sets_completed, duration, distance, heart_rate, notes, created_at`
var se models.SessionEntry
var weight, distance sql.NullFloat64
var reps, setsCompleted, duration, heartRate sql.NullInt64
var notes sql.NullString
err := r.db.QueryRowContext(ctx, query, sessionID, req.ExerciseID,
nullFloat(req.Weight), nullInt(req.Reps), nullInt(req.SetsCompleted),
nullInt(req.Duration), nullFloat(req.Distance), nullInt(req.HeartRate),
nullString(req.Notes)).
Scan(&se.ID, &se.SessionID, &se.ExerciseID, &weight, &reps, &setsCompleted, &duration, &distance, &heartRate, &notes, &se.CreatedAt)
if err != nil {
return nil, fmt.Errorf("failed to create session entry: %w", err)
}
se.Weight = weight.Float64
se.Reps = int(reps.Int64)
se.SetsCompleted = int(setsCompleted.Int64)
se.Duration = int(duration.Int64)
se.Distance = distance.Float64
se.HeartRate = int(heartRate.Int64)
se.Notes = notes.String
return &se, nil
}
func nullFloat(f float64) sql.NullFloat64 {
if f == 0 {
return sql.NullFloat64{}
}
return sql.NullFloat64{Float64: f, Valid: true}
}