2025-06-10 21:09:58 +02:00
|
|
|
package quiz
|
|
|
|
|
|
|
|
import (
|
2025-06-13 16:55:46 +02:00
|
|
|
"context"
|
2025-06-10 21:09:58 +02:00
|
|
|
"net/http"
|
|
|
|
|
2025-06-13 16:55:46 +02:00
|
|
|
"forge.cadoles.com/wpetit/kouiz/internal/http/handler/webui/auth"
|
2025-06-10 21:09:58 +02:00
|
|
|
"forge.cadoles.com/wpetit/kouiz/internal/http/handler/webui/common"
|
|
|
|
"forge.cadoles.com/wpetit/kouiz/internal/http/handler/webui/quiz/component"
|
2025-06-13 16:55:46 +02:00
|
|
|
"forge.cadoles.com/wpetit/kouiz/internal/store"
|
2025-06-10 21:09:58 +02:00
|
|
|
"github.com/a-h/templ"
|
2025-06-13 16:55:46 +02:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2025-06-10 21:09:58 +02:00
|
|
|
"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,
|
2025-06-13 16:55:46 +02:00
|
|
|
h.fillQuizPagePlayer,
|
|
|
|
h.fillQuizPageTurn,
|
2025-06-10 21:09:58 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return vmodel, nil
|
|
|
|
}
|
|
|
|
|
2025-06-13 16:55:46 +02:00
|
|
|
func (h *Handler) fillQuizPagePlayer(ctx context.Context, vmodel *component.QuizPageVModel, r *http.Request) error {
|
|
|
|
user := auth.ContextUser(ctx)
|
|
|
|
|
|
|
|
player := &store.Player{
|
|
|
|
Name: user.Name,
|
|
|
|
UserID: user.ID,
|
|
|
|
UserProvider: user.Provider,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.store.UpsertPlayer(ctx, player); err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
vmodel.Player = player
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) fillQuizPageTurn(ctx context.Context, vmodel *component.QuizPageVModel, r *http.Request) error {
|
|
|
|
turn, err := h.store.GetQuizTurn(ctx, h.playInterval)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
spew.Dump(turn)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2025-06-10 21:09:58 +02:00
|
|
|
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))
|
|
|
|
}
|