code.gitea.io/gitea@v1.22.3/routers/api/v1/admin/user_badge.go (about) 1 // Copyright 2023 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package admin 5 6 import ( 7 "net/http" 8 9 user_model "code.gitea.io/gitea/models/user" 10 api "code.gitea.io/gitea/modules/structs" 11 "code.gitea.io/gitea/modules/web" 12 "code.gitea.io/gitea/services/context" 13 ) 14 15 // ListUserBadges lists all badges belonging to a user 16 func ListUserBadges(ctx *context.APIContext) { 17 // swagger:operation GET /admin/users/{username}/badges admin adminListUserBadges 18 // --- 19 // summary: List a user's badges 20 // produces: 21 // - application/json 22 // parameters: 23 // - name: username 24 // in: path 25 // description: username of user 26 // type: string 27 // required: true 28 // responses: 29 // "200": 30 // "$ref": "#/responses/BadgeList" 31 // "404": 32 // "$ref": "#/responses/notFound" 33 34 badges, maxResults, err := user_model.GetUserBadges(ctx, ctx.ContextUser) 35 if err != nil { 36 ctx.Error(http.StatusInternalServerError, "GetUserBadges", err) 37 return 38 } 39 40 ctx.SetTotalCountHeader(maxResults) 41 ctx.JSON(http.StatusOK, &badges) 42 } 43 44 // AddUserBadges add badges to a user 45 func AddUserBadges(ctx *context.APIContext) { 46 // swagger:operation POST /admin/users/{username}/badges admin adminAddUserBadges 47 // --- 48 // summary: Add a badge to a user 49 // consumes: 50 // - application/json 51 // produces: 52 // - application/json 53 // parameters: 54 // - name: username 55 // in: path 56 // description: username of user 57 // type: string 58 // required: true 59 // - name: body 60 // in: body 61 // schema: 62 // "$ref": "#/definitions/UserBadgeOption" 63 // responses: 64 // "204": 65 // "$ref": "#/responses/empty" 66 // "403": 67 // "$ref": "#/responses/forbidden" 68 69 form := web.GetForm(ctx).(*api.UserBadgeOption) 70 badges := prepareBadgesForReplaceOrAdd(ctx, *form) 71 72 if err := user_model.AddUserBadges(ctx, ctx.ContextUser, badges); err != nil { 73 ctx.Error(http.StatusInternalServerError, "ReplaceUserBadges", err) 74 return 75 } 76 77 ctx.Status(http.StatusNoContent) 78 } 79 80 // DeleteUserBadges delete a badge from a user 81 func DeleteUserBadges(ctx *context.APIContext) { 82 // swagger:operation DELETE /admin/users/{username}/badges admin adminDeleteUserBadges 83 // --- 84 // summary: Remove a badge from a user 85 // produces: 86 // - application/json 87 // parameters: 88 // - name: username 89 // in: path 90 // description: username of user 91 // type: string 92 // required: true 93 // - name: body 94 // in: body 95 // schema: 96 // "$ref": "#/definitions/UserBadgeOption" 97 // responses: 98 // "204": 99 // "$ref": "#/responses/empty" 100 // "403": 101 // "$ref": "#/responses/forbidden" 102 // "422": 103 // "$ref": "#/responses/validationError" 104 105 form := web.GetForm(ctx).(*api.UserBadgeOption) 106 badges := prepareBadgesForReplaceOrAdd(ctx, *form) 107 108 if err := user_model.RemoveUserBadges(ctx, ctx.ContextUser, badges); err != nil { 109 ctx.Error(http.StatusInternalServerError, "ReplaceUserBadges", err) 110 return 111 } 112 113 ctx.Status(http.StatusNoContent) 114 } 115 116 func prepareBadgesForReplaceOrAdd(ctx *context.APIContext, form api.UserBadgeOption) []*user_model.Badge { 117 badges := make([]*user_model.Badge, len(form.BadgeSlugs)) 118 for i, badge := range form.BadgeSlugs { 119 badges[i] = &user_model.Badge{ 120 Slug: badge, 121 } 122 } 123 return badges 124 }