/
timetable.go
87 lines (80 loc) · 2.29 KB
/
timetable.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
79
80
81
82
83
84
85
86
87
package schema
import (
"entgo.io/contrib/entgql"
"entgo.io/ent"
"entgo.io/ent/dialect"
"entgo.io/ent/schema"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
"entgo.io/ent/schema/index"
"github.com/google/uuid"
"github.com/hkonitzer/ohmab/ent/schema/constants"
"github.com/hkonitzer/ohmab/ent/schema/hooks"
)
// Timetable holds the schema definition for a Timetable entity.
type Timetable struct {
ent.Schema
hooks.AuditLog
}
// Fields of a Timetable.
func (Timetable) Fields() []ent.Field {
return []ent.Field{
field.UUID(constants.IDFieldName, uuid.UUID{}).
Immutable().Default(uuid.New),
field.Time("datetime_from").
Annotations(entgql.OrderField("datetimeFrom")),
field.Int("duration").
Positive().Range(1, 24).Optional().
SchemaType(map[string]string{
dialect.MySQL: "TINYINT", // Override MySQL.
dialect.Postgres: "SMALLINT", // Override Postgres.
dialect.SQLite: "INTEGER", // Override SQLite.
}).Comment("The duration of the timetable entry in hours, overwrites datetime_to"),
field.Time("datetime_to").
Optional().Comment("The end of the timetable entry, only used if duration is not set"),
field.Bool("time_whole_day").Default(false),
field.Text("comment").Optional(),
field.Text("availability_by_phone").Optional(),
field.Text("availability_by_email").Optional(),
field.Text("availability_by_sms").Optional(),
field.Text("availability_by_whatsapp").Optional(),
}
}
// Mixin Time mixin for a Timetable
func (Timetable) Mixin() []ent.Mixin {
return []ent.Mixin{
TimeMixin{},
TimetableTypeMixin{},
}
}
// Edges of a Timetable.
func (Timetable) Edges() []ent.Edge {
return []ent.Edge{
edge.From("address", Address.Type).
Required().
Unique().
Ref("timetables"),
edge.To("operators_on_duty", Operator.Type),
}
}
func (Timetable) Annotations() []schema.Annotation {
return []schema.Annotation{
entgql.QueryField(),
entgql.Mutations(entgql.MutationCreate(), entgql.MutationUpdate()),
entgql.MultiOrder(),
entgql.RelayConnection(),
}
}
func (Timetable) Indexes() []ent.Index {
return []ent.Index{
index.Fields("datetime_from"),
}
}
func (t Timetable) Hooks() []ent.Hook {
return []ent.Hook{
hooks.RemoveZeroTimeValues(),
hooks.EnsureDurationIsSet(),
hooks.EnsureOnlyOneTimetableEntry(),
t.AuditLogForTimetable(),
}
}