/
models.go
126 lines (105 loc) · 3.97 KB
/
models.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package models
import (
"fmt"
"time"
"gorm.io/gorm"
)
type Model interface {
TableName() string
UUID() string
}
type Bundle struct {
ID string `gorm:"primaryKey"`
Name string
}
type Log struct {
ID string `json:"id" gorm:"primaryKey"`
Message string `json:"message" gorm:"not null"`
CreatedAt time.Time `json:"createdAt" gorm:"index"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
// Entity this Log entry is about (fe. Publisher, Software, etc.)
EntityID *string `json:"-"`
EntityType *string `json:"-"`
Entity string `json:"entity,omitempty" gorm:"->;type:text GENERATED ALWAYS AS (CASE WHEN entity_id IS NULL THEN NULL ELSE ('/' || entity_type || '/' || entity_id) END) STORED;default:(-);"` //nolint:lll
}
type Publisher struct {
ID string `json:"id" gorm:"primaryKey"`
Email *string `json:"email,omitempty"`
Description string `json:"description" gorm:"uniqueIndex;not null"`
CodeHosting []CodeHosting `json:"codeHosting" gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;unique"`
Active *bool `json:"active" gorm:"default:true;not null"`
AlternativeID *string `json:"alternativeId,omitempty" gorm:"uniqueIndex"`
CreatedAt time.Time `json:"createdAt" gorm:"index"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
}
func (Publisher) TableName() string {
return "publishers"
}
func (p Publisher) UUID() string {
return p.ID
}
func (CodeHosting) TableName() string {
return "publishers_code_hosting"
}
type CodeHosting struct {
ID string `json:"-" gorm:"primaryKey"`
URL string `json:"url" gorm:"not null;uniqueIndex"`
Group *bool `json:"group" gorm:"default:true;not null"`
PublisherID string `json:"-"`
CreatedAt time.Time `json:"createdAt" gorm:"index"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
}
type Software struct {
ID string `json:"id" gorm:"primarykey"`
// This needs to be explicitly declared, otherwise GORM won't create
// the foreign key and will be confused about the double relationship
// with SoftwareURLs (belongs to and has many).
SoftwareURLID string `json:"-" gorm:"uniqueIndex;not null"`
URL SoftwareURL `json:"url"`
Aliases []SoftwareURL `json:"aliases"`
PubliccodeYml string `json:"publiccodeYml"`
Logs []Log `json:"-" gorm:"polymorphic:Entity;"`
Active *bool `json:"active" gorm:"default:true;not null"`
CreatedAt time.Time `json:"createdAt" gorm:"index"`
UpdatedAt time.Time `json:"updatedAt"`
}
func (Software) TableName() string {
// Don't use GORM's default pluralized form ("softwares")
return "software"
}
func (s Software) UUID() string {
return s.ID
}
type SoftwareURL struct {
ID string `gorm:"primarykey"`
URL string `gorm:"uniqueIndex"`
SoftwareID string `gorm:"not null"`
CreatedAt time.Time `json:"createdAt" gorm:"index"`
UpdatedAt time.Time `json:"updatedAt"`
}
func (su SoftwareURL) MarshalJSON() ([]byte, error) {
return ([]byte)(fmt.Sprintf(`"%s"`, su.URL)), nil
}
type Webhook struct {
ID string `json:"id" gorm:"primaryKey"`
URL string `json:"url" gorm:"index:idx_webhook_url,unique"`
Secret string `json:"-"`
CreatedAt time.Time `json:"createdAt" gorm:"index"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
// Entity this Webhook is for (fe. Publisher, Software, etc.)
EntityID string `json:"-" gorm:"index:idx_webhook_url,unique"`
EntityType string `json:"-" gorm:"index:idx_webhook_url,unique"`
}
type Event struct {
ID string `gorm:"primaryKey"`
Type string
EntityType string
EntityID string
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
}