func_updateused.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package cron
  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. "github.com/xinliangnote/go-gin-api/internal/pkg/validation"
  7. )
  8. type updateUsedRequest struct {
  9. Id string `form:"id"` // 主键ID
  10. Used int32 `form:"used"` // 是否启用 1:是 -1:否
  11. }
  12. type updateUsedResponse struct {
  13. Id int32 `json:"id"` // 主键ID
  14. }
  15. // UpdateUsed 更新任务为启用/禁用
  16. // @Summary 更新任务为启用/禁用
  17. // @Description 更新任务为启用/禁用
  18. // @Tags API.cron
  19. // @Accept application/x-www-form-urlencoded
  20. // @Produce json
  21. // @Param id formData string true "hashID"
  22. // @Param used formData int true "是否启用 1:是 -1:否"
  23. // @Success 200 {object} updateUsedResponse
  24. // @Failure 400 {object} code.Failure
  25. // @Router /api/cron/used [patch]
  26. // @Security LoginToken
  27. func (h *handler) UpdateUsed() core.HandlerFunc {
  28. return func(ctx core.Context) {
  29. req := new(updateUsedRequest)
  30. res := new(updateUsedResponse)
  31. if err := ctx.ShouldBindForm(req); err != nil {
  32. ctx.AbortWithError(core.Error(
  33. http.StatusBadRequest,
  34. code.ParamBindError,
  35. validation.Error(err)).WithError(err),
  36. )
  37. return
  38. }
  39. ids, err := h.hashids.HashidsDecode(req.Id)
  40. if err != nil {
  41. ctx.AbortWithError(core.Error(
  42. http.StatusBadRequest,
  43. code.HashIdsDecodeError,
  44. code.Text(code.HashIdsDecodeError)).WithError(err),
  45. )
  46. return
  47. }
  48. id := int32(ids[0])
  49. err = h.cronService.UpdateUsed(ctx, id, req.Used)
  50. if err != nil {
  51. ctx.AbortWithError(core.Error(
  52. http.StatusBadRequest,
  53. code.AdminUpdateError,
  54. code.Text(code.AdminUpdateError)).WithError(err),
  55. )
  56. return
  57. }
  58. res.Id = id
  59. ctx.Payload(res)
  60. }
  61. }