feat: initial commit
This commit is contained in:
@ -35,6 +35,14 @@ func (h *Handler) handleProviderCallback(w http.ResponseWriter, r *http.Request)
|
||||
Provider: gothUser.Provider,
|
||||
AccessToken: gothUser.AccessToken,
|
||||
IDToken: gothUser.IDToken,
|
||||
Name: gothUser.Name,
|
||||
}
|
||||
|
||||
rawPreferredUsername, exists := gothUser.RawData["preferred_username"]
|
||||
if exists {
|
||||
if preferredUsername, ok := rawPreferredUsername.(string); ok {
|
||||
user.Name = preferredUsername
|
||||
}
|
||||
}
|
||||
|
||||
if err := h.storeSessionUser(w, r, user); err != nil {
|
||||
|
@ -5,4 +5,5 @@ type User struct {
|
||||
Provider string
|
||||
AccessToken string
|
||||
IDToken string
|
||||
Name string
|
||||
}
|
||||
|
@ -5,12 +5,14 @@ import (
|
||||
"context"
|
||||
"forge.cadoles.com/wpetit/kouiz/internal/http/form"
|
||||
common "forge.cadoles.com/wpetit/kouiz/internal/http/handler/webui/common/component"
|
||||
"forge.cadoles.com/wpetit/kouiz/internal/store"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/yuin/goldmark"
|
||||
"log/slog"
|
||||
)
|
||||
|
||||
type QuizPageVModel struct {
|
||||
Player *store.Player
|
||||
}
|
||||
|
||||
func NewQuizForm() *form.Form {
|
||||
|
@ -13,12 +13,14 @@ import (
|
||||
"context"
|
||||
"forge.cadoles.com/wpetit/kouiz/internal/http/form"
|
||||
common "forge.cadoles.com/wpetit/kouiz/internal/http/handler/webui/common/component"
|
||||
"forge.cadoles.com/wpetit/kouiz/internal/store"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/yuin/goldmark"
|
||||
"log/slog"
|
||||
)
|
||||
|
||||
type QuizPageVModel struct {
|
||||
Player *store.Player
|
||||
}
|
||||
|
||||
func NewQuizForm() *form.Form {
|
||||
|
@ -2,13 +2,15 @@ package quiz
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"forge.cadoles.com/wpetit/kouiz/internal/store"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
mux *http.ServeMux
|
||||
store *store.Store
|
||||
mux *http.ServeMux
|
||||
store *store.Store
|
||||
playInterval time.Duration
|
||||
}
|
||||
|
||||
// ServeHTTP implements http.Handler.
|
||||
@ -16,10 +18,11 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
h.mux.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
func NewHandler(store *store.Store) *Handler {
|
||||
func NewHandler(store *store.Store, playInterval time.Duration) *Handler {
|
||||
h := &Handler{
|
||||
mux: http.NewServeMux(),
|
||||
store: store,
|
||||
mux: http.NewServeMux(),
|
||||
store: store,
|
||||
playInterval: playInterval,
|
||||
}
|
||||
|
||||
h.mux.HandleFunc("GET /", h.getQuizPage)
|
||||
|
@ -1,11 +1,15 @@
|
||||
package quiz
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"forge.cadoles.com/wpetit/kouiz/internal/http/handler/webui/auth"
|
||||
"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/davecgh/go-spew/spew"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
@ -25,6 +29,8 @@ func (h *Handler) fillQuizPageVModel(r *http.Request) (*component.QuizPageVModel
|
||||
|
||||
err := common.FillViewModel(
|
||||
r.Context(), vmodel, r,
|
||||
h.fillQuizPagePlayer,
|
||||
h.fillQuizPageTurn,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
@ -33,6 +39,35 @@ func (h *Handler) fillQuizPageVModel(r *http.Request) (*component.QuizPageVModel
|
||||
return vmodel, nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func (h *Handler) handleQuizForm(w http.ResponseWriter, r *http.Request) {
|
||||
quizForm := component.NewQuizForm()
|
||||
|
||||
|
Reference in New Issue
Block a user