fix(dashboard): Return default preferences instead of 404

The GetPreferences API was returning 404 when no preferences existed
for a library, breaking the dashboard settings modal. Now returns
default preferences (empty hidden_collections, empty collection_order,
20 items_per_section) when no preferences are found, matching the
behavior of the frontend dashboard page.
This commit is contained in:
2026-03-02 13:48:29 -05:00
parent 38be055149
commit 6454ade2f7
+8 -2
View File
@@ -4,6 +4,7 @@ import (
"bookhoard/internal/database" "bookhoard/internal/database"
"bookhoard/internal/services" "bookhoard/internal/services"
"bookhoard/internal/utils" "bookhoard/internal/utils"
"log"
"net/http" "net/http"
"strconv" "strconv"
@@ -200,7 +201,6 @@ func (h *DashboardHandler) GetPreferences(c echo.Context) error {
userUUID := uuid.UUID(user.ID.Bytes) userUUID := uuid.UUID(user.ID.Bytes)
libraryID := c.QueryParam("library_id") libraryID := c.QueryParam("library_id")
// ✅ Add validation
if libraryID == "" { if libraryID == "" {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "library_id required"}) return c.JSON(http.StatusBadRequest, map[string]string{"error": "library_id required"})
} }
@@ -211,7 +211,13 @@ func (h *DashboardHandler) GetPreferences(c echo.Context) error {
} }
prefs, err := h.dashboardService.GetDashboardPreferences(c.Request().Context(), userUUID, libUUID) prefs, err := h.dashboardService.GetDashboardPreferences(c.Request().Context(), userUUID, libUUID)
if err != nil { if err != nil {
return c.JSON(http.StatusNotFound, map[string]string{"error": "Preferences not found"}) // Return default preferences instead of 404 when none exist
log.Printf("GetDashboardPreferences failed: %v", err)
prefs = database.UserDashboardPreferences{
HiddenCollections: []string{},
CollectionOrder: []string{},
ItemsPerSection: pgtype.Int4{Int32: 20, Valid: true},
}
} }
// Normalize nil slices to empty arrays for JSON // Normalize nil slices to empty arrays for JSON