-
Notifications
You must be signed in to change notification settings - Fork 1
/
team.go
45 lines (40 loc) · 1.52 KB
/
team.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
package team
import (
"errors"
"github.com/ScoreTrak/ScoreTrak/pkg/host"
"github.com/ScoreTrak/ScoreTrak/pkg/user"
"github.com/gofrs/uuid"
"gorm.io/gorm"
)
// Team model represents internal team model of the scoring engine.
type Team struct {
// this id refers to ID of a team in web.
ID uuid.UUID `json:"id,omitempty" gorm:"type:uuid;primary_key;"`
// Name is team name
Name string `json:"name" gorm:"unique;not null"`
// Pause is responsible for pausing the scoring
Pause *bool `json:"pause,omitempty" gorm:"not null;default:false"`
// Hide is responsible for hiding the Team on scoring table
Hide *bool `json:"hide,omitempty" gorm:"not null;default:false"`
// Index is team's index, which is a unique value used for templating (10.X.1.1, where X is an index) and sorting
Index *uint64 `json:"index" gorm:"unique;"`
// Users array stores all of the users that are part of a given Team
Users []*user.User `gorm:"foreignkey:TeamID;association_foreignkey:ID;constraint:OnUpdate:RESTRICT,OnDelete:CASCADE" json:"-"`
// Hosts array stores all Hosts that are part of a given Team
Hosts []*host.Host `gorm:"foreignkey:TeamID;association_foreignkey:ID; constraint:OnUpdate:RESTRICT,OnDelete:RESTRICT" json:"hosts,omitempty"`
}
var ErrNameIsRequired = errors.New("field Name is required")
// BeforeCreate ensures UUID is set.
func (t *Team) BeforeCreate(tx *gorm.DB) (err error) {
if t.Name == "" {
return ErrNameIsRequired
}
if t.ID == uuid.Nil {
u, err := uuid.NewV4()
if err != nil {
return err
}
t.ID = u
}
return nil
}