This repository has been archived on 2025-08-09. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
web-app-template/db/workout.go
Tim Wundenberg d3ff302d3e
Some checks failed
Build Docker Image / Build-Docker-Image (push) Failing after 2m8s
fix: more refactoring #181
2024-10-12 21:57:39 +02:00

61 lines
1.4 KiB
Go

package db
import (
"me-fit/types"
"me-fit/utils"
"database/sql"
"time"
"github.com/google/uuid"
)
type DbWorkout interface {
InsertWorkout(userId uuid.UUID, workout *WorkoutInsert) (*Workout, error)
}
type DbWorkoutSqlite struct {
db *sql.DB
}
func NewDbWorkoutSqlite(db *sql.DB) *DbWorkoutSqlite {
return &DbWorkoutSqlite{db: db}
}
type WorkoutInsert struct {
Date time.Time
Type string
Sets int
Reps int
}
type Workout struct {
RowId int
Date time.Time
Type string
Sets int
Reps int
}
func NewWorkoutInsert(date time.Time, workoutType string, sets int, reps int) *WorkoutInsert {
return &WorkoutInsert{Date: date, Type: workoutType, Sets: sets, Reps: reps}
}
func NewWorkoutFromInsert(rowId int, workoutInsert *WorkoutInsert) *Workout {
return &Workout{RowId: rowId, Date: workoutInsert.Date, Type: workoutInsert.Type, Sets: workoutInsert.Sets, Reps: workoutInsert.Reps}
}
func (db DbWorkoutSqlite) InsertWorkout(userId uuid.UUID, workout *WorkoutInsert) (*Workout, error) {
var rowId int
err := db.db.QueryRow(`
INSERT INTO workout (user_id, date, type, sets, reps)
VALUES (?, ?, ?, ?, ?)
RETURNING rowid`, userId, workout.Date, workout.Type, workout.Sets, workout.Reps).Scan(&rowId)
if err != nil {
utils.LogError("Error inserting workout", err)
return nil, types.ErrInternal
}
return NewWorkoutFromInsert(rowId, workout), nil
}