Complete tasks 4.1-4.2: Page management service and HTTP endpoints
- Implemented PageService with full CRUD operations - Added GetPages, CreatePage, UpdatePage, DeletePage, ReorderPages methods - Cascade deletion of widgets when page is deleted - Prevention of last page deletion - Created page HTTP endpoints (GET, POST, PUT, DELETE, reorder) - HTMX-friendly HTML fragment responses - Comprehensive unit tests for service and handlers - Updated dashboard to use PageService and create default pages
This commit is contained in:
@@ -7,21 +7,26 @@ import (
|
||||
"path/filepath"
|
||||
|
||||
"custom-start-page/internal/middleware"
|
||||
"custom-start-page/internal/models"
|
||||
"custom-start-page/internal/services"
|
||||
)
|
||||
|
||||
// DashboardHandler handles dashboard-related HTTP requests
|
||||
type DashboardHandler struct {
|
||||
templates *template.Template
|
||||
pageService services.PageServiceInterface
|
||||
templates *template.Template
|
||||
}
|
||||
|
||||
// NewDashboardHandler creates a new dashboard handler
|
||||
func NewDashboardHandler() *DashboardHandler {
|
||||
func NewDashboardHandler(pageService services.PageServiceInterface) *DashboardHandler {
|
||||
// Parse templates
|
||||
templates := template.Must(template.ParseGlob(filepath.Join("templates", "*.html")))
|
||||
template.Must(templates.ParseGlob(filepath.Join("templates", "layouts", "*.html")))
|
||||
template.Must(templates.ParseGlob(filepath.Join("templates", "partials", "*.html")))
|
||||
|
||||
return &DashboardHandler{
|
||||
templates: templates,
|
||||
pageService: pageService,
|
||||
templates: templates,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,14 +40,23 @@ func (h *DashboardHandler) HandleDashboard(w http.ResponseWriter, r *http.Reques
|
||||
return
|
||||
}
|
||||
|
||||
// TODO: Fetch user's pages from database
|
||||
// For now, we'll use mock data
|
||||
pages := []map[string]interface{}{
|
||||
{
|
||||
"ID": "default-page",
|
||||
"Name": "Home",
|
||||
"Active": true,
|
||||
},
|
||||
// Fetch user's pages from database
|
||||
pages, err := h.pageService.GetPages(r.Context(), userID)
|
||||
if err != nil {
|
||||
log.Printf("Failed to get pages: %v", err)
|
||||
http.Error(w, "Failed to load pages", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// If user has no pages, create default "Home" page
|
||||
if len(pages) == 0 {
|
||||
defaultPage, err := h.pageService.CreateDefaultPage(r.Context(), userID)
|
||||
if err != nil {
|
||||
log.Printf("Failed to create default page: %v", err)
|
||||
http.Error(w, "Failed to create default page", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
pages = []*models.Page{defaultPage}
|
||||
}
|
||||
|
||||
// Render dashboard template
|
||||
|
||||
Reference in New Issue
Block a user