-
Notifications
You must be signed in to change notification settings - Fork 28
/
user.go
118 lines (97 loc) · 2.72 KB
/
user.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
// Copyright (c) 2021 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.
// nolint: dupl // ignore similar code
package admin
import (
"fmt"
"net/http"
"github.com/go-vela/server/database"
"github.com/go-vela/server/util"
"github.com/go-vela/types/library"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)
// swagger:operation GET /api/v1/admin/users admin AdminAllUsers
//
// Get all of the users in the database
//
// ---
// produces:
// - application/json
// security:
// - ApiKeyAuth: []
// responses:
// '200':
// description: Successfully retrieved all users from the database
// schema:
// type: array
// items:
// "$ref": "#/definitions/User"
// '500':
// description: Unable to retrieve all users from the database
// schema:
// type: string
// AllUsers represents the API handler to
// captures all users stored in the database.
func AllUsers(c *gin.Context) {
logrus.Info("Admin: reading all users")
// send API call to capture all users
u, err := database.FromContext(c).GetUserList()
if err != nil {
retErr := fmt.Errorf("unable to capture all users: %w", err)
util.HandleError(c, http.StatusInternalServerError, retErr)
return
}
c.JSON(http.StatusOK, u)
}
// swagger:operation PUT /api/v1/admin/user admin AdminUpdateUser
//
// Update a user in the database
//
// ---
// produces:
// - application/json
// parameters:
// - in: body
// name: body
// description: Payload containing user to update
// required: true
// schema:
// "$ref": "#/definitions/User"
// security:
// - ApiKeyAuth: []
// responses:
// '200':
// description: Successfully updated the user in the database
// schema:
// "$ref": "#/definitions/User"
// '404':
// description: Unable to update the user in the database
// schema:
// "$ref": "#/definitions/Error"
// '500':
// description: Unable to update the user in the database
// schema:
// "$ref": "#/definitions/Error"
// UpdateUser represents the API handler to
// update any user stored in the database.
func UpdateUser(c *gin.Context) {
logrus.Info("Admin: updating user in database")
// capture body from API request
input := new(library.User)
err := c.Bind(input)
if err != nil {
retErr := fmt.Errorf("unable to decode JSON for user %d: %w", input.GetID(), err)
util.HandleError(c, http.StatusNotFound, retErr)
return
}
// send API call to update the user
err = database.FromContext(c).UpdateUser(input)
if err != nil {
retErr := fmt.Errorf("unable to update user %d: %w", input.GetID(), err)
util.HandleError(c, http.StatusInternalServerError, retErr)
return
}
c.JSON(http.StatusOK, input)
}