From 93fd79560cfb25a05e1376e5b8bd64216627392d Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Fri, 14 Aug 2026 16:58:52 +0200 Subject: [PATCH] fix: updated handlers to use new DecodeJSON generic function --- internal/api/handler/auth.go | 4 ++-- internal/api/handler/list.go | 2 +- internal/api/handler/user.go | 2 +- internal/api/request/auth.go | 32 -------------------------------- internal/api/request/list.go | 30 ++++++++++++++++-------------- internal/api/request/user.go | 19 ------------------- 6 files changed, 20 insertions(+), 69 deletions(-) diff --git a/internal/api/handler/auth.go b/internal/api/handler/auth.go index efdab88..f756e56 100644 --- a/internal/api/handler/auth.go +++ b/internal/api/handler/auth.go @@ -32,7 +32,7 @@ func NewAuthHandler(authService *domain.AuthService) *AuthHandler { func (h *AuthHandler) Login(w http.ResponseWriter, r *http.Request) { ctx := r.Context() - payload, err := request.DecodeLogin(r) + payload, err := request.DecodeJSON[request.LoginPayload](r) if err != nil { slog.ErrorContext(ctx, "failed to decode login payload", slog.Any("error", err)) response.Error(ctx, w, http.StatusBadRequest, "failed to decode request body") @@ -64,7 +64,7 @@ func (h *AuthHandler) Login(w http.ResponseWriter, r *http.Request) { func (h *AuthHandler) Refresh(w http.ResponseWriter, r *http.Request) { ctx := r.Context() - payload, err := request.DecodeRefresh(r) + payload, err := request.DecodeJSON[request.RefreshPayload](r) if err != nil { slog.ErrorContext(ctx, "failed to decode refresh payload", slog.Any("error", err)) response.Error(ctx, w, http.StatusBadRequest, "failed to decode request body") diff --git a/internal/api/handler/list.go b/internal/api/handler/list.go index 2adbfc1..677dfdf 100644 --- a/internal/api/handler/list.go +++ b/internal/api/handler/list.go @@ -32,7 +32,7 @@ func NewListHandler(listService *domain.ListService) *ListHandler { func (h *ListHandler) CreateList(w http.ResponseWriter, r *http.Request) { ctx := r.Context() - payload, err := request.DecodeCreateList(r) + payload, err := request.DecodeJSON[request.CreateListPayload](r) if err != nil { slog.ErrorContext(ctx, "failed to decode create list payload", slog.Any("error", err)) response.Error(ctx, w, http.StatusBadRequest, "failed to decode request body") diff --git a/internal/api/handler/user.go b/internal/api/handler/user.go index 38753f7..16b16f5 100644 --- a/internal/api/handler/user.go +++ b/internal/api/handler/user.go @@ -32,7 +32,7 @@ func NewUserHandler(userService *domain.UserService) *UserHandler { func (h *UserHandler) CreateUser(w http.ResponseWriter, r *http.Request) { ctx := r.Context() - payload, err := request.DecodeCreateUser(r) + payload, err := request.DecodeJSON[request.CreateUserPayload](r) if err != nil { slog.ErrorContext(ctx, "failed to decode create user payload", slog.Any("error", err)) response.Error(ctx, w, http.StatusBadRequest, "failed to decode request body") diff --git a/internal/api/request/auth.go b/internal/api/request/auth.go index e69fe74..5d5f1c3 100644 --- a/internal/api/request/auth.go +++ b/internal/api/request/auth.go @@ -1,11 +1,5 @@ package request -import ( - "encoding/json" - "fmt" - "net/http" -) - type LoginPayload struct { Email string `json:"email"` Password string `json:"password"` @@ -14,29 +8,3 @@ type LoginPayload struct { type RefreshPayload struct { RefreshToken string `json:"refresh_token"` } - -func DecodeLogin(r *http.Request) (LoginPayload, error) { - var payload LoginPayload - - decoder := json.NewDecoder(r.Body) - decoder.DisallowUnknownFields() - - if err := decoder.Decode(&payload); err != nil { - return payload, fmt.Errorf("error decoding login payload: %w", err) - } - - return payload, nil -} - -func DecodeRefresh(r *http.Request) (RefreshPayload, error) { - var payload RefreshPayload - - decoder := json.NewDecoder(r.Body) - decoder.DisallowUnknownFields() - - if err := decoder.Decode(&payload); err != nil { - return payload, fmt.Errorf("error decoding refresh payload: %w", err) - } - - return payload, nil -} diff --git a/internal/api/request/list.go b/internal/api/request/list.go index f9c88e3..331d23a 100644 --- a/internal/api/request/list.go +++ b/internal/api/request/list.go @@ -1,25 +1,27 @@ package request -import ( - "encoding/json" - "fmt" - "net/http" -) - type CreateListPayload struct { Name string `json:"name"` UserIDs []string `json:"user_ids"` } -func DecodeCreateList(r *http.Request) (CreateListPayload, error) { - var payload CreateListPayload +type AddUserToListPayload struct { + ListID string `json:"list_id"` + UserID string `json:"user_id"` +} - decoder := json.NewDecoder(r.Body) - decoder.DisallowUnknownFields() +type RemoveUserFromListPayload struct { + ListID string `json:"list_id"` + UserID string `json:"user_id"` +} - if err := decoder.Decode(&payload); err != nil { - return payload, fmt.Errorf("error decoding create list payload: %w", err) - } +type CreateListItemPayload struct { + ListID string `json:"list_id"` + Title string `json:"title"` +} - return payload, nil +type UpdateListItemPayload struct { + ListItemID string `json:"list_item_id"` + Title string `json:"title"` + IsCompleted bool `json:"is_completed"` } diff --git a/internal/api/request/user.go b/internal/api/request/user.go index 570a3ac..bca21b0 100644 --- a/internal/api/request/user.go +++ b/internal/api/request/user.go @@ -1,26 +1,7 @@ package request -import ( - "encoding/json" - "fmt" - "net/http" -) - type CreateUserPayload struct { Email string `json:"email"` Name string `json:"name"` Password string `json:"password"` } - -func DecodeCreateUser(r *http.Request) (CreateUserPayload, error) { - var payload CreateUserPayload - - decoder := json.NewDecoder(r.Body) - decoder.DisallowUnknownFields() - - if err := decoder.Decode(&payload); err != nil { - return payload, fmt.Errorf("error decoding create user payload: %w", err) - } - - return payload, nil -}