qinglong-go/api/dependencies/dependencies.go

76 lines
1.4 KiB
Go
Raw Normal View History

2022-11-20 14:11:47 +00:00
package dependencies
import (
"github.com/gin-gonic/gin"
"github.com/huoxue1/qinglong-go/models"
"github.com/huoxue1/qinglong-go/service/dependencies"
"github.com/huoxue1/qinglong-go/utils/res"
"strconv"
)
func Api(group *gin.RouterGroup) {
group.POST("", post())
group.GET("", get())
group.GET("/:id", getDep())
group.DELETE("", del())
2022-11-20 14:11:47 +00:00
}
2023-02-03 04:43:37 +00:00
var (
typMap = map[string]int{
"nodejs": 0,
"python3": 1,
"linux": 2,
}
)
2022-11-20 14:11:47 +00:00
func get() gin.HandlerFunc {
return func(ctx *gin.Context) {
2023-02-03 04:43:37 +00:00
dependences, err := models.QueryDependences(ctx.Query("searchValue"), typMap[ctx.Query("type")])
2022-11-20 14:11:47 +00:00
if err != nil {
2023-01-10 12:43:28 +00:00
ctx.JSON(503, res.Err(503, err))
2022-11-20 14:11:47 +00:00
return
}
ctx.JSON(200, res.Ok(dependences))
}
}
func del() gin.HandlerFunc {
return func(ctx *gin.Context) {
var ids []int
err := ctx.ShouldBindJSON(&ids)
if err != nil {
ctx.JSON(503, res.Err(502, err))
return
}
dependencies.DelDep(ids)
ctx.JSON(200, res.Ok(true))
}
}
2022-11-20 14:11:47 +00:00
func post() gin.HandlerFunc {
return func(ctx *gin.Context) {
var deps []*models.Dependences
err := ctx.ShouldBindJSON(&deps)
if err != nil {
2023-01-10 12:43:28 +00:00
ctx.JSON(503, res.Err(503, err))
2022-11-20 14:11:47 +00:00
return
}
for _, dep := range deps {
dependencies.AddDep(dep)
}
ctx.JSON(200, res.Ok(deps))
}
}
func getDep() gin.HandlerFunc {
return func(ctx *gin.Context) {
id, _ := strconv.Atoi(ctx.Param("id"))
dep, _ := models.GetDependences(id)
ctx.JSON(200, res.Ok(dep))
}
}