create new html views

This commit is contained in:
2025-11-11 20:55:18 +07:00
parent a9da83e6ab
commit 697c5bc428
9 changed files with 195 additions and 162 deletions

View File

@@ -3,6 +3,7 @@ package api
import (
"database/sql"
"duhweb/internal/store"
"fmt"
"html/template"
"net/http"
)
@@ -20,25 +21,35 @@ func (h *ProjectHandler) Render(w http.ResponseWriter, tmpl string, data interfa
}
func NewProjectHandler(db *sql.DB) *ProjectHandler {
tmpl := template.Must(template.New("").ParseGlob("views/*.html"))
template.Must(tmpl.ParseGlob("views/partials/*.html"))
return &ProjectHandler{
Templates: template.Must(template.ParseGlob("views/*.html")),
Templates: tmpl,
ProjectStore: store.NewSQLiteProjectStore(db),
}
}
func (h *ProjectHandler) InitPage(w http.ResponseWriter, r *http.Request) {
h.Render(w, "index", nil)
}
func (h *ProjectHandler) AboutPage(w http.ResponseWriter, r *http.Request) {
h.Render(w, "about", nil)
var hxRequest = r.Header.Get("HX-Request")
if hxRequest != "true" {
h.Render(w, "index", nil)
return
}
h.Render(w, "about-partial", nil)
}
func (h *ProjectHandler) ProjectsPage(w http.ResponseWriter, r *http.Request) {
var hxRequest = r.Header.Get("HX-Request")
fmt.Println(hxRequest)
projects, err := h.ProjectStore.GetAllProjects()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
h.Render(w, "projects", projects)
if hxRequest != "true" {
h.Render(w, "projects", projects)
return
}
h.Render(w, "projects-partial", projects)
}

View File

@@ -10,7 +10,6 @@ func SetupRoutes(app *app.Application) *chi.Mux {
r := chi.NewRouter()
r.Get("/", app.ProjectHandler.InitPage)
r.Get("/about", app.ProjectHandler.AboutPage)
r.Get("/projects", app.ProjectHandler.ProjectsPage)
return r
}
}