parent
8f8e84ef94
commit
764eab9271
@ -1,30 +1,170 @@
|
|||||||
package tag
|
package tag
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"cmsDemo/models"
|
||||||
|
"cmsDemo/utils"
|
||||||
|
"fmt"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Post(ctx *gin.Context) {
|
func Post(ctx *gin.Context) {
|
||||||
|
// 1.解析请求主体数据
|
||||||
|
tag := models.Tag{}
|
||||||
|
if err := ctx.ShouldBind(&tag); err != nil {
|
||||||
|
log.Println(err)
|
||||||
ctx.JSON(http.StatusOK, gin.H{
|
ctx.JSON(http.StatusOK, gin.H{
|
||||||
"message": "tag post",
|
"error": 1,
|
||||||
|
"message": "数据绑定失败",
|
||||||
})
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2.数据入库
|
||||||
|
if err := utils.DB.Create(&tag).Error; err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
ctx.JSON(http.StatusOK, gin.H{
|
||||||
|
"error": 2,
|
||||||
|
"message": "tag数据入库失败",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3.响应
|
||||||
|
ctx.JSON(http.StatusOK, gin.H{
|
||||||
|
"error": 0,
|
||||||
|
"data": tag,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
type DeleteReq struct {
|
||||||
|
IDs []uint `form:"ids"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func Delete(ctx *gin.Context) {
|
func Delete(ctx *gin.Context) {
|
||||||
|
// 1. 解析id值
|
||||||
|
req := DeleteReq{}
|
||||||
|
if err := ctx.ShouldBindQuery(&req); err != nil {
|
||||||
|
log.Println(err)
|
||||||
ctx.JSON(http.StatusOK, gin.H{
|
ctx.JSON(http.StatusOK, gin.H{
|
||||||
"message": "tag delete",
|
"error": 1,
|
||||||
|
"message": "数据绑定失败",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 数据库删除
|
||||||
|
result := utils.DB.Delete(&models.Tag{}, req.IDs)
|
||||||
|
if err := result.Error; err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
ctx.JSON(http.StatusOK, gin.H{
|
||||||
|
"error": 2,
|
||||||
|
"message": "数据删除失败",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3.响应
|
||||||
|
ctx.JSON(http.StatusOK, gin.H{
|
||||||
|
"error": 0,
|
||||||
|
"message": fmt.Sprintf("删除了%d条", result.RowsAffected),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func Put(ctx *gin.Context) {
|
func Put(ctx *gin.Context) {
|
||||||
|
// 1.解析请求主体数据
|
||||||
|
tag := models.Tag{}
|
||||||
|
if err := ctx.ShouldBind(&tag); err != nil {
|
||||||
|
log.Println(err)
|
||||||
ctx.JSON(http.StatusOK, gin.H{
|
ctx.JSON(http.StatusOK, gin.H{
|
||||||
"message": "tag put",
|
"error": 1,
|
||||||
|
"message": "数据绑定失败",
|
||||||
})
|
})
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 2.数据入库
|
||||||
|
if err := utils.DB.Debug().Updates(&tag).Error; err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
ctx.JSON(http.StatusOK, gin.H{
|
||||||
|
"error": 2,
|
||||||
|
"message": "tag数据入库失败",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3.响应
|
||||||
|
utils.DB.First(&tag, tag.ID)
|
||||||
|
ctx.JSON(http.StatusOK, gin.H{
|
||||||
|
"error": 0,
|
||||||
|
"data": tag,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetReq struct {
|
||||||
|
ID uint `form:"id"`
|
||||||
|
Keyword string `form:"keyword"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//type GetResp struct {
|
||||||
|
// Error int `json:"error"`
|
||||||
|
// Message string `json:"message"`
|
||||||
|
// Data any `json:"data"`
|
||||||
|
//}
|
||||||
|
|
||||||
func Get(ctx *gin.Context) {
|
func Get(ctx *gin.Context) {
|
||||||
|
// 1. 解析id值
|
||||||
|
req := GetReq{}
|
||||||
|
if err := ctx.ShouldBindQuery(&req); err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
ctx.JSON(http.StatusOK, gin.H{
|
||||||
|
"error": 1,
|
||||||
|
"message": "数据绑定失败",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 数据库查询
|
||||||
|
if req.ID != 0 { // 查询单条
|
||||||
|
tag := models.Tag{}
|
||||||
|
if err := utils.DB.First(&tag, req.ID).Error; err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
ctx.JSON(http.StatusOK, gin.H{
|
||||||
|
"error": 2,
|
||||||
|
"message": "数据查询失败",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3.响应
|
||||||
ctx.JSON(http.StatusOK, gin.H{
|
ctx.JSON(http.StatusOK, gin.H{
|
||||||
"message": "tag get",
|
"data": tag,
|
||||||
})
|
})
|
||||||
|
} else { // 查询列表
|
||||||
|
query := utils.DB.Model(&models.Tag{})
|
||||||
|
// filter
|
||||||
|
if req.Keyword != "" {
|
||||||
|
query.Where("title LIKE ?", "%"+req.Keyword+"%")
|
||||||
|
}
|
||||||
|
// pager
|
||||||
|
// order
|
||||||
|
// 还应该有翻页,排序等
|
||||||
|
|
||||||
|
// 执行查询
|
||||||
|
var tags []models.Tag
|
||||||
|
if err := query.Find(&tags).Error; err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
ctx.JSON(http.StatusOK, gin.H{
|
||||||
|
"error": 2,
|
||||||
|
"message": "数据查询失败",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// 3.响应
|
||||||
|
ctx.JSON(http.StatusOK, gin.H{
|
||||||
|
"error": 0,
|
||||||
|
"data": tags,
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in new issue