# Navigation Admin 500 Error Fix ## Problem Navigation admin editing was not working - both creating and updating navigation items resulted in a 500 Internal Server Error: ``` POST http://localhost:8080/api/v1/admin/navigation 500 (Internal Server Error) ``` Navigation items were not being saved or populated in either the frontend or admin page. ## Root Cause The issue was in the `CreateNavigationItem` function in `internal/controllers/navigation_controller.go`. When calculating the display order for new navigation items (line 109-116), the original code only considered items with `parent_id IS NULL`: ```go if item.DisplayOrder == 0 { var maxOrder int nc.DB.Model(&models.NavigationItem{}). Where("parent_id IS NULL"). // ← Only checks top-level items Select("COALESCE(MAX(display_order), -1) + 1"). Scan(&maxOrder) item.DisplayOrder = maxOrder } ``` ### Issues with this approach: 1. **Parent ID not considered**: When creating a child item (with a parent_id), it would still calculate the max order from top-level items, causing incorrect ordering 2. **Admin vs Frontend mixing**: Admin navigation items and frontend navigation items were being mixed together because `requires_admin` wasn't considered 3. **Potential conflicts**: This could lead to duplicate display_order values and database constraint violations ## Solution ### 1. Fixed Display Order Calculation Updated the `CreateNavigationItem` function to properly consider: - **Parent ID**: Calculate max order within the same parent level - **Admin status**: Separate admin and frontend navigation items - **Proper scoping**: Each level (parent/child) and type (admin/frontend) maintains its own ordering ```go if item.DisplayOrder == 0 { var maxOrder int query := nc.DB.Model(&models.NavigationItem{}) // Calculate max order for items at the same level (same parent) and same admin status if item.ParentID == nil { query = query.Where("parent_id IS NULL") } else { query = query.Where("parent_id = ?", *item.ParentID) } // Also consider requires_admin to keep frontend and admin items separate query = query.Where("requires_admin = ?", item.RequiresAdmin) query.Select("COALESCE(MAX(display_order), -1) + 1").Scan(&maxOrder) item.DisplayOrder = maxOrder } ``` ### 2. Added Better Error Messages Enhanced error responses to include detailed error information for debugging: **CreateNavigationItem**: ```go if err := nc.DB.Create(&item).Error; err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "error": "Failed to create navigation item", "details": err.Error(), // ← Added detailed error }) return } ``` **UpdateNavigationItem**: ```go if err := nc.DB.Save(&item).Error; err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "error": "Failed to update navigation item", "details": err.Error(), // ← Added detailed error }) return } ``` ## Files Modified - `internal/controllers/navigation_controller.go` - Fixed `CreateNavigationItem` function (lines 108-125) - Enhanced error messages in `CreateNavigationItem` (lines 127-134) - Enhanced error messages in `UpdateNavigationItem` (lines 184-190) ## Testing Instructions 1. Start the backend server 2. Navigate to Admin → Navigace (Navigation) 3. Test creating a new navigation item: - Click "Přidat hlavní položku" (Add main item) - Fill in the form and save - Verify the item appears in the list 4. Test editing an existing item: - Click edit icon on any navigation item - Modify fields and save - Verify changes are saved 5. Test creating child items: - Create a dropdown item - Add child items to it - Verify proper ordering ## Expected Behavior After Fix - ✅ Navigation items save successfully - ✅ No 500 errors when creating or updating items - ✅ Proper ordering maintained for frontend and admin navigation separately - ✅ Child items (dropdown submenu items) order correctly within their parent - ✅ Detailed error messages shown if database issues occur ## Related Files - Frontend: `frontend/src/pages/admin/NavigationAdminPage.tsx` - Service: `frontend/src/services/navigation.ts` - Model: `internal/models/navigation.go` - Routes: `internal/routes/routes.go` (lines 332-340) - Migration: `database/migrations/20251010154600_create_navigation_system.up.sql`