fix: in "CreateList", the authenticated userid is now added in the service layer, rather than the handler layer
This commit is contained in:
@@ -3,7 +3,6 @@ package handler
|
|||||||
import (
|
import (
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"slices"
|
|
||||||
|
|
||||||
"github.com/robindittmar/dttmr-api/internal/api/request"
|
"github.com/robindittmar/dttmr-api/internal/api/request"
|
||||||
"github.com/robindittmar/dttmr-api/internal/api/response"
|
"github.com/robindittmar/dttmr-api/internal/api/response"
|
||||||
@@ -47,12 +46,7 @@ func (h *ListHandler) CreateList(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: should this be in ListService?
|
list, err := h.ListService.Create(ctx, authContext.UserID, payload.Name, payload.UserIDs)
|
||||||
if !slices.Contains(payload.UserIDs, authContext.UserID) {
|
|
||||||
payload.UserIDs = append(payload.UserIDs, authContext.UserID)
|
|
||||||
}
|
|
||||||
|
|
||||||
list, err := h.ListService.Create(ctx, payload.Name, payload.UserIDs)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.ErrorContext(ctx, "failed to create list", slog.Any("error", err))
|
slog.ErrorContext(ctx, "failed to create list", slog.Any("error", err))
|
||||||
response.Error(ctx, w, http.StatusInternalServerError, "failed to create list")
|
response.Error(ctx, w, http.StatusInternalServerError, "failed to create list")
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package domain
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"slices"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -48,6 +49,10 @@ func (s *ListService) Create(ctx context.Context, authUserID string, name string
|
|||||||
return nil, errors.New("list name must not be empty")
|
return nil, errors.New("list name must not be empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !slices.Contains(userIDs, authUserID) {
|
||||||
|
userIDs = append(userIDs, authUserID)
|
||||||
|
}
|
||||||
|
|
||||||
return s.repo.CreateList(ctx, name, userIDs)
|
return s.repo.CreateList(ctx, name, userIDs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user