This repository has been archived by the owner on Jul 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
query.go
154 lines (133 loc) · 3.12 KB
/
query.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
package hero
import (
"errors"
"github.com/jinzhu/gorm"
)
type query struct {
*gorm.DB
}
func (q *query) ClientByCode(code string) (*Client, error) {
c := &Client{}
if code == "" {
return nil, errors.New("invalid code")
}
d := q.Where(&Client{UUID: code}).First(c)
if d.Error != nil {
return nil, d.Error
}
return c, nil
}
func (q *query) GrantByRefreshToken(code string) (*Grant, error) {
tok := &Token{}
d := q.Where(&Token{Code: code}).First(tok)
if d.Error != nil {
return nil, d.Error
}
g := &Grant{}
d = q.Where(&Grant{RefreshTokenID: tok.ID}).First(g)
if d.Error != nil {
return nil, d.Error
}
return g, nil
}
func (q *query) GrantByCode(code string) (*Grant, error) {
g := &Grant{}
d := q.Where(&Grant{Code: code}).Preload("AccessToken").Preload("AuthorizaToken").
Preload("RefreshToken").First(g)
if d.Error != nil {
return nil, d.Error
}
return g, nil
}
func (q *query) GrantByCLient(c *Client, code string) (*Grant, error) {
g := &Grant{}
d := q.Where(&Grant{Code: code, ClientID: c.ID}).Preload("AccessToken").Preload("AuthorizeToken").
Preload("RefreshToken").First(g)
if d.Error != nil {
return nil, d.Error
}
return g, nil
}
func (q *query) GrantByBearer(bearerCode string) (*Grant, error) {
tok, err := q.TokenByCode(bearerCode)
if err != nil {
return nil, err
}
g := &Grant{}
d := q.Where(&Grant{AccessTokenID: tok.ID}).
Preload("AccessToken").Preload("RefreshToken").First(g)
if d.Error != nil {
return nil, d.Error
}
return g, nil
}
func (q *query) SaveModel(model interface{}) error {
return q.Save(model).Error
}
func (q *query) UpdateModel(model interface{}) error {
return q.Update(model).Error
}
func (q *query) DeleteModel(model interface{}) error {
return q.Delete(model).Error
}
func (q *query) GetSessionByKey(key string) (*Session, error) {
ss := &Session{}
d := q.Where(&Session{Key: key}).First(ss)
if d.Error != nil {
return nil, d.Error
}
return ss, nil
}
func (q *query) UpdateSession(sess *Session) error {
ss, err := q.GetSessionByKey(sess.Key)
if err != nil {
return err
}
ss.Data = sess.Data
return q.Save(ss).Error
}
func (q *query) DeleteSession(key string) error {
ss, err := q.GetSessionByKey(key)
if err != nil {
return err
}
return q.Delete(ss).Error
}
func (q *query) SaveSession(ss *Session) error {
return q.Save(ss).Error
}
func (q *query) UserByID(id int64) (*User, error) {
usr := &User{}
d := q.Where(&User{ID: id}).First(usr)
if d.Error != nil {
return nil, d.Error
}
return usr, nil
}
func (q *query) UserByUserName(username string) (*User, error) {
usr := &User{}
d := q.Where(&User{UserName: username}).First(usr)
if d.Error != nil {
return nil, d.Error
}
return usr, nil
}
func (q *query) UserByEmail(email string) (*User, error) {
usr := &User{}
d := q.Where(&User{Email: email}).First(usr)
if d.Error != nil {
return nil, d.Error
}
return usr, nil
}
func (q *query) CreateUser(usr *User) error {
return q.Save(usr).Error
}
func (q *query) TokenByCode(code string) (*Token, error) {
tok := &Token{}
d := q.Where(&Token{Code: code}).First(tok)
if d.Error != nil {
return nil, d.Error
}
return tok, nil
}