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:
2026-02-19 00:08:05 -05:00
parent 9f07b0c6f9
commit 299ac03939
16 changed files with 1572 additions and 31 deletions

View File

@@ -9,6 +9,7 @@ import (
"custom-start-page/internal/auth"
"custom-start-page/internal/handlers"
"custom-start-page/internal/middleware"
"custom-start-page/internal/services"
"custom-start-page/internal/storage"
"custom-start-page/pkg/config"
)
@@ -37,9 +38,22 @@ func main() {
log.Fatalf("Failed to create Users table: %v", err)
}
// Create Pages table if it doesn't exist
if err := dbClient.CreatePagesTable(ctx); err != nil {
log.Fatalf("Failed to create Pages table: %v", err)
}
// Create Widgets table if it doesn't exist
if err := dbClient.CreateWidgetsTable(ctx); err != nil {
log.Fatalf("Failed to create Widgets table: %v", err)
}
// Initialize repositories
userRepo := storage.NewUserRepository(dbClient, "Users")
// Initialize services
pageService := services.NewPageService(dbClient)
// Initialize auth services
stateStore := auth.NewMemoryStateStore()
oauthService := auth.NewOAuthService(
@@ -53,7 +67,8 @@ func main() {
// Initialize handlers
authHandler := handlers.NewAuthHandler(oauthService, userService, sessionStore)
dashboardHandler := handlers.NewDashboardHandler()
dashboardHandler := handlers.NewDashboardHandler(pageService)
pageHandler := handlers.NewPageHandler(pageService)
// Setup routes
mux := http.NewServeMux()
@@ -90,6 +105,13 @@ func main() {
// Protected dashboard route
mux.Handle("GET /dashboard", requireAuth(http.HandlerFunc(dashboardHandler.HandleDashboard)))
// Protected page routes
mux.Handle("GET /pages/{id}", requireAuth(http.HandlerFunc(pageHandler.HandleGetPage)))
mux.Handle("POST /pages", requireAuth(http.HandlerFunc(pageHandler.HandleCreatePage)))
mux.Handle("PUT /pages/{id}", requireAuth(http.HandlerFunc(pageHandler.HandleUpdatePage)))
mux.Handle("DELETE /pages/{id}", requireAuth(http.HandlerFunc(pageHandler.HandleDeletePage)))
mux.Handle("POST /pages/reorder", requireAuth(http.HandlerFunc(pageHandler.HandleReorderPages)))
// Start server
addr := fmt.Sprintf("%s:%s", cfg.Server.Host, cfg.Server.Port)
log.Printf("Starting server on %s", addr)