-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathorder.go
48 lines (36 loc) · 953 Bytes
/
order.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
package cosy
import (
"net/http"
"github.com/uozi-tech/cosy/model"
"gorm.io/gorm"
)
func (c *Ctx[T]) UpdateOrder() {
var json struct {
TargetID interface{} `json:"target_id"`
Direction int `json:"direction" binding:"oneof=-1 1"`
AffectedIDs []interface{} `json:"affected_ids"`
}
if !BindAndValid(c.Context, &json) {
return
}
affectedLen := len(json.AffectedIDs)
db := model.UseDB()
if c.table != "" {
db = db.Table(c.table, c.tableArgs...)
}
// update target
err := db.Model(&c.Model).Where("id = ?", json.TargetID).
Update("order_id", gorm.Expr("order_id + ?", affectedLen*(-json.Direction))).Error
if err != nil {
errHandler(c.Context, err)
return
}
// update affected
err = db.Model(&c.Model).Where("id in ?", json.AffectedIDs).
Update("order_id", gorm.Expr("order_id + ?", json.Direction)).Error
if err != nil {
errHandler(c.Context, err)
return
}
c.JSON(http.StatusOK, json)
}