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.

211 lines
4.1 KiB

package code
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"product/backend/module/base"
"product/backend/module/directive"
"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
}
// association
var directives []directive.Model
if len(postModel.DirectiveID) > 0 {
db.DB.Find(&directives, postModel.DirectiveID)
}
if err := db.DB.Model(&model).Association("Directives").Append(&directives); err != nil {
log.Warn(err)
}
// build response
row := Model{}
if result := db.DB.First(&row, model.ID); result.Error == nil {
// association
}
ctx.JSON(http.StatusOK, gin.H{
"code": 0,
"data": model,
})
}
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
model := putModel.Model
model.ID = putModel.ID
if result := db.DB.Updates(&model); result.Error != nil {
log.Error(result.Error)
ctx.JSON(http.StatusOK, gin.H{
"code": 1,
})
return
}
// update association
var directives []directive.Model
if len(putModel.DirectiveID) > 0 {
db.DB.Find(&directives, putModel.DirectiveID)
}
if err := db.DB.Model(&model).Association("Directives").Replace(&directives); err != nil {
log.Warn(err)
}
// response
row := Model{}
if result := db.DB.First(&row, model.ID); result.Error == nil {
db.DB.Model(&row).Association("Directives").Find(&row.Directives)
}
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
}
// build response
for i := range rows {
db.DB.Model(&rows[i]).Association("Directives").Find(&rows[i].Directives)
}
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
db.DB.Model(&row).Association("Directives").Find(&row.Directives)
ctx.JSON(http.StatusOK, gin.H{
"code": 0,
"data": row,
})
}
}