60 lines
1.4 KiB
Go
60 lines
1.4 KiB
Go
package quiz
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"forge.cadoles.com/wpetit/kouiz/internal/http/handler/webui/common"
|
|
"forge.cadoles.com/wpetit/kouiz/internal/http/handler/webui/quiz/component"
|
|
"forge.cadoles.com/wpetit/kouiz/internal/store"
|
|
"github.com/a-h/templ"
|
|
"github.com/pkg/errors"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func (h *Handler) getHistoryPage(w http.ResponseWriter, r *http.Request) {
|
|
vmodel, err := h.fillHistoryPageVModel(r)
|
|
if err != nil {
|
|
h.handleError(w, r, errors.WithStack(err))
|
|
return
|
|
}
|
|
|
|
page := component.HistoryPage(*vmodel)
|
|
templ.Handler(page).ServeHTTP(w, r)
|
|
}
|
|
|
|
func (h *Handler) fillHistoryPageVModel(r *http.Request) (*component.HistoryPageVModel, error) {
|
|
vmodel := &component.HistoryPageVModel{}
|
|
|
|
err := common.FillViewModel(
|
|
r.Context(), vmodel, r,
|
|
h.fillHistoryPageHistory,
|
|
)
|
|
if err != nil {
|
|
return nil, errors.WithStack(err)
|
|
}
|
|
|
|
return vmodel, nil
|
|
}
|
|
|
|
func (h *Handler) fillHistoryPageHistory(ctx context.Context, vmodel *component.HistoryPageVModel, r *http.Request) error {
|
|
player, err := h.getRequestPlayer(r)
|
|
if err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
err = h.store.Do(ctx, func(db *gorm.DB) error {
|
|
err := db.Model(&store.QuizTurn{}).
|
|
Preload("Entries").
|
|
Preload("Entries.Category").
|
|
Order("started_at DESC").
|
|
Find(&vmodel.History, "ended_at < ?", player.PlayedAt.UTC()).Error
|
|
if err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
return nil
|
|
})
|
|
|
|
return nil
|
|
}
|