-
Notifications
You must be signed in to change notification settings - Fork 0
/
item.go
78 lines (67 loc) · 1.67 KB
/
item.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package schema
import (
"entgo.io/ent"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
"entgo.io/ent/schema/index"
"entgo.io/ent/schema/mixin"
"github.com/google/uuid"
)
// Item holds the schema definition for the Item entity.
type Item struct {
ent.Schema
}
// Fields of the Item.
func (Item) Fields() []ent.Field {
return []ent.Field{
field.UUID("id", uuid.UUID{}).Default(uuid.New),
field.String("code").NotEmpty().Unique().Immutable(),
field.Int("steps").Default(2),
}
}
// Edges of the Item.
func (Item) Edges() []ent.Edge {
return []ent.Edge{
// has
edge.To("responses", Response.Type),
edge.To("translations", ItemTranslation.Type),
// belongs to
edge.From("scale", Scale.Type).Ref("items").Through("scale_item", ScaleItem.Type),
edge.From("question", Question.Type).Ref("items"),
}
}
func (Item) Mixin() []ent.Mixin {
return []ent.Mixin{
mixin.Time{},
}
}
// TRANSLATIONS //
// ItemTranslations holds the schema definition for the ItemTranslations entity.
type ItemTranslation struct {
ent.Schema
}
// Fields of the ItemTranslations.
func (ItemTranslation) Fields() []ent.Field {
return []ent.Field{
field.UUID("id", uuid.UUID{}).Default(uuid.New),
field.String("content").NotEmpty(),
}
}
// Edges of the ItemTranslations.
func (ItemTranslation) Edges() []ent.Edge {
return []ent.Edge{
// belongs to
edge.From("item", Item.Type).Ref("translations").Unique(),
}
}
func (ItemTranslation) Indexes() []ent.Index {
return []ent.Index{
// one translation per locale per item
index.Edges("item").Fields("locale").Unique(),
}
}
func (ItemTranslation) Mixin() []ent.Mixin {
return []ent.Mixin{
LocaleMixin{}, // holds locale names
}
}