@@ -6,11 +6,9 @@ package user
66import (
77 "context"
88 "fmt"
9- "net/url"
109 "strings"
1110
1211 "code.gitea.io/gitea/models/db"
13- "code.gitea.io/gitea/modules/setting"
1412
1513 "xorm.io/builder"
1614 "xorm.io/xorm"
@@ -36,10 +34,6 @@ func init() {
3634 db .RegisterModel (new (UserBadge ))
3735}
3836
39- func AdminCreateBadge (ctx context.Context , badge * Badge ) error {
40- return CreateBadge (ctx , badge )
41- }
42-
4337// GetUserBadges returns the user's badges.
4438func GetUserBadges (ctx context.Context , u * User ) ([]* Badge , int64 , error ) {
4539 sess := db .GetEngine (ctx ).
@@ -52,34 +46,27 @@ func GetUserBadges(ctx context.Context, u *User) ([]*Badge, int64, error) {
5246 return badges , count , err
5347}
5448
55- // GetBadgeUsers returns the badges users.
49+ // GetBadgeUsers returns the users that have a specific badge .
5650func GetBadgeUsers (ctx context.Context , b * Badge ) ([]* User , int64 , error ) {
5751 sess := db .GetEngine (ctx ).
5852 Select ("`user`.*" ).
5953 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 )
6256 users := make ([]* User , 0 , 8 )
6357 count , err := sess .FindAndCount (& users )
6458 return users , count , err
6559}
6660
6761// CreateBadge creates a new badge.
6862func 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 )
7865
7966 return err
8067}
8168
82- // GetBadge returns a badge
69+ // GetBadge returns a specific badge
8370func GetBadge (ctx context.Context , slug string ) (* Badge , error ) {
8471 badge := new (Badge )
8572 has , err := db .GetEngine (ctx ).Where ("slug=?" , slug ).Get (badge )
@@ -89,22 +76,9 @@ func GetBadge(ctx context.Context, slug string) (*Badge, error) {
8976 return badge , err
9077}
9178
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-
10579// UpdateBadge updates a badge based on its slug.
10680func 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 )
10882 return err
10983}
11084
@@ -114,24 +88,8 @@ func DeleteBadge(ctx context.Context, badge *Badge) error {
11488 return err
11589}
11690
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-
12691// AddUserBadge adds a badge to a user.
12792func 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-
13593 return AddUserBadges (ctx , u , []* Badge {badge })
13694}
13795
@@ -140,11 +98,11 @@ func AddUserBadges(ctx context.Context, u *User, badges []*Badge) error {
14098 return db .WithTx (ctx , func (ctx context.Context ) error {
14199 for _ , badge := range badges {
142100 // 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 )
144102 if err != nil {
145103 return err
146104 } else if ! has {
147- return ErrBadgeNotExist {ID : badge .ID }
105+ return ErrBadgeNotExist {Slug : badge .Slug }
148106 }
149107 if err := db .Insert (ctx , & UserBadge {
150108 BadgeID : badge .ID ,
@@ -162,11 +120,19 @@ func RemoveUserBadge(ctx context.Context, u *User, badge *Badge) error {
162120 return RemoveUserBadges (ctx , u , []* Badge {badge })
163121}
164122
165- // RemoveUserBadges removes badges from a user.
123+ // RemoveUserBadges removes specific badges from a user.
166124func RemoveUserBadges (ctx context.Context , u * User , badges []* Badge ) error {
167125 return db .WithTx (ctx , func (ctx context.Context ) error {
168126 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 {
170136 return err
171137 }
172138 }
@@ -180,28 +146,6 @@ func RemoveAllUserBadges(ctx context.Context, u *User) error {
180146 return err
181147}
182148
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-
205149// SearchBadgeOptions represents the options when fdin badges
206150type SearchBadgeOptions struct {
207151 db.ListOptions
0 commit comments