64 lines
1.4 KiB
Go
64 lines
1.4 KiB
Go
package quiz
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"forge.cadoles.com/wpetit/kouiz/internal/http/handler/webui/common"
|
|
"forge.cadoles.com/wpetit/kouiz/internal/http/handler/webui/quiz/component"
|
|
"github.com/a-h/templ"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
func (h *Handler) getQuizPage(w http.ResponseWriter, r *http.Request) {
|
|
vmodel, err := h.fillQuizPageVModel(r)
|
|
if err != nil {
|
|
h.handleError(w, r, errors.WithStack(err))
|
|
return
|
|
}
|
|
|
|
issue := component.QuizPage(*vmodel)
|
|
templ.Handler(issue).ServeHTTP(w, r)
|
|
}
|
|
|
|
func (h *Handler) fillQuizPageVModel(r *http.Request) (*component.QuizPageVModel, error) {
|
|
vmodel := &component.QuizPageVModel{}
|
|
|
|
err := common.FillViewModel(
|
|
r.Context(), vmodel, r,
|
|
)
|
|
if err != nil {
|
|
return nil, errors.WithStack(err)
|
|
}
|
|
|
|
return vmodel, nil
|
|
}
|
|
|
|
func (h *Handler) handleQuizForm(w http.ResponseWriter, r *http.Request) {
|
|
quizForm := component.NewQuizForm()
|
|
|
|
if err := quizForm.Handle(r); err != nil {
|
|
h.handleError(w, r, errors.WithStack(err))
|
|
return
|
|
}
|
|
|
|
vmodel, err := h.fillQuizPageVModel(r)
|
|
if err != nil {
|
|
h.handleError(w, r, errors.WithStack(err))
|
|
return
|
|
}
|
|
|
|
if errs := quizForm.Validate(); errs != nil {
|
|
page := component.QuizPage(*vmodel)
|
|
templ.Handler(page).ServeHTTP(w, r)
|
|
|
|
return
|
|
}
|
|
|
|
page := component.QuizPage(*vmodel)
|
|
templ.Handler(page).ServeHTTP(w, r)
|
|
}
|
|
|
|
func (h *Handler) handleError(w http.ResponseWriter, r *http.Request, err error) {
|
|
common.HandleError(w, r, errors.WithStack(err))
|
|
}
|