/
online.go
86 lines (74 loc) · 2.3 KB
/
online.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package controllers
import (
"net/http"
"timingniao_wlx_api/libraries/models"
"github.com/gatecloud/utils"
"github.com/gin-gonic/gin"
)
type OnlineController struct {
Controller
}
func (ctrl *OnlineController) Patch(ctx *gin.Context) {
var (
entities []models.Online
tableModel interface{}
)
clientProfile, err := ctrl.GetClientProfile(ctx)
if err != nil {
utils.ErrRespWithJSON(ctx, http.StatusUnauthorized, err.Error())
return
}
if err := ctx.BindJSON(&entities); err != nil {
utils.ErrRespWithJSON(ctx, http.StatusBadRequest, err.Error())
return
}
tx := ctrl.DB.Begin()
for i, bindEntity := range entities {
if err := ctrl.Validator.Struct(bindEntity); err != nil {
tx.Rollback()
utils.ErrRespWithJSON(ctx, http.StatusBadRequest, err.Error())
return
}
switch bindEntity.Type {
// case types.OnlineProduct:
// if !ctrl.RolePermit(clientProfile) {
// tx.Rollback()
// utils.ErrRespWithJSON(ctx, http.StatusForbidden, "permission forbidden")
// return
// }
// tableModel = &models.Product{}
// var chkEntity models.Product
// if tx.Where("id = ?", bindEntity.ID).Find(&chkEntity).RecordNotFound() {
// utils.ErrRespWithJSON(ctx, http.StatusBadRequest, "Product not found")
// return
// }
// t := tx.NewScope(&models.Product{}).TableName()
// if err := tx.Exec("UPDATE "+t+" SET online = ? WHERE id = ?",
// bindEntity.Online, bindEntity.ID).Error; err != nil {
// utils.ErrRespWithJSON(ctx, http.StatusInternalServerError, err.Error())
// return
// }
// case types.OnlineInventory:
// tableModel = &models.ProductInventory{}
// var chkEntity models.ProductInventory
// if tx.Where("id = ?", bindEntity.ID).Find(&chkEntity).RecordNotFound() {
// utils.ErrRespWithJSON(ctx, http.StatusBadRequest, "Inventory Category not found")
// return
// }
default:
utils.ErrRespWithJSON(ctx, http.StatusBadRequest, "The current type is not supported")
return
}
tableName := tx.NewScope(tableModel).TableName()
if err := tx.Exec("UPDATE "+tableName+" SET online=? WHERE id = ?",
bindEntity.Online, bindEntity.ID).Error; err != nil {
utils.ErrRespWithJSON(ctx, http.StatusInternalServerError, err.Error())
return
}
entities[i] = bindEntity
}
tx.Commit()
ctx.JSON(http.StatusOK, entities)
ctx.Abort()
return
}