Dragonfly2
137 строк · 3.5 Кб
1/*
2* Copyright 2022 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 handlers18
19import (20"net/http"21
22"github.com/gin-gonic/gin"23
24// nolint25_ "d7y.io/dragonfly/v2/manager/models"26"d7y.io/dragonfly/v2/manager/types"27)
28
29// @Summary Create Peer
30// @Description Create by json config
31// @Tags Peer
32// @Accept json
33// @Produce json
34// @Param Peer body types.CreatePeerRequest true "Peer"
35// @Success 200 {object} models.Peer
36// @Failure 400
37// @Failure 404
38// @Failure 500
39// @Router /peers [post]
40func (h *Handlers) CreatePeer(ctx *gin.Context) {41var json types.CreatePeerRequest42if err := ctx.ShouldBindJSON(&json); err != nil {43ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})44return45}46
47peer, err := h.service.CreatePeer(ctx.Request.Context(), json)48if err != nil {49ctx.Error(err) // nolint: errcheck50return51}52
53ctx.JSON(http.StatusOK, peer)54}
55
56// @Summary Destroy Peer
57// @Description Destroy by id
58// @Tags Peer
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 /peers/{id} [delete]
67func (h *Handlers) DestroyPeer(ctx *gin.Context) {68var params types.PeerParams69if err := ctx.ShouldBindUri(¶ms); err != nil {70ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})71return72}73
74if err := h.service.DestroyPeer(ctx.Request.Context(), params.ID); err != nil {75ctx.Error(err) // nolint: errcheck76return77}78
79ctx.Status(http.StatusOK)80}
81
82// @Summary Get Peer
83// @Description Get Peer by id
84// @Tags Peer
85// @Accept json
86// @Produce json
87// @Param id path string true "id"
88// @Success 200 {object} models.Peer
89// @Failure 400
90// @Failure 404
91// @Failure 500
92// @Router /peers/{id} [get]
93func (h *Handlers) GetPeer(ctx *gin.Context) {94var params types.PeerParams95if err := ctx.ShouldBindUri(¶ms); err != nil {96ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})97return98}99
100peer, err := h.service.GetPeer(ctx.Request.Context(), params.ID)101if err != nil {102ctx.Error(err) // nolint: errcheck103return104}105
106ctx.JSON(http.StatusOK, peer)107}
108
109// @Summary Get Peers
110// @Description Get Peers
111// @Tags Peer
112// @Accept json
113// @Produce json
114// @Param page query int true "current page" default(0)
115// @Param per_page query int true "return max item count, default 10, max 50" default(10) minimum(2) maximum(50)
116// @Success 200 {object} []models.Peer
117// @Failure 400
118// @Failure 404
119// @Failure 500
120// @Router /peers [get]
121func (h *Handlers) GetPeers(ctx *gin.Context) {122var query types.GetPeersQuery123if err := ctx.ShouldBindQuery(&query); err != nil {124ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})125return126}127
128h.setPaginationDefault(&query.Page, &query.PerPage)129peers, count, err := h.service.GetPeers(ctx.Request.Context(), query)130if err != nil {131ctx.Error(err) // nolint: errcheck132return133}134
135h.setPaginationLinkHeader(ctx, query.Page, query.PerPage, int(count))136ctx.JSON(http.StatusOK, peers)137}
138