Initial commit

This commit is contained in:
Vojtěch Káně
2020-12-03 23:07:44 +01:00
commit 28e22e3422
24 changed files with 1526 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
package schema
import (
"github.com/facebook/ent"
"github.com/facebook/ent/schema/edge"
"github.com/facebook/ent/schema/field"
"github.com/google/uuid"
)
type Answer struct {
ent.Schema
}
func (Answer) Fields() []ent.Field {
return []ent.Field{
field.UUID("id", uuid.New()).Immutable().Unique(),
field.Time("answered").Immutable(),
}
}
func (Answer) Indexes() []ent.Index {
return []ent.Index{
}
}
func (Answer) Edges() []ent.Edge {
return []ent.Edge{
edge.From("choice", Choice.Type).
Ref("answers").
Unique().
Required(),
edge.From("answerer", Player.Type).
Ref("answers").
Unique().
Required(),
}
}
func (Answer) Config() ent.Config {
return ent.Config{
Table: "answers",
}
}

View File

@@ -0,0 +1,41 @@
package schema
import (
"github.com/facebook/ent"
"github.com/facebook/ent/schema/edge"
"github.com/facebook/ent/schema/field"
"github.com/google/uuid"
)
type Choice struct {
ent.Schema
}
func (Choice) Fields() []ent.Field {
return []ent.Field{
field.UUID("id", uuid.New()).Immutable(),
field.Text("title").MinLen(1).MaxLen(256),
field.Bool("correct"),
}
}
func (Choice) Indexes() []ent.Index {
return []ent.Index{
}
}
func (Choice) Edges() []ent.Edge {
return []ent.Edge{
edge.From("question", Question.Type).
Ref("choices").
Unique().
Required(),
edge.To("answers", Answer.Type),
}
}
func (Choice) Config() ent.Config {
return ent.Config{
Table: "options",
}
}

View File

@@ -0,0 +1,34 @@
package schema
import (
"github.com/facebook/ent"
"github.com/facebook/ent/schema/edge"
"github.com/facebook/ent/schema/field"
"github.com/google/uuid"
)
type Game struct {
ent.Schema
}
func (Game) Fields() []ent.Field {
return []ent.Field{
field.UUID("id", uuid.New()).Unique().Immutable(),
field.Text("name").MaxLen(64),
field.Time("created").Immutable(),
field.Text("author").MaxLen(64),
}
}
func (Game) Edges() []ent.Edge {
return []ent.Edge{
edge.To("sessions", Session.Type),
edge.To("questions", Question.Type),
}
}
func (Game) Config() ent.Config {
return ent.Config{
Table: "games",
}
}

View File

@@ -0,0 +1,45 @@
package schema
import (
"github.com/facebook/ent"
"github.com/facebook/ent/schema/edge"
"github.com/facebook/ent/schema/field"
"github.com/facebook/ent/schema/index"
"github.com/google/uuid"
"regexp"
)
type Player struct {
ent.Schema
}
func (Player) Fields() []ent.Field {
return []ent.Field{
field.UUID("id", uuid.New()).Immutable(),
field.Text("name").MaxLen(64).MinLen(1).Match(regexp.MustCompile("(?:[a-z]|[A-Z]|_|-|.|,|[0-9])+")),
field.Time("joined").Immutable(),
field.Bool("organiser").Default(false),
}
}
func (Player) Indexes() []ent.Index {
return []ent.Index{
index.Fields("name").Edges("session").Unique(),
}
}
func (Player) Edges() []ent.Edge {
return []ent.Edge{
edge.From("session", Session.Type).
Ref("players").
Unique().
Required(),
edge.To("answers", Answer.Type),
}
}
func (Player) Config() ent.Config {
return ent.Config{
Table: "players",
}
}

View File

@@ -0,0 +1,44 @@
package schema
import (
"github.com/facebook/ent"
"github.com/facebook/ent/schema/edge"
"github.com/facebook/ent/schema/field"
"github.com/facebook/ent/schema/index"
"github.com/google/uuid"
)
type Question struct {
ent.Schema
}
func (Question) Fields() []ent.Field {
return []ent.Field{
field.UUID("id", uuid.New()).Immutable(),
field.Text("title").MaxLen(256).MinLen(1),
field.Int("order"),
}
}
func (Question) Indexes() []ent.Index {
return []ent.Index{
index.Fields("order").Edges("game").Unique(),
}
}
func (Question) Edges() []ent.Edge {
return []ent.Edge{
edge.From("game", Game.Type).
Ref("questions").
Unique().
Required(),
edge.To("choices", Choice.Type),
edge.To("current_sessions", Session.Type),
}
}
func (Question) Config() ent.Config {
return ent.Config{
Table: "questions",
}
}

View File

@@ -0,0 +1,46 @@
package schema
import (
"github.com/facebook/ent"
"github.com/facebook/ent/schema/edge"
"github.com/facebook/ent/schema/field"
"github.com/google/uuid"
)
type Session struct {
ent.Schema
}
func (Session) Fields() []ent.Field {
return []ent.Field{
field.UUID("id", uuid.New()).Immutable(),
field.Time("created").Immutable(),
field.Time("started").Nillable().Optional(), // TODO remove?
field.Time("current_question_until").Nillable().Optional(),
field.String("code").MinLen(6).MaxLen(6).Immutable().Unique(),
}
}
func (Session) Indexes() []ent.Index {
return []ent.Index{
}
}
func (Session) Edges() []ent.Edge {
return []ent.Edge{
edge.From("game", Game.Type).
Ref("sessions").
Unique().
Required(),
edge.To("players", Player.Type),
edge.From("current_question", Question.Type).
Ref("current_sessions").
Unique(),
}
}
func (Session) Config() ent.Config {
return ent.Config{
Table: "sessions",
}
}