Enable adding/removing users to list as well as add/update list items #11
@@ -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")
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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"`
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user