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.

187 lines
3.3 KiB

package demo
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"product/backend/module/base"
"product/backend/moo/db"
"product/backend/moo/log"
)
func Post(ctx *gin.Context) {
// bind request data
postModel := &PostModel{}
if err := ctx.ShouldBind(&postModel); err != nil {
log.Error(err)
ctx.JSON(http.StatusOK, gin.H{
"code": 1,
"errors": base.Translate(err),
})
return
}
// insert
model := postModel.Model
if result := db.DB.Create(&model); result.Error != nil {
log.Error(result.Error)
ctx.JSON(http.StatusOK, gin.H{
"code": 1,
})
return
}
// build response
row := Model{}
if err := db.DB.First(&row, model.ID).Error; err != nil {
// association
}
ctx.JSON(http.StatusOK, gin.H{
"code": 0,
"data": row,
})
}
func Put(ctx *gin.Context) {
// bind request data
putModel := &PutModel{}
if err := ctx.ShouldBind(&putModel); err != nil {
log.Error(err)
ctx.JSON(http.StatusOK, gin.H{
"code": 1,
"errors": base.Translate(err),
})
return
}
// exists
if err := db.DB.First(&Model{}, putModel.ID).Error; err != nil {
log.Error(err)
ctx.JSON(http.StatusOK, gin.H{
"code": 1,
"errors": gin.H{
"id": "对应的记录不存在",
},
})
return
}
// update
model := putModel.Model
if result := db.DB.Updates(&model); result.Error != nil {
log.Error(result.Error)
ctx.JSON(http.StatusOK, gin.H{
"code": 1,
})
return
}
// build response
row := Model{}
if err := db.DB.First(&row, model.ID).Error; err != nil {
// association
}
ctx.JSON(http.StatusOK, gin.H{
"code": 0,
"data": row,
})
}
func Delete(ctx *gin.Context) {
id := ctx.QueryArray("id")
if result := db.DB.Delete(&Model{}, id); result.Error != nil {
log.Error(result.Error)
ctx.JSON(http.StatusOK, gin.H{
"code": 1,
})
return
}
// response
ctx.JSON(http.StatusOK, gin.H{
"code": 0,
})
}
func Get(ctx *gin.Context) {
// get request param
req := &GetReq{}
if err := req.Init(ctx); err != nil {
log.Error(err)
ctx.JSON(http.StatusOK, gin.H{
"code": 1,
})
return
}
// init query
query := db.DB.Model(&Model{})
if req.ID == 0 { // list
// filter
// keyword
if req.Keyword != "" {
query.Where("title LIKE ?", req.Keyword+"%")
}
// total
var total int64
query.Count(&total)
// order
query.Order(fmt.Sprintf("%s %s", req.SortField, req.SortOrder))
// # offset limit
offset := -1 // 不限制
if req.PageSize > 0 {
offset = (req.PageIndex - 1) * req.PageSize
}
query.Offset(offset).Limit(req.PageSize)
// select
query.Select("*")
// fetch
var rows []*Model
if result := query.Find(&rows); result.Error != nil {
log.Error(result.Error)
ctx.JSON(http.StatusOK, gin.H{
"code": 1,
})
return
}
// response
ctx.JSON(http.StatusOK, gin.H{
"code": 0,
"data": gin.H{
"list": rows,
"total": total,
"keyword": req.Keyword,
"pageIndex": req.PageIndex,
"pageSize": req.PageSize,
"sortField": req.SortField,
"sortOrder": req.SortOrder,
},
})
} else { // one
// fetch
var row *Model
if result := query.First(&row, req.ID); result.Error != nil {
log.Error(result.Error)
ctx.JSON(http.StatusOK, gin.H{
"code": 1,
})
return
}
// response
ctx.JSON(http.StatusOK, gin.H{
"code": 0,
"data": row,
})
}
}