Dragonfly2
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
17package handlers
18
19import (
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]
40func (h *Handlers) CreateConfig(ctx *gin.Context) {
41var json types.CreateConfigRequest
42if err := ctx.ShouldBindJSON(&json); err != nil {
43ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
44return
45}
46
47config, err := h.service.CreateConfig(ctx.Request.Context(), json)
48if err != nil {
49ctx.Error(err) // nolint: errcheck
50return
51}
52
53ctx.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]
67func (h *Handlers) DestroyConfig(ctx *gin.Context) {
68var params types.ConfigParams
69if err := ctx.ShouldBindUri(¶ms); err != nil {
70ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
71return
72}
73
74if err := h.service.DestroyConfig(ctx.Request.Context(), params.ID); err != nil {
75ctx.Error(err) // nolint: errcheck
76return
77}
78
79ctx.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]
94func (h *Handlers) UpdateConfig(ctx *gin.Context) {
95var params types.ConfigParams
96if err := ctx.ShouldBindUri(¶ms); err != nil {
97ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
98return
99}
100
101var json types.UpdateConfigRequest
102if err := ctx.ShouldBindJSON(&json); err != nil {
103ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
104return
105}
106
107config, err := h.service.UpdateConfig(ctx.Request.Context(), params.ID, json)
108if err != nil {
109ctx.Error(err) // nolint: errcheck
110return
111}
112
113ctx.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]
127func (h *Handlers) GetConfig(ctx *gin.Context) {
128var params types.ConfigParams
129if err := ctx.ShouldBindUri(¶ms); err != nil {
130ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
131return
132}
133
134config, err := h.service.GetConfig(ctx.Request.Context(), params.ID)
135if err != nil {
136ctx.Error(err) // nolint: errcheck
137return
138}
139
140ctx.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]
155func (h *Handlers) GetConfigs(ctx *gin.Context) {
156var query types.GetConfigsQuery
157if err := ctx.ShouldBindQuery(&query); err != nil {
158ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
159return
160}
161
162h.setPaginationDefault(&query.Page, &query.PerPage)
163configs, count, err := h.service.GetConfigs(ctx.Request.Context(), query)
164if err != nil {
165ctx.Error(err) // nolint: errcheck
166return
167}
168
169h.setPaginationLinkHeader(ctx, query.Page, query.PerPage, int(count))
170ctx.JSON(http.StatusOK, configs)
171}
172