This repository has been archived by the owner on Mar 11, 2021. It is now read-only.
/
verification_code.go
188 lines (157 loc) · 6.38 KB
/
verification_code.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
176
177
178
179
180
181
182
183
184
185
186
187
188
package repository
import (
"context"
"time"
repository "github.com/fabric8-services/fabric8-auth/application/repository/base"
"github.com/fabric8-services/fabric8-auth/errors"
"github.com/fabric8-services/fabric8-auth/gormsupport"
"github.com/fabric8-services/fabric8-auth/log"
"github.com/goadesign/goa"
"github.com/jinzhu/gorm"
errs "github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
)
type VerificationCode struct {
gormsupport.Lifecycle
ID uuid.UUID `sql:"type:uuid default uuid_generate_v4()" gorm:"primary_key"` // This is the ID PK field
User User
UserID uuid.UUID `sql:"type:uuid"`
Code string
}
// TableName overrides the table name settings in Gorm to force a specific table name
// in the database.
func (m VerificationCode) TableName() string {
return "verification_codes"
}
// GormVerificationCodeRepository is the implementation of the storage interface for
// VerificationCode.
type GormVerificationCodeRepository struct {
db *gorm.DB
}
// NewVerificationCodeRepository creates a new storage type.
func NewVerificationCodeRepository(db *gorm.DB) *GormVerificationCodeRepository {
return &GormVerificationCodeRepository{db: db}
}
// VerificationCodeRepository represents the storage interface.
type VerificationCodeRepository interface {
repository.Exister
Load(ctx context.Context, id uuid.UUID) (*VerificationCode, error)
LoadByCode(ctx context.Context, code string) ([]VerificationCode, error)
Create(ctx context.Context, VerificationCode *VerificationCode) error
Save(ctx context.Context, VerificationCode *VerificationCode) error
Delete(ctx context.Context, id uuid.UUID) error
Query(funcs ...func(*gorm.DB) *gorm.DB) ([]VerificationCode, error)
}
// TableName overrides the table name settings in Gorm to force a specific table name
// in the database.
func (m *GormVerificationCodeRepository) TableName() string {
return "verification_codes"
}
// CRUD Functions
// Load returns a single VerificationCode as a Database Model
// This is more for use internally, and probably not what you want in your controllers
func (m *GormVerificationCodeRepository) Load(ctx context.Context, id uuid.UUID) (*VerificationCode, error) {
defer goa.MeasureSince([]string{"goa", "db", "VerificationCode", "load"}, time.Now())
var native VerificationCode
err := m.db.Table(m.TableName()).Where("id = ?", id).Find(&native).Error
if err == gorm.ErrRecordNotFound {
return nil, errors.NewNotFoundError("verification_code_id", id.String())
}
return &native, errs.WithStack(err)
}
// LoadByCode loads result by filtering with respect to the verificate code.
func (m *GormVerificationCodeRepository) LoadByCode(ctx context.Context, code string) ([]VerificationCode, error) {
return m.Query(VerificationCodeWithUser(), VerificationCodeFilterByCode(code)) // maybe load with user?
}
// CheckExists returns nil if the given ID exists otherwise returns an error
func (m *GormVerificationCodeRepository) CheckExists(ctx context.Context, id string) error {
defer goa.MeasureSince([]string{"goa", "db", "VerificationCode", "exists"}, time.Now())
return repository.CheckExists(ctx, m.db, m.TableName(), id)
}
// Create creates a new record.
func (m *GormVerificationCodeRepository) Create(ctx context.Context, model *VerificationCode) error {
defer goa.MeasureSince([]string{"goa", "db", "VerificationCode", "create"}, time.Now())
if model.ID == uuid.Nil {
model.ID = uuid.NewV4()
}
err := m.db.Create(model).Error
if err != nil {
log.Error(ctx, map[string]interface{}{
"verification_code_id": model.ID,
"err": err,
}, "unable to create the verification_code")
return errs.WithStack(err)
}
log.Info(ctx, map[string]interface{}{
"verification_code_id": model.ID,
}, "verification_code created!")
return nil
}
// Save modifies a single record.
func (m *GormVerificationCodeRepository) Save(ctx context.Context, model *VerificationCode) error {
defer goa.MeasureSince([]string{"goa", "db", "VerificationCode", "save"}, time.Now())
obj, err := m.Load(ctx, model.ID)
if err != nil {
log.Error(ctx, map[string]interface{}{
"verification_code_id": model.ID,
"err": err,
}, "unable to update the verification_code")
return errs.WithStack(err)
}
err = m.db.Model(obj).Updates(model).Error
log.Debug(ctx, map[string]interface{}{
"verification_code_id": model.ID,
}, "verification_code saved!")
return errs.WithStack(err)
}
// Delete removes a single record. This is a hard delete!
func (m *GormVerificationCodeRepository) Delete(ctx context.Context, id uuid.UUID) error {
defer goa.MeasureSince([]string{"goa", "db", "VerificationCode", "delete"}, time.Now())
obj := VerificationCode{ID: id}
result := m.db.Delete(obj)
if result.Error != nil {
log.Error(ctx, map[string]interface{}{
"verification_code_id": id,
"err": result.Error,
}, "unable to delete the verification_code")
return errs.WithStack(result.Error)
}
if result.RowsAffected == 0 {
return errors.NewNotFoundError("verification_code", id.String())
}
log.Debug(ctx, map[string]interface{}{
"verification_code_id": id,
}, "verification_code deleted!")
return nil
}
// Query expose an open ended Query model
func (m *GormVerificationCodeRepository) Query(funcs ...func(*gorm.DB) *gorm.DB) ([]VerificationCode, error) {
defer goa.MeasureSince([]string{"goa", "db", "VerificationCode", "query"}, time.Now())
var verificationCodes []VerificationCode
err := m.db.Scopes(funcs...).Table(m.TableName()).Find(&verificationCodes).Error
if err != nil && err != gorm.ErrRecordNotFound {
return nil, errs.WithStack(err)
}
log.Debug(nil, map[string]interface{}{
"verification_code_query": verificationCodes,
}, "verification_code query executed successfully!")
return verificationCodes, nil
}
// VerificationCodeFilterByUserID is a gorm filter for a Belongs To relationship.
func VerificationCodeFilterByUserID(userID uuid.UUID) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
return db.Where("user_id = ?", userID)
}
}
// VerificationCodeFilterByCode is a gorm filter for a Belongs To relationship.
func VerificationCodeFilterByCode(code string) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
return db.Where("code = ?", code)
}
}
// VerificationCodeWithUser is a gorm filter for preloading the user relationship.
func VerificationCodeWithUser() func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
return db.Preload("User")
}
}