Dragonfly2
171 строка · 4.6 Кб
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 Scheduler
30// @Description Create by json config
31// @Tags Scheduler
32// @Accept json
33// @Produce json
34// @Param Scheduler body types.CreateSchedulerRequest true "Scheduler"
35// @Success 200 {object} models.Scheduler
36// @Failure 400
37// @Failure 404
38// @Failure 500
39// @Router /schedulers [post]
40func (h *Handlers) CreateScheduler(ctx *gin.Context) {
41var json types.CreateSchedulerRequest
42if err := ctx.ShouldBindJSON(&json); err != nil {
43ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
44return
45}
46
47scheduler, err := h.service.CreateScheduler(ctx.Request.Context(), json)
48if err != nil {
49ctx.Error(err) // nolint: errcheck
50return
51}
52
53ctx.JSON(http.StatusOK, scheduler)
54}
55
56// @Summary Destroy Scheduler
57// @Description Destroy by id
58// @Tags Scheduler
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 /schedulers/{id} [delete]
67func (h *Handlers) DestroyScheduler(ctx *gin.Context) {
68var params types.SchedulerParams
69if err := ctx.ShouldBindUri(¶ms); err != nil {
70ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
71return
72}
73
74if err := h.service.DestroyScheduler(ctx.Request.Context(), params.ID); err != nil {
75ctx.Error(err) // nolint: errcheck
76return
77}
78
79ctx.Status(http.StatusOK)
80}
81
82// @Summary Update Scheduler
83// @Description Update by json config
84// @Tags Scheduler
85// @Accept json
86// @Produce json
87// @Param id path string true "id"
88// @Param Scheduler body types.UpdateSchedulerRequest true "Scheduler"
89// @Success 200 {object} models.Scheduler
90// @Failure 400
91// @Failure 404
92// @Failure 500
93// @Router /schedulers/{id} [patch]
94func (h *Handlers) UpdateScheduler(ctx *gin.Context) {
95var params types.SchedulerParams
96if err := ctx.ShouldBindUri(¶ms); err != nil {
97ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
98return
99}
100
101var json types.UpdateSchedulerRequest
102if err := ctx.ShouldBindJSON(&json); err != nil {
103ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
104return
105}
106
107scheduler, err := h.service.UpdateScheduler(ctx.Request.Context(), params.ID, json)
108if err != nil {
109ctx.Error(err) // nolint: errcheck
110return
111}
112
113ctx.JSON(http.StatusOK, scheduler)
114}
115
116// @Summary Get Scheduler
117// @Description Get Scheduler by id
118// @Tags Scheduler
119// @Accept json
120// @Produce json
121// @Param id path string true "id"
122// @Success 200 {object} models.Scheduler
123// @Failure 400
124// @Failure 404
125// @Failure 500
126// @Router /schedulers/{id} [get]
127func (h *Handlers) GetScheduler(ctx *gin.Context) {
128var params types.SchedulerParams
129if err := ctx.ShouldBindUri(¶ms); err != nil {
130ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
131return
132}
133
134scheduler, err := h.service.GetScheduler(ctx.Request.Context(), params.ID)
135if err != nil {
136ctx.Error(err) // nolint: errcheck
137return
138}
139
140ctx.JSON(http.StatusOK, scheduler)
141}
142
143// @Summary Get Schedulers
144// @Description Get Schedulers
145// @Tags Scheduler
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.Scheduler
151// @Failure 400
152// @Failure 404
153// @Failure 500
154// @Router /schedulers [get]
155func (h *Handlers) GetSchedulers(ctx *gin.Context) {
156var query types.GetSchedulersQuery
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)
163schedulers, count, err := h.service.GetSchedulers(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, schedulers)
171}
172