func_updatesort.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package menu
  2. import (
  3. "net/http"
  4. "github.com/xinliangnote/go-gin-api/internal/code"
  5. "github.com/xinliangnote/go-gin-api/internal/pkg/core"
  6. )
  7. type updateSortRequest struct {
  8. Id string `form:"id"` // HashId
  9. Sort int32 `form:"sort"` // 排序
  10. }
  11. type updateSortResponse struct {
  12. Id int32 `json:"id"` // 主键ID
  13. }
  14. // UpdateSort 更新菜单排序
  15. // @Summary 更新菜单排序
  16. // @Description 更新菜单排序
  17. // @Tags API.menu
  18. // @Accept application/x-www-form-urlencoded
  19. // @Produce json
  20. // @Param id formData string true "hashId"
  21. // @Param sort formData int true "排序"
  22. // @Success 200 {object} updateSortResponse
  23. // @Failure 400 {object} code.Failure
  24. // @Router /api/menu/sort [patch]
  25. // @Security LoginToken
  26. func (h *handler) UpdateSort() core.HandlerFunc {
  27. return func(c core.Context) {
  28. req := new(updateSortRequest)
  29. res := new(updateSortResponse)
  30. if err := c.ShouldBindForm(req); err != nil {
  31. c.AbortWithError(core.Error(
  32. http.StatusBadRequest,
  33. code.ParamBindError,
  34. code.Text(code.ParamBindError)).WithError(err),
  35. )
  36. return
  37. }
  38. ids, err := h.hashids.HashidsDecode(req.Id)
  39. if err != nil {
  40. c.AbortWithError(core.Error(
  41. http.StatusBadRequest,
  42. code.HashIdsDecodeError,
  43. code.Text(code.HashIdsDecodeError)).WithError(err),
  44. )
  45. return
  46. }
  47. id := int32(ids[0])
  48. err = h.menuService.UpdateSort(c, id, req.Sort)
  49. if err != nil {
  50. c.AbortWithError(core.Error(
  51. http.StatusBadRequest,
  52. code.MenuUpdateError,
  53. code.Text(code.MenuUpdateError)).WithError(err),
  54. )
  55. return
  56. }
  57. res.Id = id
  58. c.Payload(res)
  59. }
  60. }