Dragonfly2
183 строки · 4.7 Кб
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"github.com/gin-gonic/gin/binding"
24
25"d7y.io/dragonfly/v2/internal/job"
26_ "d7y.io/dragonfly/v2/manager/models" // nolint
27"d7y.io/dragonfly/v2/manager/types"
28)
29
30// @Summary Create Job
31// @Description Create by json config
32// @Tags Job
33// @Accept json
34// @Produce json
35// @Param Job body types.CreateJobRequest true "Job"
36// @Success 200 {object} models.Job
37// @Failure 400
38// @Failure 404
39// @Failure 500
40// @Router /jobs [post]
41func (h *Handlers) CreateJob(ctx *gin.Context) {
42var json types.CreateJobRequest
43if err := ctx.ShouldBindBodyWith(&json, binding.JSON); err != nil {
44ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
45return
46}
47
48switch json.Type {
49case job.PreheatJob:
50var json types.CreatePreheatJobRequest
51if err := ctx.ShouldBindBodyWith(&json, binding.JSON); err != nil {
52ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
53return
54}
55
56job, err := h.service.CreatePreheatJob(ctx.Request.Context(), json)
57if err != nil {
58ctx.Error(err) // nolint: errcheck
59return
60}
61
62ctx.JSON(http.StatusOK, job)
63default:
64ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": "Unknow type"})
65}
66}
67
68// @Summary Destroy Job
69// @Description Destroy by id
70// @Tags Job
71// @Accept json
72// @Produce json
73// @Param id path string true "id"
74// @Success 200
75// @Failure 400
76// @Failure 404
77// @Failure 500
78// @Router /jobs/{id} [delete]
79func (h *Handlers) DestroyJob(ctx *gin.Context) {
80var params types.JobParams
81if err := ctx.ShouldBindUri(¶ms); err != nil {
82ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
83return
84}
85
86if err := h.service.DestroyJob(ctx.Request.Context(), params.ID); err != nil {
87ctx.Error(err) // nolint: errcheck
88return
89}
90
91ctx.Status(http.StatusOK)
92}
93
94// @Summary Update Job
95// @Description Update by json config
96// @Tags Job
97// @Accept json
98// @Produce json
99// @Param id path string true "id"
100// @Param Job body types.UpdateJobRequest true "Job"
101// @Success 200 {object} models.Job
102// @Failure 400
103// @Failure 404
104// @Failure 500
105// @Router /jobs/{id} [patch]
106func (h *Handlers) UpdateJob(ctx *gin.Context) {
107var params types.JobParams
108if err := ctx.ShouldBindUri(¶ms); err != nil {
109ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
110return
111}
112
113var json types.UpdateJobRequest
114if err := ctx.ShouldBindJSON(&json); err != nil {
115ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
116return
117}
118
119job, err := h.service.UpdateJob(ctx.Request.Context(), params.ID, json)
120if err != nil {
121ctx.Error(err) // nolint: errcheck
122return
123}
124
125ctx.JSON(http.StatusOK, job)
126}
127
128// @Summary Get Job
129// @Description Get Job by id
130// @Tags Job
131// @Accept json
132// @Produce json
133// @Param id path string true "id"
134// @Success 200 {object} models.Job
135// @Failure 400
136// @Failure 404
137// @Failure 500
138// @Router /jobs/{id} [get]
139func (h *Handlers) GetJob(ctx *gin.Context) {
140var params types.JobParams
141if err := ctx.ShouldBindUri(¶ms); err != nil {
142ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
143return
144}
145
146job, err := h.service.GetJob(ctx.Request.Context(), params.ID)
147if err != nil {
148ctx.Error(err) // nolint: errcheck
149return
150}
151
152ctx.JSON(http.StatusOK, job)
153}
154
155// @Summary Get Jobs
156// @Description Get Jobs
157// @Tags Job
158// @Accept json
159// @Produce json
160// @Param page query int true "current page" default(0)
161// @Param per_page query int true "return max item count, default 10, max 50" default(10) minimum(2) maximum(50)
162// @Success 200 {object} []models.Job
163// @Failure 400
164// @Failure 404
165// @Failure 500
166// @Router /jobs [get]
167func (h *Handlers) GetJobs(ctx *gin.Context) {
168var query types.GetJobsQuery
169if err := ctx.ShouldBindQuery(&query); err != nil {
170ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
171return
172}
173
174h.setPaginationDefault(&query.Page, &query.PerPage)
175jobs, count, err := h.service.GetJobs(ctx.Request.Context(), query)
176if err != nil {
177ctx.Error(err) // nolint: errcheck
178return
179}
180
181h.setPaginationLinkHeader(ctx, query.Page, query.PerPage, int(count))
182ctx.JSON(http.StatusOK, jobs)
183}
184