Some checks failed
Build Docker Image / Build-Docker-Image (push) Failing after 2m8s
61 lines
1.4 KiB
Go
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
|
|
}
|