@@ -6,11 +6,9 @@ package user
6
6
import (
7
7
"context"
8
8
"fmt"
9
- "net/url"
10
9
"strings"
11
10
12
11
"code.gitea.io/gitea/models/db"
13
- "code.gitea.io/gitea/modules/setting"
14
12
15
13
"xorm.io/builder"
16
14
"xorm.io/xorm"
@@ -36,10 +34,6 @@ func init() {
36
34
db .RegisterModel (new (UserBadge ))
37
35
}
38
36
39
- func AdminCreateBadge (ctx context.Context , badge * Badge ) error {
40
- return CreateBadge (ctx , badge )
41
- }
42
-
43
37
// GetUserBadges returns the user's badges.
44
38
func GetUserBadges (ctx context.Context , u * User ) ([]* Badge , int64 , error ) {
45
39
sess := db .GetEngine (ctx ).
@@ -52,34 +46,27 @@ func GetUserBadges(ctx context.Context, u *User) ([]*Badge, int64, error) {
52
46
return badges , count , err
53
47
}
54
48
55
- // GetBadgeUsers returns the badges users.
49
+ // GetBadgeUsers returns the users that have a specific badge .
56
50
func GetBadgeUsers (ctx context.Context , b * Badge ) ([]* User , int64 , error ) {
57
51
sess := db .GetEngine (ctx ).
58
52
Select ("`user`.*" ).
59
53
Join ("INNER" , "user_badge" , "`user_badge`.user_id=user.id" ).
60
- Where ( " user_badge.badge_id=?" , b . ID )
61
-
54
+ Join ( "INNER" , "badge" , "` user_badge` .badge_id=badge.id" ).
55
+ Where ( "badge.slug=?" , b . Slug )
62
56
users := make ([]* User , 0 , 8 )
63
57
count , err := sess .FindAndCount (& users )
64
58
return users , count , err
65
59
}
66
60
67
61
// CreateBadge creates a new badge.
68
62
func CreateBadge (ctx context.Context , badge * Badge ) error {
69
- isExist , err := IsBadgeExist (ctx , 0 , badge .Slug )
70
-
71
- if err != nil {
72
- return err
73
- } else if isExist {
74
- return ErrBadgeAlreadyExist {badge .Slug }
75
- }
76
-
77
- _ , err = db .GetEngine (ctx ).Insert (badge )
63
+ // this will fail if the badge already exists due to the UNIQUE constraint
64
+ _ , err := db .GetEngine (ctx ).Insert (badge )
78
65
79
66
return err
80
67
}
81
68
82
- // GetBadge returns a badge
69
+ // GetBadge returns a specific badge
83
70
func GetBadge (ctx context.Context , slug string ) (* Badge , error ) {
84
71
badge := new (Badge )
85
72
has , err := db .GetEngine (ctx ).Where ("slug=?" , slug ).Get (badge )
@@ -89,22 +76,9 @@ func GetBadge(ctx context.Context, slug string) (*Badge, error) {
89
76
return badge , err
90
77
}
91
78
92
- // GetBadgeByID returns a badge
93
- func GetBadgeByID (ctx context.Context , id int64 ) (* Badge , error ) {
94
- badge := new (Badge )
95
- has , err := db .GetEngine (ctx ).Where ("id=?" , id ).Get (badge )
96
- if err != nil {
97
- return nil , err
98
- } else if ! has {
99
- return nil , ErrBadgeNotExist {ID : id }
100
- }
101
-
102
- return badge , err
103
- }
104
-
105
79
// UpdateBadge updates a badge based on its slug.
106
80
func UpdateBadge (ctx context.Context , badge * Badge ) error {
107
- _ , err := db .GetEngine (ctx ).Where ("id =?" , badge .ID ).Cols ("slug" , "description" , "image_url" ).Update (badge )
81
+ _ , err := db .GetEngine (ctx ).Where ("slug =?" , badge .Slug ).Cols ("description" , "image_url" ).Update (badge )
108
82
return err
109
83
}
110
84
@@ -114,24 +88,8 @@ func DeleteBadge(ctx context.Context, badge *Badge) error {
114
88
return err
115
89
}
116
90
117
- // DeleteUserBadgeRecord deletes a user badge record.
118
- func DeleteUserBadgeRecord (ctx context.Context , badge * Badge ) error {
119
- userBadge := & UserBadge {
120
- BadgeID : badge .ID ,
121
- }
122
- _ , err := db .GetEngine (ctx ).Where ("badge_id=?" , userBadge .BadgeID ).Delete (userBadge )
123
- return err
124
- }
125
-
126
91
// AddUserBadge adds a badge to a user.
127
92
func AddUserBadge (ctx context.Context , u * User , badge * Badge ) error {
128
- isExist , err := IsBadgeUserExist (ctx , u .ID , badge .ID )
129
- if err != nil {
130
- return err
131
- } else if isExist {
132
- return ErrBadgeAlreadyExist {}
133
- }
134
-
135
93
return AddUserBadges (ctx , u , []* Badge {badge })
136
94
}
137
95
@@ -140,11 +98,11 @@ func AddUserBadges(ctx context.Context, u *User, badges []*Badge) error {
140
98
return db .WithTx (ctx , func (ctx context.Context ) error {
141
99
for _ , badge := range badges {
142
100
// hydrate badge and check if it exists
143
- has , err := db .GetEngine (ctx ).Where ("id =?" , badge .ID ).Get (badge )
101
+ has , err := db .GetEngine (ctx ).Where ("slug =?" , badge .Slug ).Get (badge )
144
102
if err != nil {
145
103
return err
146
104
} else if ! has {
147
- return ErrBadgeNotExist {ID : badge .ID }
105
+ return ErrBadgeNotExist {Slug : badge .Slug }
148
106
}
149
107
if err := db .Insert (ctx , & UserBadge {
150
108
BadgeID : badge .ID ,
@@ -162,11 +120,19 @@ func RemoveUserBadge(ctx context.Context, u *User, badge *Badge) error {
162
120
return RemoveUserBadges (ctx , u , []* Badge {badge })
163
121
}
164
122
165
- // RemoveUserBadges removes badges from a user.
123
+ // RemoveUserBadges removes specific badges from a user.
166
124
func RemoveUserBadges (ctx context.Context , u * User , badges []* Badge ) error {
167
125
return db .WithTx (ctx , func (ctx context.Context ) error {
168
126
for _ , badge := range badges {
169
- if _ , err := db .GetEngine (ctx ).Delete (& UserBadge {BadgeID : badge .ID , UserID : u .ID }); err != nil {
127
+ subQuery := builder .
128
+ Select ("id" ).
129
+ From ("badge" ).
130
+ Where (builder.Eq {"slug" : badge .Slug })
131
+
132
+ if _ , err := db .GetEngine (ctx ).
133
+ Where ("`user_badge`.user_id=?" , u .ID ).
134
+ And (builder .In ("badge_id" , subQuery )).
135
+ Delete (& UserBadge {}); err != nil {
170
136
return err
171
137
}
172
138
}
@@ -180,28 +146,6 @@ func RemoveAllUserBadges(ctx context.Context, u *User) error {
180
146
return err
181
147
}
182
148
183
- // HTMLURL returns the badges full link.
184
- func (u * Badge ) HTMLURL () string {
185
- return setting .AppURL + url .PathEscape (u .Slug )
186
- }
187
-
188
- // IsBadgeExist checks if given badge slug exist,
189
- // it is used when creating/updating a badge slug
190
- func IsBadgeExist (ctx context.Context , uid int64 , slug string ) (bool , error ) {
191
- if len (slug ) == 0 {
192
- return false , nil
193
- }
194
- return db .GetEngine (ctx ).
195
- Where ("slug!=?" , uid ).
196
- Get (& Badge {Slug : strings .ToLower (slug )})
197
- }
198
-
199
- // IsBadgeUserExist checks if given badge id, uid exist,
200
- func IsBadgeUserExist (ctx context.Context , uid , bid int64 ) (bool , error ) {
201
- return db .GetEngine (ctx ).
202
- Get (& UserBadge {UserID : uid , BadgeID : bid })
203
- }
204
-
205
149
// SearchBadgeOptions represents the options when fdin badges
206
150
type SearchBadgeOptions struct {
207
151
db.ListOptions
0 commit comments