func_delete.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 deleteRequest struct {
  8. Id string `uri:"id"` // HashID
  9. }
  10. type deleteResponse struct {
  11. Id int32 `json:"id"` // 主键ID
  12. }
  13. // Delete 删除菜单
  14. // @Summary 删除菜单
  15. // @Description 删除菜单
  16. // @Tags API.menu
  17. // @Accept json
  18. // @Produce json
  19. // @Param id path string true "hashId"
  20. // @Success 200 {object} deleteResponse
  21. // @Failure 400 {object} code.Failure
  22. // @Router /api/menu/{id} [delete]
  23. // @Security LoginToken
  24. func (h *handler) Delete() core.HandlerFunc {
  25. return func(c core.Context) {
  26. req := new(deleteRequest)
  27. res := new(deleteResponse)
  28. if err := c.ShouldBindURI(req); err != nil {
  29. c.AbortWithError(core.Error(
  30. http.StatusBadRequest,
  31. code.ParamBindError,
  32. code.Text(code.ParamBindError)).WithError(err),
  33. )
  34. return
  35. }
  36. ids, err := h.hashids.HashidsDecode(req.Id)
  37. if err != nil {
  38. c.AbortWithError(core.Error(
  39. http.StatusBadRequest,
  40. code.HashIdsDecodeError,
  41. code.Text(code.HashIdsDecodeError)).WithError(err),
  42. )
  43. return
  44. }
  45. id := int32(ids[0])
  46. err = h.menuService.Delete(c, id)
  47. if err != nil {
  48. c.AbortWithError(core.Error(
  49. http.StatusBadRequest,
  50. code.MenuDeleteError,
  51. code.Text(code.MenuDeleteError)).WithError(err),
  52. )
  53. return
  54. }
  55. res.Id = id
  56. c.Payload(res)
  57. }
  58. }