71 lines
1.8 KiB
Go
71 lines
1.8 KiB
Go
package api
|
|
|
|
import (
|
|
"database/sql"
|
|
"duhweb/internal/store"
|
|
"html/template"
|
|
"net/http"
|
|
)
|
|
|
|
type ApiHandler struct {
|
|
Templates *template.Template
|
|
ProjectStore *store.SQLiteProjectStore
|
|
ExperienceStore *store.SQLiteExperienceStore
|
|
}
|
|
|
|
func (h *ApiHandler) Render(w http.ResponseWriter, tmpl string, data interface{}) {
|
|
if err := h.Templates.ExecuteTemplate(w, tmpl, data); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|
|
|
|
func NewApiHandler(db *sql.DB) *ApiHandler {
|
|
tmpl := template.Must(template.New("").ParseGlob("views/*.html"))
|
|
template.Must(tmpl.ParseGlob("views/partials/*.html"))
|
|
|
|
return &ApiHandler{
|
|
Templates: tmpl,
|
|
ProjectStore: store.NewSQLiteProjectStore(db),
|
|
ExperienceStore: store.NewSQLiteExperienceStore(db),
|
|
}
|
|
}
|
|
|
|
func (h *ApiHandler) InitPage(w http.ResponseWriter, r *http.Request) {
|
|
var hxRequest = r.Header.Get("HX-Request")
|
|
if hxRequest != "true" {
|
|
h.Render(w, "index", nil)
|
|
return
|
|
}
|
|
h.Render(w, "about-partial", nil)
|
|
}
|
|
|
|
func (h *ApiHandler) ProjectsPage(w http.ResponseWriter, r *http.Request) {
|
|
var hxRequest = r.Header.Get("HX-Request")
|
|
projects, err := h.ProjectStore.GetAllProjects()
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
if hxRequest != "true" {
|
|
h.Render(w, "projects", projects)
|
|
return
|
|
}
|
|
h.Render(w, "projects-partial", projects)
|
|
}
|
|
|
|
func (h *ApiHandler) ExperiencePage(w http.ResponseWriter, r *http.Request) {
|
|
var hxRequest = r.Header.Get("HX-Request")
|
|
// experiences, err := h.ExperienceStore.GetAllExperiences()
|
|
experiences, err := store.GetAllExperiencesJSON()
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
if hxRequest != "true" {
|
|
h.Render(w, "experience", experiences)
|
|
return
|
|
}
|
|
h.Render(w, "experience-partial", experiences)
|
|
}
|