-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
105 lines (86 loc) · 2.63 KB
/
model.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
package login
import (
"errors"
"time"
"github.com/google/uuid"
"github.com/monologid/m9/db"
"github.com/monologid/m9/util"
"gorm.io/datatypes"
)
// Migrate executes database auto-migrate schema
// Please note that the auto migrate will create tables,
// missing foreign keys, constraints, columns and indexes,
// and will change existing column’s type if it’s size, precision,
// nullable changed, it WON’T delete unused columns to protect your data
func Migrate() {
db.DB.AutoMigrate(
&AccountModel{},
&AccountServiceProviderModel{},
)
}
// AccountModel is a schema for account
type AccountModel struct {
db.Model
FirstName string `gorm:"type:varchar(255);not null;"`
LastName string `gorm:"type:varchar(255)"`
Email string `gorm:"type:varchar(255);not null;index"`
Password string `gorm:"type:varchar(255);"`
PicURL string
Birthday *time.Time
IsVerified bool `gorm:"default:FALSE"`
AccountServiceProvider AccountServiceProviderModel `gorm:"foreignKey:AccountID"`
}
// TableName returns account table name
func (m *AccountModel) TableName() string {
return "account"
}
// AccountServiceProviderModel is a schema for account service provider
type AccountServiceProviderModel struct {
db.Model
AccountID uuid.UUID `gorm:"not null;"`
ServiceProvider string `gorm:"type:varchar(20);not null;"`
ServiceProviderID string `gorm:"type:varchar(50);not null;"`
PicURL string
Metadata datatypes.JSON
}
// TableName returns account service provider table name
func (m *AccountServiceProviderModel) TableName() string {
return "account_service_provider"
}
// ReqRegisterModel is a schema for submitting new account registration
type ReqRegisterModel struct {
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Email string `json:"email"`
Password string `json:"password"`
}
// Validate validates registration submission
func (r ReqRegisterModel) Validate() error {
if len(r.FirstName) == 0 {
return errors.New("invalid first name")
}
var email util.Email = util.Email(r.Email)
if !email.Validate() {
return errors.New("invalid email address")
}
if len(r.Password) == 0 {
return errors.New("invalid password")
}
return nil
}
// ReqLoginModel is a schema for submitting login
type ReqLoginModel struct {
Email string `json:"email"`
Password string `json:"password"`
}
// Validate validates login request
func (r ReqLoginModel) Validate() error {
var email util.Email = util.Email(r.Email)
if !email.Validate() {
return errors.New("invalid email address")
}
if len(r.Password) == 0 {
return errors.New("invalid password")
}
return nil
}