You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
171 lines
3.1 KiB
171 lines
3.1 KiB
package tag
|
|
|
|
import (
|
|
"cmsDemo/models"
|
|
"cmsDemo/utils"
|
|
"fmt"
|
|
"github.com/gin-gonic/gin"
|
|
"log"
|
|
"net/http"
|
|
)
|
|
|
|
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{
|
|
"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) {
|
|
// 1. 解析id值
|
|
req := DeleteReq{}
|
|
if err := ctx.ShouldBindQuery(&req); err != nil {
|
|
log.Println(err)
|
|
ctx.JSON(http.StatusOK, gin.H{
|
|
"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) {
|
|
// 1.解析请求主体数据
|
|
tag := models.Tag{}
|
|
if err := ctx.ShouldBind(&tag); err != nil {
|
|
log.Println(err)
|
|
ctx.JSON(http.StatusOK, gin.H{
|
|
"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) {
|
|
// 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{
|
|
"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,
|
|
})
|
|
}
|
|
}
|