fix: updated handlers to use new DecodeJSON generic function

This commit is contained in:
2026-08-14 16:58:52 +02:00
parent e0ab3bb51a
commit 93fd79560c
6 changed files with 20 additions and 69 deletions
+2 -2
View File
@@ -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")
+1 -1
View File
@@ -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")
+1 -1
View File
@@ -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")
-32
View File
@@ -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
}
+17 -15
View File
@@ -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
decoder := json.NewDecoder(r.Body)
decoder.DisallowUnknownFields()
if err := decoder.Decode(&payload); err != nil {
return payload, fmt.Errorf("error decoding create list payload: %w", err)
type AddUserToListPayload struct {
ListID string `json:"list_id"`
UserID string `json:"user_id"`
}
return payload, nil
type RemoveUserFromListPayload struct {
ListID string `json:"list_id"`
UserID string `json:"user_id"`
}
type CreateListItemPayload struct {
ListID string `json:"list_id"`
Title string `json:"title"`
}
type UpdateListItemPayload struct {
ListItemID string `json:"list_item_id"`
Title string `json:"title"`
IsCompleted bool `json:"is_completed"`
}
-19
View File
@@ -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
}