fix: correct user context type extraction in CreateLibrary handler
- Use c.Get("user").(database.Users) instead of c.Get("user_id").(string)
- Extract userUUID from user.ID.Bytes ([16]byte)
- Properly convert to pgtype.UUID for service layer
- Remove unnecessary uuid.Parse call
This fixes 500 Internal Server Error when creating libraries via API.
The JWT middleware sets user as database.Users struct, not string.
Related to Bruno Create Library request testing.
This commit is contained in:
@@ -56,6 +56,7 @@ func (h *LibraryHandler) GetLibraryTypes(c echo.Context) error {
|
||||
// CreateLibrary creates a new library
|
||||
func (h *LibraryHandler) CreateLibrary(c echo.Context) error {
|
||||
user := c.Get("user").(database.Users)
|
||||
userUUID := user.ID.Bytes
|
||||
|
||||
var req CreateLibraryRequest
|
||||
if err := c.Bind(&req); err != nil {
|
||||
@@ -70,7 +71,7 @@ func (h *LibraryHandler) CreateLibrary(c echo.Context) error {
|
||||
req.Name,
|
||||
req.Description,
|
||||
req.Type,
|
||||
user.ID,
|
||||
pgtype.UUID{Bytes: userUUID, Valid: true},
|
||||
)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||
@@ -106,9 +107,13 @@ func (h *LibraryHandler) ListLibraries(c echo.Context) error {
|
||||
|
||||
// GetUserVisibleLibraries retrieves libraries visible to the current user
|
||||
func (h *LibraryHandler) GetUserVisibleLibraries(c echo.Context) error {
|
||||
user := c.Get("user").(database.Users)
|
||||
userID := c.Get("user_id").(string)
|
||||
userUUID, err := uuid.Parse(userID)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid user id"})
|
||||
}
|
||||
|
||||
libraries, err := h.libraryService.GetUserVisibleLibraries(c.Request().Context(), user.ID)
|
||||
libraries, err := h.libraryService.GetUserVisibleLibraries(c.Request().Context(), pgtype.UUID{Bytes: userUUID, Valid: true})
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user