fix: more refactoring #181
Some checks failed
Build Docker Image / Build-Docker-Image (push) Failing after 2m8s

This commit is contained in:
2024-10-12 21:57:39 +02:00
parent 0fab1e1f2e
commit d3ff302d3e
13 changed files with 779 additions and 561 deletions

60
db/workout.go Normal file
View File

@@ -0,0 +1,60 @@
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
}