fix: updated handlers to use new DecodeJSON generic function
This commit is contained in:
@@ -32,7 +32,7 @@ func NewAuthHandler(authService *domain.AuthService) *AuthHandler {
|
|||||||
func (h *AuthHandler) Login(w http.ResponseWriter, r *http.Request) {
|
func (h *AuthHandler) Login(w http.ResponseWriter, r *http.Request) {
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
|
|
||||||
payload, err := request.DecodeLogin(r)
|
payload, err := request.DecodeJSON[request.LoginPayload](r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.ErrorContext(ctx, "failed to decode login payload", slog.Any("error", err))
|
slog.ErrorContext(ctx, "failed to decode login payload", slog.Any("error", err))
|
||||||
response.Error(ctx, w, http.StatusBadRequest, "failed to decode request body")
|
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) {
|
func (h *AuthHandler) Refresh(w http.ResponseWriter, r *http.Request) {
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
|
|
||||||
payload, err := request.DecodeRefresh(r)
|
payload, err := request.DecodeJSON[request.RefreshPayload](r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.ErrorContext(ctx, "failed to decode refresh payload", slog.Any("error", err))
|
slog.ErrorContext(ctx, "failed to decode refresh payload", slog.Any("error", err))
|
||||||
response.Error(ctx, w, http.StatusBadRequest, "failed to decode request body")
|
response.Error(ctx, w, http.StatusBadRequest, "failed to decode request body")
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ func NewListHandler(listService *domain.ListService) *ListHandler {
|
|||||||
func (h *ListHandler) CreateList(w http.ResponseWriter, r *http.Request) {
|
func (h *ListHandler) CreateList(w http.ResponseWriter, r *http.Request) {
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
|
|
||||||
payload, err := request.DecodeCreateList(r)
|
payload, err := request.DecodeJSON[request.CreateListPayload](r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.ErrorContext(ctx, "failed to decode create list payload", slog.Any("error", err))
|
slog.ErrorContext(ctx, "failed to decode create list payload", slog.Any("error", err))
|
||||||
response.Error(ctx, w, http.StatusBadRequest, "failed to decode request body")
|
response.Error(ctx, w, http.StatusBadRequest, "failed to decode request body")
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ func NewUserHandler(userService *domain.UserService) *UserHandler {
|
|||||||
func (h *UserHandler) CreateUser(w http.ResponseWriter, r *http.Request) {
|
func (h *UserHandler) CreateUser(w http.ResponseWriter, r *http.Request) {
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
|
|
||||||
payload, err := request.DecodeCreateUser(r)
|
payload, err := request.DecodeJSON[request.CreateUserPayload](r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.ErrorContext(ctx, "failed to decode create user payload", slog.Any("error", err))
|
slog.ErrorContext(ctx, "failed to decode create user payload", slog.Any("error", err))
|
||||||
response.Error(ctx, w, http.StatusBadRequest, "failed to decode request body")
|
response.Error(ctx, w, http.StatusBadRequest, "failed to decode request body")
|
||||||
|
|||||||
@@ -1,11 +1,5 @@
|
|||||||
package request
|
package request
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
type LoginPayload struct {
|
type LoginPayload struct {
|
||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
@@ -14,29 +8,3 @@ type LoginPayload struct {
|
|||||||
type RefreshPayload struct {
|
type RefreshPayload struct {
|
||||||
RefreshToken string `json:"refresh_token"`
|
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
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,25 +1,27 @@
|
|||||||
package request
|
package request
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
type CreateListPayload struct {
|
type CreateListPayload struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
UserIDs []string `json:"user_ids"`
|
UserIDs []string `json:"user_ids"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func DecodeCreateList(r *http.Request) (CreateListPayload, error) {
|
type AddUserToListPayload struct {
|
||||||
var payload CreateListPayload
|
ListID string `json:"list_id"`
|
||||||
|
UserID string `json:"user_id"`
|
||||||
|
}
|
||||||
|
|
||||||
decoder := json.NewDecoder(r.Body)
|
type RemoveUserFromListPayload struct {
|
||||||
decoder.DisallowUnknownFields()
|
ListID string `json:"list_id"`
|
||||||
|
UserID string `json:"user_id"`
|
||||||
|
}
|
||||||
|
|
||||||
if err := decoder.Decode(&payload); err != nil {
|
type CreateListItemPayload struct {
|
||||||
return payload, fmt.Errorf("error decoding create list payload: %w", err)
|
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"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,26 +1,7 @@
|
|||||||
package request
|
package request
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
type CreateUserPayload struct {
|
type CreateUserPayload struct {
|
||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Password string `json:"password"`
|
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
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user