Implement session creation

This commit is contained in:
Vojtěch Káně
2021-03-29 20:37:52 +02:00
parent 782f96ebbd
commit a5bc7c6984
10 changed files with 180 additions and 4 deletions

View File

@@ -0,0 +1,10 @@
package schema
import (
"entgo.io/ent"
)
// Provides a DBMS independent way of obtaining unique values
type CodesSequence struct {
ent.Schema
}

View File

@@ -17,6 +17,7 @@ func (Game) Fields() []ent.Field {
field.Text("name").MaxLen(64),
field.Time("created").Immutable(),
field.Text("author").MaxLen(64),
field.Text("code").MinLen(1).Unique(),
}
}

View File

@@ -16,7 +16,7 @@ func (Session) Fields() []ent.Field {
field.UUID("id", uuid.Nil).Immutable(),
field.Time("created").Immutable(),
field.Time("started").Nillable().Optional(),
field.String("code").MinLen(6).MaxLen(6).Immutable().Unique(),
field.String("code").MinLen(1).Immutable().Unique(),
}
}

View File

@@ -8,6 +8,7 @@ import (
"errors"
"github.com/google/uuid"
"time"
"vkane.cz/tinyquiz/pkg/codeGenerator"
"vkane.cz/tinyquiz/pkg/model/ent"
"vkane.cz/tinyquiz/pkg/model/ent/answer"
"vkane.cz/tinyquiz/pkg/model/ent/askedquestion"
@@ -84,6 +85,41 @@ func (m *Model) RegisterPlayer(playerName string, sessionCode string, now time.T
}
}
func (m *Model) CreateSession(organiserName string, gameCode string, now time.Time, c context.Context) (*ent.Session, *ent.Player, error) {
tx, err := m.c.BeginTx(c, &sql.TxOptions{
Isolation: sql.LevelReadCommitted,
})
if err != nil {
return nil, nil, err
}
defer tx.Rollback()
if gameId, err := tx.Game.Query().Where(game.Code(gameCode)).OnlyID(c); err == nil {
if incremental, err := m.getCodeIncremental(c); err == nil {
if code, err := codeGenerator.GenerateRandomCode(incremental, codeRandomPartLength); err == nil {
if s, err := tx.Session.Create().SetID(uuid.New()).SetCreated(now).SetCode(string(code)).SetGameID(gameId).Save(c); err == nil {
if p, err := tx.Player.Create().SetID(uuid.New()).SetJoined(now).SetName(organiserName).SetSession(s).SetOrganiser(true).Save(c); err == nil {
err := tx.Commit()
return s, p, err
} else {
return nil, nil, err
}
} else {
return nil, nil, err
}
} else {
return nil, nil, err
}
} else {
return nil, nil, err
}
} else if ent.IsNotFound(err) {
return nil, nil, NoSuchEntity
} else {
return nil, nil, err
}
}
func (m *Model) GetPlayerWithSessionAndGame(uid uuid.UUID, c context.Context) (*ent.Player, error) {
tx, err := m.c.BeginTx(c, &sql.TxOptions{
Isolation: sql.LevelRepeatableRead,
@@ -268,3 +304,13 @@ func (m *Model) SaveAnswer(playerId uuid.UUID, choiceId uuid.UUID, now time.Time
return nil, err
}
}
const codeRandomPartLength uint8 = 3
func (m *Model) getCodeIncremental(c context.Context) (uint64, error) {
if c, err := m.c.CodesSequence.Create().Save(c); err == nil {
return uint64(c.ID), nil
} else {
return 0, nil
}
}

View File

@@ -43,7 +43,7 @@ func newTestModelWithData(t *testing.T) *Model {
defer tx.Rollback()
var gamesC = []*ent.GameCreate{
tx.Game.Create().SetID(uuid.MustParse("cab48de7-bba3-4873-9335-eec4aaaae1e9")).SetName("5th grade knowledge test").SetCreated(time.Unix(1613387448, 0)).SetAuthor("Adam Smith PhD."),
tx.Game.Create().SetID(uuid.MustParse("cab48de7-bba3-4873-9335-eec4aaaae1e9")).SetName("5th grade knowledge test").SetCode("abcdef").SetCreated(time.Unix(1613387448, 0)).SetAuthor("Adam Smith PhD."),
}
games := tx.Game.CreateBulk(gamesC...).SaveX(c)