feat: initial commit

This commit is contained in:
2025-06-15 14:46:32 +02:00
parent 85f0bc1024
commit c50d1858f2
25 changed files with 2002 additions and 55 deletions

View File

@ -3,8 +3,10 @@ package store
import (
"context"
"math/rand/v2"
"slices"
"time"
"forge.cadoles.com/wpetit/kouiz/internal/timex"
"github.com/pkg/errors"
"gorm.io/gorm"
)
@ -49,7 +51,7 @@ func (s *Store) UpsertQuizEntry(ctx context.Context, entry *QuizEntry) error {
}))
}
func (s *Store) GetQuizTurn(ctx context.Context, playInterval time.Duration) (*QuizTurn, error) {
func (s *Store) GetQuizTurn(ctx context.Context, playInterval time.Duration, period timex.PeriodType) (*QuizTurn, error) {
var quizTurn *QuizTurn
err := s.Tx(ctx, func(tx *gorm.DB) error {
now := time.Now().UTC()
@ -63,9 +65,13 @@ func (s *Store) GetQuizTurn(ctx context.Context, playInterval time.Duration) (*Q
return nil
}
interval := timex.NewInterval(playInterval, period)
current := interval.GetCurrent()
start, end := interval.GetIntervalBounds(current, time.Now().UTC())
quizTurn = &QuizTurn{
StartedAt: now.Round(time.Hour),
EndedAt: now.Add(playInterval).Round(time.Hour),
StartedAt: start.UTC(),
EndedAt: end.UTC(),
}
alreadyUsed := make([]uint, 0)
@ -93,6 +99,7 @@ func (s *Store) GetQuizTurn(ctx context.Context, playInterval time.Duration) (*Q
ID: entryIDs[index],
},
})
entryIDs = slices.Delete(entryIDs, index, index+1)
}
if err := tx.Save(quizTurn).Error; err != nil {