Skip to content

Commit

Permalink
refact: use pagination func (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
crlssn authored Nov 23, 2024
1 parent 5691245 commit e41fed9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 39 deletions.
2 changes: 0 additions & 2 deletions server/rpc/v1/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ var (

func (h *auth) RefreshToken(ctx context.Context, _ *connect.Request[v1.RefreshTokenRequest]) (*connect.Response[v1.RefreshTokenResponse], error) {
log := xcontext.MustExtractLogger(ctx)

refreshToken, ok := xcontext.ExtractRefreshToken(ctx)
if !ok {
log.Warn("refresh token not provided")
Expand Down Expand Up @@ -193,7 +192,6 @@ func (h *auth) RefreshToken(ctx context.Context, _ *connect.Request[v1.RefreshTo

func (h *auth) Logout(ctx context.Context, _ *connect.Request[v1.LogoutRequest]) (*connect.Response[v1.LogoutResponse], error) {
log := xcontext.MustExtractLogger(ctx)

refreshToken, ok := xcontext.ExtractRefreshToken(ctx)
if ok {
if err := h.repo.DeleteRefreshToken(ctx, refreshToken); err != nil {
Expand Down
26 changes: 13 additions & 13 deletions server/rpc/v1/exercise.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@ import (
"github.com/crlssn/getstronger/server/pkg/orm"
v1 "github.com/crlssn/getstronger/server/pkg/pb/api/v1"
"github.com/crlssn/getstronger/server/pkg/pb/api/v1/apiv1connect"
repo2 "github.com/crlssn/getstronger/server/pkg/repo"
"github.com/crlssn/getstronger/server/pkg/repo"
"github.com/crlssn/getstronger/server/pkg/xcontext"
)

var _ apiv1connect.ExerciseServiceHandler = (*exerciseHandler)(nil)

type exerciseHandler struct {
repo *repo2.Repo
repo *repo.Repo
}

func NewExerciseHandler(r *repo2.Repo) apiv1connect.ExerciseServiceHandler {
func NewExerciseHandler(r *repo.Repo) apiv1connect.ExerciseServiceHandler {
return &exerciseHandler{r}
}

func (h *exerciseHandler) Create(ctx context.Context, req *connect.Request[v1.CreateExerciseRequest]) (*connect.Response[v1.CreateExerciseResponse], error) {
log := xcontext.MustExtractLogger(ctx)
userID := xcontext.MustExtractUserID(ctx)

exercise, err := h.repo.CreateExercise(ctx, repo2.CreateExerciseParams{
exercise, err := h.repo.CreateExercise(ctx, repo.CreateExerciseParams{
UserID: userID,
Name: req.Msg.GetName(),
Label: req.Msg.GetLabel(),
Expand All @@ -50,7 +50,7 @@ func (h *exerciseHandler) Get(ctx context.Context, req *connect.Request[v1.GetEx
log := xcontext.MustExtractLogger(ctx)
userID := xcontext.MustExtractUserID(ctx)

exercise, err := h.repo.GetExercise(ctx, repo2.GetExerciseWithID(req.Msg.GetId()))
exercise, err := h.repo.GetExercise(ctx, repo.GetExerciseWithID(req.Msg.GetId()))
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
log.Error("exercise not found", zap.String("id", req.Msg.GetId()))
Expand All @@ -77,7 +77,7 @@ func (h *exerciseHandler) Update(ctx context.Context, req *connect.Request[v1.Up
log := xcontext.MustExtractLogger(ctx)
userID := xcontext.MustExtractUserID(ctx)

exercise, err := h.repo.GetExercise(ctx, repo2.GetExerciseWithID(req.Msg.GetExercise().GetId()))
exercise, err := h.repo.GetExercise(ctx, repo.GetExerciseWithID(req.Msg.GetExercise().GetId()))
if err != nil {
log.Error("find exercise failed", zap.Error(err))
return nil, connect.NewError(connect.CodeInternal, nil)
Expand Down Expand Up @@ -115,7 +115,7 @@ func (h *exerciseHandler) Delete(ctx context.Context, req *connect.Request[v1.De
log := xcontext.MustExtractLogger(ctx)
userID := xcontext.MustExtractUserID(ctx)

if err := h.repo.SoftDeleteExercise(ctx, repo2.SoftDeleteExerciseParams{
if err := h.repo.SoftDeleteExercise(ctx, repo.SoftDeleteExerciseParams{
UserID: userID,
ExerciseID: req.Msg.GetId(),
}); err != nil {
Expand All @@ -133,18 +133,18 @@ func (h *exerciseHandler) List(ctx context.Context, req *connect.Request[v1.List

limit := int(req.Msg.GetPageSize())
exercises, err := h.repo.ListExercises(ctx,
repo2.ListExercisesWithIDs(req.Msg.GetExerciseIds()),
repo2.ListExercisesWithName(req.Msg.GetName()),
repo2.ListExercisesWithLimit(limit+1),
repo2.ListExercisesWithUserID(userID),
repo2.ListExercisesWithPageToken(req.Msg.GetPageToken()),
repo.ListExercisesWithIDs(req.Msg.GetExerciseIds()),
repo.ListExercisesWithName(req.Msg.GetName()),
repo.ListExercisesWithLimit(limit+1),
repo.ListExercisesWithUserID(userID),
repo.ListExercisesWithPageToken(req.Msg.GetPageToken()),
)
if err != nil {
log.Error("list exercises failed", zap.Error(err))
return nil, connect.NewError(connect.CodeInternal, nil)
}

pagination, err := repo2.PaginateSlice(exercises, limit, func(exercise *orm.Exercise) time.Time {
pagination, err := repo.PaginateSlice(exercises, limit, func(exercise *orm.Exercise) time.Time {
return exercise.CreatedAt
})
if err != nil {
Expand Down
20 changes: 9 additions & 11 deletions server/rpc/v1/routine.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package v1
import (
"context"
"encoding/json"
"time"

"connectrpc.com/connect"
"go.uber.org/zap"
Expand Down Expand Up @@ -162,21 +163,18 @@ func (h *routineHandler) List(ctx context.Context, req *connect.Request[v1.ListR
return nil, connect.NewError(connect.CodeInternal, nil)
}

var nextPageToken []byte
if len(routines) > limit {
routines = routines[:limit]
if nextPageToken, err = json.Marshal(repo.PageToken{
CreatedAt: routines[len(routines)-1].CreatedAt,
}); err != nil {
log.Error("marshal page token failed", zap.Error(err))
return nil, connect.NewError(connect.CodeInternal, nil)
}
pagination, err := repo.PaginateSlice(routines, limit, func(routine *orm.Routine) time.Time {
return routine.CreatedAt
})
if err != nil {
log.Error("paginate routines failed", zap.Error(err))
return nil, connect.NewError(connect.CodeInternal, nil)
}

log.Info("routines listed")
return connect.NewResponse(&v1.ListRoutinesResponse{
Routines: parseRoutinesToPB(routines),
NextPageToken: nextPageToken,
Routines: parseRoutinesToPB(pagination.Items),
NextPageToken: pagination.NextPageToken,
}), nil
}

Expand Down
26 changes: 13 additions & 13 deletions server/rpc/v1/workout.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@ import (
"github.com/crlssn/getstronger/server/pkg/orm"
v1 "github.com/crlssn/getstronger/server/pkg/pb/api/v1"
"github.com/crlssn/getstronger/server/pkg/pb/api/v1/apiv1connect"
repo2 "github.com/crlssn/getstronger/server/pkg/repo"
"github.com/crlssn/getstronger/server/pkg/repo"
"github.com/crlssn/getstronger/server/pkg/xcontext"
)

var _ apiv1connect.WorkoutServiceHandler = (*workoutHandler)(nil)

type workoutHandler struct {
repo *repo2.Repo
repo *repo.Repo
}

func NewWorkoutHandler(r *repo2.Repo) apiv1connect.WorkoutServiceHandler {
func NewWorkoutHandler(r *repo.Repo) apiv1connect.WorkoutServiceHandler {
return &workoutHandler{r}
}

func (h *workoutHandler) Create(ctx context.Context, req *connect.Request[v1.CreateWorkoutRequest]) (*connect.Response[v1.CreateWorkoutResponse], error) {
log := xcontext.MustExtractLogger(ctx)
userID := xcontext.MustExtractUserID(ctx)

routine, err := h.repo.GetRoutine(ctx, repo2.GetRoutineWithID(req.Msg.GetRoutineId()))
routine, err := h.repo.GetRoutine(ctx, repo.GetRoutineWithID(req.Msg.GetRoutineId()))
if err != nil {
log.Error("failed to get routine", zap.Error(err))
return nil, connect.NewError(connect.CodeInternal, nil)
Expand All @@ -39,7 +39,7 @@ func (h *workoutHandler) Create(ctx context.Context, req *connect.Request[v1.Cre
return nil, connect.NewError(connect.CodePermissionDenied, nil)
}

workout, err := h.repo.CreateWorkout(ctx, repo2.CreateWorkoutParams{
workout, err := h.repo.CreateWorkout(ctx, repo.CreateWorkoutParams{
Name: routine.Title,
UserID: userID,
FinishedAt: req.Msg.GetFinishedAt().AsTime(),
Expand All @@ -63,8 +63,8 @@ func (h *workoutHandler) Get(ctx context.Context, req *connect.Request[v1.GetWor
userID := xcontext.MustExtractUserID(ctx)

workout, err := h.repo.GetWorkout(ctx,
repo2.GetWorkoutWithID(req.Msg.GetId()),
repo2.GetWorkoutWithExerciseSets(),
repo.GetWorkoutWithID(req.Msg.GetId()),
repo.GetWorkoutWithExerciseSets(),
)
if err != nil {
log.Error("failed to get workout", zap.Error(err))
Expand All @@ -90,16 +90,16 @@ func (h *workoutHandler) List(ctx context.Context, req *connect.Request[v1.ListW

limit := int(req.Msg.GetPageSize())
workouts, err := h.repo.ListWorkouts(ctx,
repo2.ListWorkoutsWithLimit(limit+1),
repo2.ListWorkoutsWithUserID(userID),
repo2.ListWorkoutsWithPageToken(req.Msg.GetPageToken()),
repo.ListWorkoutsWithLimit(limit+1),
repo.ListWorkoutsWithUserID(userID),
repo.ListWorkoutsWithPageToken(req.Msg.GetPageToken()),
)
if err != nil {
log.Error("failed to list workouts", zap.Error(err))
return nil, connect.NewError(connect.CodeInternal, nil)
}

pagination, err := repo2.PaginateSlice(workouts, limit, func(workout *orm.Workout) time.Time {
pagination, err := repo.PaginateSlice(workouts, limit, func(workout *orm.Workout) time.Time {
return workout.CreatedAt
})
if err != nil {
Expand All @@ -121,8 +121,8 @@ func (h *workoutHandler) Delete(ctx context.Context, req *connect.Request[v1.Del
userID := xcontext.MustExtractUserID(ctx)

if err := h.repo.DeleteWorkout(ctx,
repo2.DeleteWorkoutWithID(req.Msg.GetId()),
repo2.DeleteWorkoutWithUserID(userID),
repo.DeleteWorkoutWithID(req.Msg.GetId()),
repo.DeleteWorkoutWithUserID(userID),
); err != nil {
log.Error("failed to delete workout", zap.Error(err))
return nil, connect.NewError(connect.CodeInternal, nil)
Expand Down

0 comments on commit e41fed9

Please sign in to comment.