module_shop_act_images_reorder.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package modules
  2. import (
  3. "context"
  4. "encoding/json"
  5. "golang-fave/engine/utils"
  6. "golang-fave/engine/wrapper"
  7. )
  8. type OrderItem struct {
  9. Id int
  10. Order int
  11. }
  12. type Orders struct {
  13. Items []OrderItem
  14. }
  15. func (this *Modules) RegisterAction_ShopImagesReorder() *Action {
  16. return this.newAction(AInfo{
  17. WantDB: true,
  18. Mount: "shop-images-reorder",
  19. WantAdmin: true,
  20. }, func(wrap *wrapper.Wrapper) {
  21. pf_data := utils.Trim(wrap.R.FormValue("data"))
  22. var orders Orders
  23. if err := json.Unmarshal([]byte(pf_data), &orders); err != nil {
  24. wrap.MsgError(err.Error())
  25. return
  26. }
  27. if len(orders.Items) > 0 {
  28. if err := wrap.DB.Transaction(wrap.R.Context(), func(ctx context.Context, tx *wrapper.Tx) error {
  29. for _, value := range orders.Items {
  30. if _, err := tx.Exec(ctx, "UPDATE fave_shop_product_images SET ord = ? WHERE id = ?;", value.Order, value.Id); err != nil {
  31. return err
  32. }
  33. }
  34. return nil
  35. }); err != nil {
  36. wrap.MsgError(err.Error())
  37. return
  38. }
  39. }
  40. wrap.RecreateProductXmlFile()
  41. wrap.ResetCacheBlocks()
  42. // Remove loading effect
  43. wrap.Write(`$('#list-images').removeClass('loading');`)
  44. })
  45. }