func_create.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package authorized
  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/services/authorized"
  7. )
  8. type createRequest struct {
  9. BusinessKey string `form:"business_key"` // 调用方key
  10. BusinessDeveloper string `form:"business_developer"` // 调用方对接人
  11. Remark string `form:"remark"` // 备注
  12. }
  13. type createResponse struct {
  14. Id int32 `json:"id"` // 主键ID
  15. }
  16. // Create 新增调用方
  17. // @Summary 新增调用方
  18. // @Description 新增调用方
  19. // @Tags API.authorized
  20. // @Accept application/x-www-form-urlencoded
  21. // @Produce json
  22. // @Param business_key formData string true "调用方key"
  23. // @Param business_developer formData string true "调用方对接人"
  24. // @Param remark formData string true "备注"
  25. // @Success 200 {object} createResponse
  26. // @Failure 400 {object} code.Failure
  27. // @Router /api/authorized [post]
  28. // @Security LoginToken
  29. func (h *handler) Create() core.HandlerFunc {
  30. return func(c core.Context) {
  31. req := new(createRequest)
  32. res := new(createResponse)
  33. if err := c.ShouldBindForm(req); err != nil {
  34. c.AbortWithError(core.Error(
  35. http.StatusBadRequest,
  36. code.ParamBindError,
  37. code.Text(code.ParamBindError)).WithError(err),
  38. )
  39. return
  40. }
  41. createData := new(authorized.CreateAuthorizedData)
  42. createData.BusinessKey = req.BusinessKey
  43. createData.BusinessDeveloper = req.BusinessDeveloper
  44. createData.Remark = req.Remark
  45. id, err := h.authorizedService.Create(c, createData)
  46. if err != nil {
  47. c.AbortWithError(core.Error(
  48. http.StatusBadRequest,
  49. code.AuthorizedCreateError,
  50. code.Text(code.AuthorizedCreateError)).WithError(err),
  51. )
  52. return
  53. }
  54. res.Id = id
  55. c.Payload(res)
  56. }
  57. }