feat: initial commit
This commit is contained in:
@ -28,7 +28,9 @@ func (h *Handler) getQuizPage(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (h *Handler) fillQuizPageVModel(r *http.Request) (*component.QuizPageVModel, error) {
|
||||
vmodel := &component.QuizPageVModel{}
|
||||
vmodel := &component.QuizPageVModel{
|
||||
PlayDelay: h.playDelay,
|
||||
}
|
||||
|
||||
err := common.FillViewModel(
|
||||
r.Context(), vmodel, r,
|
||||
@ -148,6 +150,8 @@ func (h *Handler) handleSelectEntryForm(w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
|
||||
func (h *Handler) handleAnswerForm(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
player, err := h.getRequestPlayer(r)
|
||||
if err != nil {
|
||||
h.handleError(w, r, errors.WithStack(err))
|
||||
@ -193,7 +197,33 @@ func (h *Handler) handleAnswerForm(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
ctx := r.Context()
|
||||
now := time.Now().UTC()
|
||||
|
||||
if player.SelectedAt.UTC().Add(h.playDelay + time.Duration(float64(h.playDelay)*0.10)).Before(now) {
|
||||
err = h.store.Tx(ctx, func(db *gorm.DB) error {
|
||||
result := db.Model(&player).Updates(map[string]any{
|
||||
"selected_answer": selectedAnswerIndex,
|
||||
"played_at": time.Now().UTC(),
|
||||
}).Where("played_at = ?", player.PlayedAt)
|
||||
|
||||
if result.Error != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
if result.RowsAffected != 1 {
|
||||
return errors.New("unexpected number of updated player")
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
h.handleError(w, r, errors.WithStack(err))
|
||||
return
|
||||
}
|
||||
|
||||
h.handleError(w, r, common.NewError("delay expired", "Vous avez malheureusement trop tardé à répondre ! Réessayez au prochain tour !", http.StatusBadRequest))
|
||||
return
|
||||
}
|
||||
|
||||
turn, err := h.store.GetQuizTurn(ctx, h.playInterval, h.playPeriod)
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user