exercise

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 24, 2024 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound = errors.New("not found")

	// TODO: custom error containing which fields are invalid
	ErrInvalidFields = errors.New("contains invalid fields")
)

Functions

func NewCreateHandler

func NewCreateHandler(l *slog.Logger, exercises Storer) http.Handler

func NewDeleteHandler

func NewDeleteHandler(l *slog.Logger, exercises Deleter) http.Handler

func NewDownHandler

func NewDownHandler(l *slog.Logger, exercises Orderer) http.Handler

HandleMoveDownExercise moves a workout exercise one position down increasing the exercise index with 1 but never higher than the exercise count requires {username}, {workout}, and {exercise} path variables

func NewFetchAllHandler

func NewFetchAllHandler(l *slog.Logger, exercises Retreiver) http.Handler

func NewFetchHandler

func NewFetchHandler(l *slog.Logger, exercises Retreiver) http.Handler

func NewSwapHandler

func NewSwapHandler(l *slog.Logger, exercises Orderer) http.Handler

HandleSwapExercises swaps the index of the exercise with one provided requires {username}, {workout}, and {exercise} path variables requires json payload {"with": INDEX}

func NewUpHandler

func NewUpHandler(l *slog.Logger, exercises Orderer) http.Handler

HandleMoveUpExercise moves a workout exercise one position up reducing the index with 1 but never lower than 1 requires {username}, {workout}, and {exercise} path variables

func NewUpdateHandler

func NewUpdateHandler(l *slog.Logger, exercises Updater) http.Handler

TODO merge all update functions into a single one

Types

type ByIndex

type ByIndex []Exercise

ByIndex implements sort.Interface, sorting exercises according to their index

func (ByIndex) Len

func (xs ByIndex) Len() int

func (ByIndex) Less

func (xs ByIndex) Less(i, j int) bool

func (ByIndex) Swap

func (xs ByIndex) Swap(i, j int)

type Deleter

type Deleter interface {
	// DeleteExercise deletes an exercise if exists
	// and updates the references in the linked list
	Delete(owner string, workout int, exercise int) (Exercise, error)
}

type Exercise

type Exercise struct {
	Owner       string  `json:"owner"`
	Workout     int     `json:"workout"`
	Index       int     `json:"index"`
	Name        string  `json:"name"`
	Weight      float64 `json:"weight"`
	Repetitions int     `json:"repetitions"`
}

Exercise contains details of a single workout exercise an exercise is a node in a linked list, determining the order

func (Exercise) Ref

func (e Exercise) Ref() ExerciseRef

Key prints unique identifiable key

func (Exercise) String

func (e Exercise) String() string

String prints the Exercise is a human readable format implementing the String interface

type ExerciseRef

type ExerciseRef struct {
	Username      string
	WorkoutIndex  int
	ExerciseIndex int
}

ExerciseRef represents the unique key that references an Exercise

func ParseRef

func ParseRef(s string) (ExerciseRef, error)

func (ExerciseRef) String

func (er ExerciseRef) String() string

type ExerciseStore

type ExerciseStore interface {
	Retreiver
	Storer
	Updater
	Deleter
	Orderer
}

type Orderer

type Orderer interface {
	// Swap swaps the indices from the given exercises
	// if the workout or index doesn't exist it returns an error
	Swap(owner string, workout int, e1 int, e2 int) error

	// Len returns the length of all exercises of a workout
	Len(owner string, workout int) (int, error)
}

type Retreiver

type Retreiver interface {
	// ByID takes an excercise id and returns the exercise
	// from the exercises repository if it exists
	ByID(owner string, workout int, exercise int) (Exercise, error)

	// ByWorkout returns all exercises belongign to an user's workout
	ByWorkout(owner string, workout int) ([]Exercise, error)
}

Implementation of the Retreiver interface enables querying exercises

type SQLExerciseStore

type SQLExerciseStore struct {
	*storage.SqliteDatastore
}

func NewSQLExerciseStore

func NewSQLExerciseStore(db *storage.SqliteDatastore) *SQLExerciseStore

func (*SQLExerciseStore) ByID

func (xs *SQLExerciseStore) ByID(owner string, workout int, exercise int) (Exercise, error)

ExerciseByID returns an exercise from the database if exists otherwise returns NotFound error

func (*SQLExerciseStore) ByWorkout

func (xs *SQLExerciseStore) ByWorkout(owner string, workout int) ([]Exercise, error)

func (*SQLExerciseStore) ChangeName

func (xs *SQLExerciseStore) ChangeName(owner string, workout int, exercise int, name string) (Exercise, error)

func (*SQLExerciseStore) Delete

func (xs *SQLExerciseStore) Delete(owner string, workout int, exercise int) (Exercise, error)

func (*SQLExerciseStore) Len

func (xs *SQLExerciseStore) Len(owner string, workout int) (int, error)

func (*SQLExerciseStore) New

func (xs *SQLExerciseStore) New(owner string, workout int, name string, weight float64, repetitions int) (Exercise, error)

func (*SQLExerciseStore) Swap

func (xs *SQLExerciseStore) Swap(owner string, workout int, e1 int, e2 int) error

func (*SQLExerciseStore) UpdateRepetitions

func (xs *SQLExerciseStore) UpdateRepetitions(owner string, workout int, exercise int, repetitions int) (Exercise, error)

func (*SQLExerciseStore) UpdateWeight

func (xs *SQLExerciseStore) UpdateWeight(owner string, workout int, exercise int, weight float64) (Exercise, error)

type Storer

type Storer interface {
	// New stores an exercise at the tail,
	// updating the references and returns the exercise.
	// user and workout must exist before adding exercise
	New(owner string, workout int, name string, weight float64, repetitions int) (Exercise, error)
}

Implementation of the Storer interface enables creating new exercises

type Updater

type Updater interface {
	// ChangeName updates the name of an existing exercise
	ChangeName(owner string, workout int, exercise int, newName string) (Exercise, error)

	// UpdateWeight updates the weight of an existing exercise
	UpdateWeight(owner string, workout int, exercise int, newWeight float64) (Exercise, error)

	// UpdateRepetitions updates the repetitions of an existing exercise
	UpdateRepetitions(owner string, workout int, exercise int, newRepetitions int) (Exercise, error)
}

Implemention of Updater interface enables updating exercises

type With

type With struct {
	Exercise ExerciseRef
}

With is used to represent the target exercise used to swap exercises

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL