Skip to content

Commit

Permalink
refact: rpc: parser pkg (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
crlssn authored Dec 20, 2024
1 parent 558275b commit a459232
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 148 deletions.
13 changes: 7 additions & 6 deletions server/rpc/handlers/v1/exercise.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
apiv1 "github.com/crlssn/getstronger/server/gen/proto/api/v1"
"github.com/crlssn/getstronger/server/gen/proto/api/v1/apiv1connect"
"github.com/crlssn/getstronger/server/repo"
"github.com/crlssn/getstronger/server/rpc/parser"
"github.com/crlssn/getstronger/server/xcontext"
"github.com/crlssn/getstronger/server/xzap"
)
Expand Down Expand Up @@ -61,7 +62,7 @@ func (h *exerciseHandler) GetExercise(ctx context.Context, req *connect.Request[
}

return connect.NewResponse(&apiv1.GetExerciseResponse{
Exercise: parseExerciseToPB(exercise),
Exercise: parser.ExerciseToPB(exercise),
}), nil
}

Expand Down Expand Up @@ -112,7 +113,7 @@ func (h *exerciseHandler) UpdateExercise(ctx context.Context, req *connect.Reque

log.Info("exercise updated")
return connect.NewResponse(&apiv1.UpdateExerciseResponse{
Exercise: parseExerciseToPB(exercise),
Exercise: parser.ExerciseToPB(exercise),
}), nil
}

Expand Down Expand Up @@ -174,7 +175,7 @@ func (h *exerciseHandler) ListExercises(ctx context.Context, req *connect.Reques

log.Info("exercises listed")
return connect.NewResponse(&apiv1.ListExercisesResponse{
Exercises: parseExerciseSliceToPB(pagination.Items),
Exercises: parser.ExercisesToPB(pagination.Items),
Pagination: &apiv1.PaginationResponse{
NextPageToken: pagination.NextPageToken,
},
Expand Down Expand Up @@ -229,7 +230,7 @@ func (h *exerciseHandler) GetPreviousWorkoutSets(ctx context.Context, req *conne
return nil, connect.NewError(connect.CodeInternal, nil)
}

exerciseSets, err := parseSetSliceToExerciseSetsPB(sets, exercises)
exerciseSets, err := parser.ExerciseSetSlicesToPB(exercises, sets)
if err != nil {
log.Error("failed to parse set slice to exercise sets", zap.Error(err))
return nil, connect.NewError(connect.CodeInternal, nil)
Expand Down Expand Up @@ -262,7 +263,7 @@ func (h *exerciseHandler) GetPersonalBests(ctx context.Context, req *connect.Req
return nil, connect.NewError(connect.CodeInternal, nil)
}

personalBestSlice, err := parseExerciseSetSlicesToPB(exercises, personalBests)
personalBestSlice, err := parser.ExerciseSetSliceToPB(exercises, personalBests)
if err != nil {
log.Error("failed to parse personal best slice to pb", zap.Error(err))
return nil, connect.NewError(connect.CodeInternal, nil)
Expand Down Expand Up @@ -295,7 +296,7 @@ func (h *exerciseHandler) ListSets(ctx context.Context, req *connect.Request[api
return nil, connect.NewError(connect.CodeInternal, nil)
}

setSlice, err := parseSetSliceToPB(paginated.Items, nil)
setSlice, err := parser.SetsToPB(paginated.Items, nil)
if err != nil {
log.Error("failed to parse set slice to pb", zap.Error(err))
return nil, connect.NewError(connect.CodeInternal, nil)
Expand Down
3 changes: 2 additions & 1 deletion server/rpc/handlers/v1/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
apiv1 "github.com/crlssn/getstronger/server/gen/proto/api/v1"
"github.com/crlssn/getstronger/server/gen/proto/api/v1/apiv1connect"
"github.com/crlssn/getstronger/server/repo"
"github.com/crlssn/getstronger/server/rpc/parser"
"github.com/crlssn/getstronger/server/xcontext"
)

Expand Down Expand Up @@ -89,7 +90,7 @@ func (h *feedHandler) ListFeedItems(ctx context.Context, req *connect.Request[ap
mapPersonalBests[pb.ID] = struct{}{}
}

feedItems, err := parseFeedItemsToPB(paginated.Items, exercises, mapPersonalBests)
feedItems, err := parser.FeedItemsToPB(paginated.Items, exercises, mapPersonalBests)
if err != nil {
log.Error("failed to parse feed items", zap.Error(err))
return nil, connect.NewError(connect.CodeInternal, nil)
Expand Down
3 changes: 2 additions & 1 deletion server/rpc/handlers/v1/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
apiv1 "github.com/crlssn/getstronger/server/gen/proto/api/v1"
"github.com/crlssn/getstronger/server/gen/proto/api/v1/apiv1connect"
"github.com/crlssn/getstronger/server/repo"
"github.com/crlssn/getstronger/server/rpc/parser"
"github.com/crlssn/getstronger/server/stream"
"github.com/crlssn/getstronger/server/xcontext"
)
Expand Down Expand Up @@ -87,7 +88,7 @@ func (h *notificationHandler) ListNotifications(ctx context.Context, req *connec

return &connect.Response[apiv1.ListNotificationsResponse]{
Msg: &apiv1.ListNotificationsResponse{
Notifications: parseNotificationSliceToPB(paginated.Items, nPayloads, users, workouts),
Notifications: parser.NotificationsToPB(paginated.Items, nPayloads, users, workouts),
Pagination: &apiv1.PaginationResponse{
NextPageToken: paginated.NextPageToken,
},
Expand Down
7 changes: 4 additions & 3 deletions server/rpc/handlers/v1/routine.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
apiv1 "github.com/crlssn/getstronger/server/gen/proto/api/v1"
"github.com/crlssn/getstronger/server/gen/proto/api/v1/apiv1connect"
"github.com/crlssn/getstronger/server/repo"
"github.com/crlssn/getstronger/server/rpc/parser"
"github.com/crlssn/getstronger/server/xcontext"
)

Expand Down Expand Up @@ -83,7 +84,7 @@ func (h *routineHandler) GetRoutine(ctx context.Context, req *connect.Request[ap

log.Info("routine returned")
return connect.NewResponse(&apiv1.GetRoutineResponse{
Routine: parseRoutineToPB(routine),
Routine: parser.RoutineToPB(routine),
}), nil
}

Expand Down Expand Up @@ -148,7 +149,7 @@ func (h *routineHandler) UpdateRoutine(ctx context.Context, req *connect.Request

log.Info("routine updated")
return connect.NewResponse(&apiv1.UpdateRoutineResponse{
Routine: parseRoutineToPB(routine),
Routine: parser.RoutineToPB(routine),
}), nil
}

Expand Down Expand Up @@ -202,7 +203,7 @@ func (h *routineHandler) ListRoutines(ctx context.Context, req *connect.Request[

log.Info("routines listed")
return connect.NewResponse(&apiv1.ListRoutinesResponse{
Routines: parseRoutineSliceToPB(pagination.Items),
Routines: parser.RoutinesToPB(pagination.Items),
Pagination: &apiv1.PaginationResponse{
NextPageToken: pagination.NextPageToken,
},
Expand Down
9 changes: 5 additions & 4 deletions server/rpc/handlers/v1/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/crlssn/getstronger/server/pubsub"
"github.com/crlssn/getstronger/server/pubsub/payloads"
"github.com/crlssn/getstronger/server/repo"
"github.com/crlssn/getstronger/server/rpc/parser"
"github.com/crlssn/getstronger/server/xcontext"
)

Expand Down Expand Up @@ -48,7 +49,7 @@ func (h *userHandler) GetUser(ctx context.Context, req *connect.Request[apiv1.Ge

return &connect.Response[apiv1.GetUserResponse]{
Msg: &apiv1.GetUserResponse{
User: parseUserToPB(user, followed),
User: parser.UserToPB(user, followed),
},
}, nil
}
Expand Down Expand Up @@ -77,7 +78,7 @@ func (h *userHandler) SearchUsers(ctx context.Context, req *connect.Request[apiv
log.Info("searched users")
return &connect.Response[apiv1.SearchUsersResponse]{
Msg: &apiv1.SearchUsersResponse{
Users: parseUserSliceToPB(pagination.Items),
Users: parser.UsersToPB(pagination.Items),
Pagination: &apiv1.PaginationResponse{
NextPageToken: pagination.NextPageToken,
},
Expand Down Expand Up @@ -135,7 +136,7 @@ func (h *userHandler) ListFollowers(ctx context.Context, req *connect.Request[ap

return &connect.Response[apiv1.ListFollowersResponse]{
Msg: &apiv1.ListFollowersResponse{
Followers: parseUserSliceToPB(followers),
Followers: parser.UsersToPB(followers),
},
}, nil
}
Expand All @@ -151,7 +152,7 @@ func (h *userHandler) ListFollowees(ctx context.Context, req *connect.Request[ap

return &connect.Response[apiv1.ListFolloweesResponse]{
Msg: &apiv1.ListFolloweesResponse{
Followees: parseUserSliceToPB(followees),
Followees: parser.UsersToPB(followees),
},
}, nil
}
11 changes: 6 additions & 5 deletions server/rpc/handlers/v1/workout.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/crlssn/getstronger/server/pubsub"
"github.com/crlssn/getstronger/server/pubsub/payloads"
"github.com/crlssn/getstronger/server/repo"
"github.com/crlssn/getstronger/server/rpc/parser"
"github.com/crlssn/getstronger/server/xcontext"
)

Expand Down Expand Up @@ -56,7 +57,7 @@ func (h *workoutHandler) CreateWorkout(ctx context.Context, req *connect.Request
UserID: userID,
StartedAt: req.Msg.GetStartedAt().AsTime(),
FinishedAt: req.Msg.GetFinishedAt().AsTime(),
ExerciseSets: parseExerciseSetsFromPB(req.Msg.GetExerciseSets()),
ExerciseSets: parser.ExercisesFromPB(req.Msg.GetExerciseSets()),
})
if err != nil {
log.Error("failed to create workout", zap.Error(err))
Expand Down Expand Up @@ -119,7 +120,7 @@ func (h *workoutHandler) GetWorkout(ctx context.Context, req *connect.Request[ap
mapPersonalBests[set.ID] = struct{}{}
}

w, err := parseWorkoutToPB(workout, exercises, users, mapPersonalBests)
w, err := parser.WorkoutToPB(workout, exercises, users, mapPersonalBests)
if err != nil {
log.Error("failed to parse workout", zap.Error(err))
return nil, connect.NewError(connect.CodeInternal, nil)
Expand Down Expand Up @@ -191,7 +192,7 @@ func (h *workoutHandler) ListWorkouts(ctx context.Context, req *connect.Request[
mapPersonalBests[pb.ID] = struct{}{}
}

w, err := parseWorkoutSliceToPB(pagination.Items, exercises, users, mapPersonalBests)
w, err := parser.WorkoutsToPB(pagination.Items, exercises, users, mapPersonalBests)
if err != nil {
log.Error("failed to parse workouts", zap.Error(err))
return nil, connect.NewError(connect.CodeInternal, nil)
Expand Down Expand Up @@ -257,7 +258,7 @@ func (h *workoutHandler) PostComment(ctx context.Context, req *connect.Request[a
log.Info("workout comment posted")
return &connect.Response[apiv1.PostCommentResponse]{
Msg: &apiv1.PostCommentResponse{
Comment: parseWorkoutCommentToPB(comment, user),
Comment: parser.WorkoutCommentToPB(comment, user),
},
}, nil
}
Expand Down Expand Up @@ -293,7 +294,7 @@ func (h *workoutHandler) UpdateWorkout(ctx context.Context, req *connect.Request
return fmt.Errorf("failed to update workout: %w", err)
}

exerciseSets := parseExerciseSetsFromPB(req.Msg.GetWorkout().GetExerciseSets())
exerciseSets := parser.ExercisesFromPB(req.Msg.GetWorkout().GetExerciseSets())
if err = tx.UpdateWorkoutSets(ctx, workout.ID, exerciseSets); err != nil {
return fmt.Errorf("failed to update workout sets: %w", err)
}
Expand Down
Loading

0 comments on commit a459232

Please sign in to comment.