-
Notifications
You must be signed in to change notification settings - Fork 25
/
template.go
65 lines (56 loc) · 1.9 KB
/
template.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
package models
import (
"fmt"
"time"
"github.com/content-services/content-sources-backend/pkg/config"
"gorm.io/gorm"
)
type Template struct {
Base
Name string `gorm:"not null;default:null"`
OrgID string `gorm:"default:null"`
Description string `gorm:"default:null"`
Date time.Time `gorm:"default:null"`
Version string `gorm:"default:null"`
Arch string `gorm:"default:null"`
DeletedAt gorm.DeletedAt `json:"deleted_at"`
RepositoryConfigurations []RepositoryConfiguration `gorm:"many2many:templates_repository_configurations"`
}
// BeforeCreate perform validations and sets UUID of Template
func (t *Template) BeforeCreate(tx *gorm.DB) error {
if err := t.Base.BeforeCreate(tx); err != nil {
return err
}
if err := t.validate(); err != nil {
return err
}
return nil
}
func (t *Template) validate() error {
var err error
if t.Name == "" {
err = Error{Message: "Name cannot be blank.", Validation: true}
return err
}
if t.OrgID == "" {
err = Error{Message: "Org ID cannot be blank.", Validation: true}
return err
}
if t.Arch != "" && !config.ValidArchLabel(t.Arch) {
return Error{Message: fmt.Sprintf("Specified architecture %s is invalid.", t.Arch),
Validation: true}
}
valid, invalidVer := config.ValidDistributionVersionLabels([]string{t.Version})
if len(t.Version) > 0 && !valid {
return Error{Message: fmt.Sprintf("Specified distribution version %s is invalid.", invalidVer),
Validation: true}
}
return nil
}
func (t *Template) MapForUpdate() map[string]interface{} {
forUpdate := make(map[string]interface{})
// Name, version, arch cannot be updated
forUpdate["description"] = t.Description
forUpdate["date"] = t.Date
return forUpdate
}