-
Notifications
You must be signed in to change notification settings - Fork 261
/
model.go
125 lines (109 loc) · 2.97 KB
/
model.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package handlers
import (
"net/http"
"github.com/gin-gonic/gin"
_ "d7y.io/dragonfly/v2/manager/models" // nolint
"d7y.io/dragonfly/v2/manager/types"
)
// @Summary Destroy Model
// @Description Destroy by id
// @Tags Model
// @Accept json
// @Produce json
// @Param id path string true "id"
// @Success 200
// @Failure 400
// @Failure 404
// @Failure 500
// @Router /models/{id} [delete]
func (h *Handlers) DestroyModel(ctx *gin.Context) {
var params types.ModelParams
if err := ctx.ShouldBindUri(¶ms); err != nil {
ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
return
}
if err := h.service.DestroyModel(ctx.Request.Context(), params.ID); err != nil {
ctx.Error(err) // nolint: errcheck
return
}
ctx.Status(http.StatusOK)
}
// @Summary Update Model
// @Description Update by json config
// @Tags Model
// @Accept json
// @Produce json
// @Param id path string true "id"
// @Param Model body types.UpdateModelRequest true "Model"
// @Success 200 {object} models.Model
// @Failure 400
// @Failure 404
// @Failure 500
// @Router /models/{id} [patch]
func (h *Handlers) UpdateModel(ctx *gin.Context) {
var params types.ModelParams
if err := ctx.ShouldBindUri(¶ms); err != nil {
ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
return
}
var json types.UpdateModelRequest
if err := ctx.ShouldBindJSON(&json); err != nil {
ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
return
}
model, err := h.service.UpdateModel(ctx.Request.Context(), params.ID, json)
if err != nil {
ctx.Error(err) // nolint: errcheck
return
}
ctx.JSON(http.StatusOK, model)
}
// @Summary Get Model
// @Description Get Model by id
// @Tags Model
// @Accept json
// @Produce json
// @Param id path string true "id"
// @Success 200 {object} models.Model
// @Failure 400
// @Failure 404
// @Failure 500
// @Router /models/{id} [get]
func (h *Handlers) GetModel(ctx *gin.Context) {
var params types.ModelParams
if err := ctx.ShouldBindUri(¶ms); err != nil {
ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
return
}
model, err := h.service.GetModel(ctx.Request.Context(), params.ID)
if err != nil {
ctx.Error(err) // nolint: errcheck
return
}
ctx.JSON(http.StatusOK, model)
}
// @Summary Get Models
// @Description Get Models
// @Tags Model
// @Accept json
// @Produce json
// @Success 200 {object} []models.Model
// @Failure 400
// @Failure 404
// @Failure 500
// @Router /models [get]
func (h *Handlers) GetModels(ctx *gin.Context) {
var query types.GetModelsQuery
if err := ctx.ShouldBindQuery(&query); err != nil {
ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
return
}
h.setPaginationDefault(&query.Page, &query.PerPage)
models, count, err := h.service.GetModels(ctx.Request.Context(), query)
if err != nil {
ctx.Error(err) // nolint: errcheck
return
}
h.setPaginationLinkHeader(ctx, query.Page, query.PerPage, int(count))
ctx.JSON(http.StatusOK, models)
}