-
Notifications
You must be signed in to change notification settings - Fork 28
/
step.go
118 lines (97 loc) · 2.74 KB
/
step.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/steps admin AdminAllSteps
//
// Get all of the steps in the database
//
// ---
// produces:
// - application/json
// security:
// - ApiKeyAuth: []
// responses:
// '200':
// description: Successfully retrieved all steps from the database
// schema:
// type: array
// items:
// "$ref": "#/definitions/Step"
// '500':
// description: Unable to retrieve all steps from the database
// schema:
// "$ref": "#/definitions/Error"
// AllSteps represents the API handler to
// captures all steps stored in the database.
func AllSteps(c *gin.Context) {
logrus.Info("Admin: reading all steps")
// send API call to capture all steps
s, err := database.FromContext(c).GetStepList()
if err != nil {
retErr := fmt.Errorf("unable to capture all steps: %w", err)
util.HandleError(c, http.StatusInternalServerError, retErr)
return
}
c.JSON(http.StatusOK, s)
}
// swagger:operation PUT /api/v1/admin/step admin AdminUpdateStep
//
// Update a step in the database
//
// ---
// produces:
// - application/json
// parameters:
// - in: body
// name: body
// description: Payload containing step to update
// required: true
// schema:
// "$ref": "#/definitions/Step"
// security:
// - ApiKeyAuth: []
// responses:
// '200':
// description: Successfully updated the step in the database
// schema:
// "$ref": "#/definitions/Step"
// '404':
// description: Unable to update the step in the database
// schema:
// "$ref": "#/definitions/Error"
// '500':
// description: Unable to update the step in the database
// schema:
// "$ref": "#/definitions/Error"
// UpdateStep represents the API handler to
// update any step stored in the database.
func UpdateStep(c *gin.Context) {
logrus.Info("Admin: updating step in database")
// capture body from API request
input := new(library.Step)
err := c.Bind(input)
if err != nil {
retErr := fmt.Errorf("unable to decode JSON for step %d: %w", input.GetID(), err)
util.HandleError(c, http.StatusNotFound, retErr)
return
}
// send API call to update the step
err = database.FromContext(c).UpdateStep(input)
if err != nil {
retErr := fmt.Errorf("unable to update step %d: %w", input.GetID(), err)
util.HandleError(c, http.StatusInternalServerError, retErr)
return
}
c.JSON(http.StatusOK, input)
}