go-todos-app/handlers/homeHandler.go
Michael Thomson 09a957ca4b
All checks were successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/dryrun Pipeline was successful
ci/woodpecker/push/publish-tag Pipeline was successful
ci/woodpecker/push/publish-latest Pipeline was successful
db, handlers, and interfaces
2024-06-15 21:33:14 -04:00

42 lines
788 B
Go

package handlers
import (
"net/http"
"michaelthomson.dev/mthomson/go-todos-app/services"
"michaelthomson.dev/mthomson/go-todos-app/templates/pages"
)
type HomeHandler struct {
ts *services.TodoService
}
func NewHomeHandler(ts *services.TodoService) HomeHandler {
return HomeHandler{ts: ts}
}
func (h *HomeHandler) Home(w http.ResponseWriter, r *http.Request) {
var err error
todos, err := h.ts.GetTodos()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
err = pages.Home(todos).Render(r.Context(), w)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func (h *HomeHandler) Router() http.Handler {
router := http.NewServeMux()
router.HandleFunc("/", h.Home)
return router
}