Skip to content

Commit

Permalink
refact: personal bests (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
crlssn authored Dec 12, 2024
1 parent 9f5ac2a commit 0f42d9a
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 359 deletions.
4 changes: 4 additions & 0 deletions database/migrations/019_drop_personal_bests.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DROP VIEW getstronger.personal_bests;

CREATE INDEX ON getstronger.sets (workout_id);
CREATE INDEX ON getstronger.workouts (user_id);
5 changes: 1 addition & 4 deletions server/pkg/orm/boil_view_names.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

324 changes: 0 additions & 324 deletions server/pkg/orm/personal_bests.go

This file was deleted.

34 changes: 19 additions & 15 deletions server/pkg/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ func ListWorkoutsWithSets() ListWorkoutsOpt {
}
}

func ListWorkoutsWithUserIDs(userIDs []string) ListWorkoutsOpt {
func ListWorkoutsWithUserIDs(userIDs ...string) ListWorkoutsOpt {
return func() ([]qm.QueryMod, error) {
return []qm.QueryMod{
orm.WorkoutWhere.UserID.IN(userIDs),
Expand Down Expand Up @@ -790,26 +790,30 @@ func (r *Repo) GetPreviousWorkoutSets(ctx context.Context, exerciseIDs []string)
return sets, nil
}

type ListPersonalBestsOpt func() qm.QueryMod

func ListPersonalBestsWithUserID(userID string) ListPersonalBestsOpt {
return func() qm.QueryMod {
return orm.PersonalBestWhere.UserID.EQ(null.StringFrom(userID))
func (r *Repo) GetPersonalBests(ctx context.Context, userID string) (orm.SetSlice, error) {
workouts, err := r.ListWorkouts(ctx, ListWorkoutsWithUserIDs(userID))
if err != nil {
return nil, fmt.Errorf("workouts fetch: %w", err)
}
}

func (r *Repo) ListPersonalBests(ctx context.Context, opts ...ListPersonalBestsOpt) (orm.PersonalBestSlice, error) {
query := make([]qm.QueryMod, 0, len(opts))
for _, opt := range opts {
query = append(query, opt())
workoutIDs := make([]string, 0, len(workouts))
for _, workout := range workouts {
workoutIDs = append(workoutIDs, workout.ID)
}

personalBests, err := orm.PersonalBests(query...).All(ctx, r.executor())
if err != nil {
return nil, fmt.Errorf("personal bests fetch: %w", err)
rawQuery := `
SELECT DISTINCT ON (exercise_id) exercise_id, weight, reps, workout_id, created_at
FROM getstronger.sets
WHERE workout_id = ANY ($1)
ORDER BY exercise_id, weight DESC, reps DESC;
`

var sets orm.SetSlice
if err = queries.Raw(rawQuery, types.Array(workoutIDs)).Bind(ctx, r.executor(), &sets); err != nil {
return nil, fmt.Errorf("sets fetch: %w", err)
}

return personalBests, nil
return sets, nil
}

type FollowParams struct {
Expand Down
Loading

0 comments on commit 0f42d9a

Please sign in to comment.