-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.go
175 lines (155 loc) · 5.06 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package backend
import (
"fmt"
"strings"
"time"
"github.com/gopsql/bcrypt"
"github.com/gopsql/psql"
)
type (
// Simple admin with name and password.
Admin struct {
Id int
Name string `validate:"gt=0,lte=30,uniqueness"`
Password bcrypt.Password `validate:"required"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time
}
// Admin session contains session ID, IP address and user-agent.
AdminSession struct {
Id int
AdminId int
SessionId string
IpAddress string
UserAgent string
CreatedAt time.Time
UpdatedAt time.Time
}
IsAdmin interface {
GetId() int
GetName() string
GetPassword() bcrypt.Password
GetCreatedAt() time.Time
GetUpdatedAt() time.Time
GetDeletedAt() *time.Time
SetId(int)
SetName(string)
SetPassword(string) error
SetCreatedAt(time.Time)
SetUpdatedAt(time.Time)
SetDeletedAt(*time.Time)
}
Serializable interface {
Serialize(typ string, data ...interface{}) interface{}
}
HasParams interface {
Params(string) []string
}
IsAdminSession interface {
GetId() int
GetAdminId() int
GetSessionId() string
GetIpAddress() string
GetUserAgent() string
GetCreatedAt() time.Time
GetUpdatedAt() time.Time
SetId(int)
SetAdminId(int)
SetSessionId(string)
SetIpAddress(string)
SetUserAgent(string)
SetCreatedAt(time.Time)
SetUpdatedAt(time.Time)
}
)
var (
_ IsAdmin = (*Admin)(nil)
)
func (a Admin) GetId() int { return a.Id }
func (a Admin) GetName() string { return a.Name }
func (a Admin) GetPassword() bcrypt.Password { return a.Password }
func (a Admin) GetCreatedAt() time.Time { return a.CreatedAt }
func (a Admin) GetUpdatedAt() time.Time { return a.UpdatedAt }
func (a Admin) GetDeletedAt() *time.Time { return a.DeletedAt }
func (a *Admin) SetId(id int) { a.Id = id }
func (a *Admin) SetName(name string) { a.Name = name }
func (a *Admin) SetPassword(password string) error { return a.Password.Update(password) }
func (a *Admin) SetCreatedAt(createdAt time.Time) { a.CreatedAt = createdAt }
func (a *Admin) SetUpdatedAt(updatedAt time.Time) { a.UpdatedAt = updatedAt }
func (a *Admin) SetDeletedAt(deletedAt *time.Time) { a.DeletedAt = deletedAt }
func (Admin) AfterCreateSchema(m psql.Model) string {
if m.Connection().DriverName() == "sqlite" {
return fmt.Sprintf("CREATE UNIQUE INDEX unique_admin ON %s (%s COLLATE NOCASE);",
m.TableName(), m.ToColumnName("Name"))
}
return fmt.Sprintf("CREATE UNIQUE INDEX unique_admin ON %s USING btree (lower(%s));",
m.TableName(), m.ToColumnName("Name"))
}
func (Admin) DataType(m psql.Model, fieldName string) (dataType string) {
if fieldName == "DeletedAt" {
if m.Connection() != nil && m.Connection().DriverName() == "sqlite" {
dataType = "timestamp"
} else {
dataType = "timestamptz"
}
}
return
}
func (a Admin) IsUnique(backend *Backend, field string) bool { // uniqueness
if field == "Name" {
return !backend.ModelByName("Admin").
Where("lower(name) = $1 AND id != $2", strings.ToLower(a.Name), a.Id).MustExists()
}
return true
}
var (
_ Serializable = (*Admin)(nil)
)
type (
adminForMe struct {
Id int
Name string
}
)
func (a Admin) Serialize(typ string, data ...interface{}) interface{} {
switch typ {
case "me":
return adminForMe{
Id: a.Id,
Name: a.Name,
}
}
return a
}
var (
_ IsAdminSession = (*AdminSession)(nil)
)
func (a AdminSession) GetId() int { return a.Id }
func (a AdminSession) GetAdminId() int { return a.AdminId }
func (a AdminSession) GetSessionId() string { return a.SessionId }
func (a AdminSession) GetIpAddress() string { return a.IpAddress }
func (a AdminSession) GetUserAgent() string { return a.UserAgent }
func (a AdminSession) GetCreatedAt() time.Time { return a.CreatedAt }
func (a AdminSession) GetUpdatedAt() time.Time { return a.UpdatedAt }
func (a *AdminSession) SetId(id int) { a.Id = id }
func (a *AdminSession) SetAdminId(adminId int) { a.AdminId = adminId }
func (a *AdminSession) SetSessionId(sessionId string) { a.SessionId = sessionId }
func (a *AdminSession) SetIpAddress(ipAddress string) { a.IpAddress = ipAddress }
func (a *AdminSession) SetUserAgent(userAgent string) { a.UserAgent = userAgent }
func (a *AdminSession) SetCreatedAt(createdAt time.Time) { a.CreatedAt = createdAt }
func (a *AdminSession) SetUpdatedAt(updatedAt time.Time) { a.UpdatedAt = updatedAt }
func (AdminSession) AfterCreateSchema(m psql.Model) string {
return fmt.Sprintf("CREATE UNIQUE INDEX unique_admin_session ON %s (%s, %s);",
m.TableName(), m.ToColumnName("AdminId"), m.ToColumnName("SessionId"))
}
func (AdminSession) DataType(m psql.Model, fieldName string) (dataType string) {
if fieldName == "SessionId" {
if m.Connection() != nil && m.Connection().DriverName() == "sqlite" {
dataType = "text NOT NULL DEFAULT (hex(randomblob(16)))"
} else {
dataType = "UUID NOT NULL DEFAULT gen_random_uuid()"
}
}
return
}