Dragonfly2

Форк
0
171 строка · 4.4 Кб
1
/*
2
 *     Copyright 2020 The Dragonfly Authors
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package handlers
18

19
import (
20
	"net/http"
21

22
	"github.com/gin-gonic/gin"
23

24
	// nolint
25
	_ "d7y.io/dragonfly/v2/manager/models"
26
	"d7y.io/dragonfly/v2/manager/types"
27
)
28

29
// @Summary Create Config
30
// @Description Create by json config
31
// @Tags Config
32
// @Accept json
33
// @Produce json
34
// @Param Config body types.CreateConfigRequest true "Config"
35
// @Success 200 {object} models.Config
36
// @Failure 400
37
// @Failure 404
38
// @Failure 500
39
// @Router /configs [post]
40
func (h *Handlers) CreateConfig(ctx *gin.Context) {
41
	var json types.CreateConfigRequest
42
	if err := ctx.ShouldBindJSON(&json); err != nil {
43
		ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
44
		return
45
	}
46

47
	config, err := h.service.CreateConfig(ctx.Request.Context(), json)
48
	if err != nil {
49
		ctx.Error(err) // nolint: errcheck
50
		return
51
	}
52

53
	ctx.JSON(http.StatusOK, config)
54
}
55

56
// @Summary Destroy Config
57
// @Description Destroy by id
58
// @Tags Config
59
// @Accept json
60
// @Produce json
61
// @Param id path string true "id"
62
// @Success 200
63
// @Failure 400
64
// @Failure 404
65
// @Failure 500
66
// @Router /configs/{id} [delete]
67
func (h *Handlers) DestroyConfig(ctx *gin.Context) {
68
	var params types.ConfigParams
69
	if err := ctx.ShouldBindUri(&params); err != nil {
70
		ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
71
		return
72
	}
73

74
	if err := h.service.DestroyConfig(ctx.Request.Context(), params.ID); err != nil {
75
		ctx.Error(err) // nolint: errcheck
76
		return
77
	}
78

79
	ctx.Status(http.StatusOK)
80
}
81

82
// @Summary Update Config
83
// @Description Update by json config
84
// @Tags Config
85
// @Accept json
86
// @Produce json
87
// @Param id path string true "id"
88
// @Param Config body types.UpdateConfigRequest true "Config"
89
// @Success 200 {object} models.Config
90
// @Failure 400
91
// @Failure 404
92
// @Failure 500
93
// @Router /configs/{id} [patch]
94
func (h *Handlers) UpdateConfig(ctx *gin.Context) {
95
	var params types.ConfigParams
96
	if err := ctx.ShouldBindUri(&params); err != nil {
97
		ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
98
		return
99
	}
100

101
	var json types.UpdateConfigRequest
102
	if err := ctx.ShouldBindJSON(&json); err != nil {
103
		ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
104
		return
105
	}
106

107
	config, err := h.service.UpdateConfig(ctx.Request.Context(), params.ID, json)
108
	if err != nil {
109
		ctx.Error(err) // nolint: errcheck
110
		return
111
	}
112

113
	ctx.JSON(http.StatusOK, config)
114
}
115

116
// @Summary Get Config
117
// @Description Get Config by id
118
// @Tags Config
119
// @Accept json
120
// @Produce json
121
// @Param id path string true "id"
122
// @Success 200 {object} models.Config
123
// @Failure 400
124
// @Failure 404
125
// @Failure 500
126
// @Router /configs/{id} [get]
127
func (h *Handlers) GetConfig(ctx *gin.Context) {
128
	var params types.ConfigParams
129
	if err := ctx.ShouldBindUri(&params); err != nil {
130
		ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
131
		return
132
	}
133

134
	config, err := h.service.GetConfig(ctx.Request.Context(), params.ID)
135
	if err != nil {
136
		ctx.Error(err) // nolint: errcheck
137
		return
138
	}
139

140
	ctx.JSON(http.StatusOK, config)
141
}
142

143
// @Summary Get Configs
144
// @Description Get Configs
145
// @Tags Config
146
// @Accept json
147
// @Produce json
148
// @Param page query int true "current page" default(0)
149
// @Param per_page query int true "return max item count, default 10, max 50" default(10) minimum(2) maximum(50)
150
// @Success 200 {object} []models.Config
151
// @Failure 400
152
// @Failure 404
153
// @Failure 500
154
// @Router /configs [get]
155
func (h *Handlers) GetConfigs(ctx *gin.Context) {
156
	var query types.GetConfigsQuery
157
	if err := ctx.ShouldBindQuery(&query); err != nil {
158
		ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
159
		return
160
	}
161

162
	h.setPaginationDefault(&query.Page, &query.PerPage)
163
	configs, count, err := h.service.GetConfigs(ctx.Request.Context(), query)
164
	if err != nil {
165
		ctx.Error(err) // nolint: errcheck
166
		return
167
	}
168

169
	h.setPaginationLinkHeader(ctx, query.Page, query.PerPage, int(count))
170
	ctx.JSON(http.StatusOK, configs)
171
}
172

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.