Enha: create tx; cardword test

This commit is contained in:
Grail Finder
2025-07-04 07:00:16 +03:00
parent c9196d3202
commit 8b81e2e2c4
3 changed files with 553 additions and 1 deletions

View File

@ -159,6 +159,21 @@ func HandleStartGame(w http.ResponseWriter, r *http.Request) {
abortWithError(w, err.Error())
return
}
// Initialize transaction
ctx, tx, err := repo.InitTx(r.Context())
if err != nil {
log.Error("failed to init transaction", "error", err)
abortWithError(w, err.Error())
return
}
defer func() {
if r := recover(); r != nil {
tx.Rollback()
panic(r)
}
}()
fi.Room.IsRunning = true
fi.Room.IsOver = false
fi.Room.TeamTurn = "blue"
@ -174,10 +189,42 @@ func HandleStartGame(w http.ResponseWriter, r *http.Request) {
Action: models.ActionTypeGameStarted,
}
fi.Room.ActionHistory = append(fi.Room.ActionHistory, action)
if err := saveFullInfo(r.Context(), fi); err != nil {
// Use the new context with transaction
if err := saveFullInfo(ctx, fi); err != nil {
tx.Rollback()
abortWithError(w, err.Error())
return
}
// Save action history
action.RoomID = fi.Room.ID
action.CreatedAtUnix = time.Now().Unix()
if err := repo.CreateAction(ctx, fi.Room.ID, &action); err != nil {
tx.Rollback()
log.Error("failed to save action", "error", err)
abortWithError(w, err.Error())
return
}
// Save word cards
for _, card := range fi.Room.Cards {
card.RoomID = fi.Room.ID // Ensure RoomID is set for each card
if err := repo.WordCardsCreate(ctx, &card); err != nil {
tx.Rollback()
log.Error("failed to save word card", "error", err)
abortWithError(w, err.Error())
return
}
}
// Commit the transaction
if err := tx.Commit(); err != nil {
log.Error("failed to commit transaction", "error", err)
abortWithError(w, err.Error())
return
}
// reveal all cards
if fi.State.Role == "mime" {
fi.Room.MimeView()