getAllProduct.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package handler
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/gin-gonic/gin"
  6. "github.com/sirupsen/logrus"
  7. "youngee_b_api/consts"
  8. "youngee_b_api/middleware"
  9. "youngee_b_api/model/http_model"
  10. "youngee_b_api/service"
  11. "youngee_b_api/util"
  12. )
  13. func WrapGetAllProjectHandler(ctx *gin.Context) {
  14. handler := newGetAllProductHandler(ctx)
  15. baseRun(handler)
  16. }
  17. type GetAllProductHandler struct {
  18. ctx *gin.Context
  19. req *http_model.GetAllProductRequest
  20. resp *http_model.CommonResponse
  21. }
  22. func (g GetAllProductHandler) getContext() *gin.Context {
  23. return g.ctx
  24. }
  25. func (g GetAllProductHandler) getResponse() interface{} {
  26. return g.resp
  27. }
  28. func (g GetAllProductHandler) getRequest() interface{} {
  29. return g.req
  30. }
  31. func (g GetAllProductHandler) run() {
  32. enterpriseID := middleware.GetSessionAuth(g.ctx).EnterpriseID
  33. res, err := service.Product.GetAllProduct(g.ctx, g.req, enterpriseID)
  34. if err != nil {
  35. logrus.Errorf("[GetAllProductHandler] call GetAllProduct err:%+v\n", err)
  36. util.HandlerPackErrorResp(g.resp, consts.ErrorInternal, "")
  37. logrus.Info("GetAllProduct fail,req:%+v", g.req)
  38. return
  39. }
  40. g.resp.Message = "项目列表查询成功"
  41. g.resp.Data = res
  42. }
  43. func (g GetAllProductHandler) checkParam() error {
  44. var errs []error
  45. if g.req.PageNum < 0 || g.req.PageSize <= 0 {
  46. errs = append(errs, errors.New("page param error"))
  47. }
  48. g.req.PageNum--
  49. if len(errs) != 0 {
  50. return fmt.Errorf("check param errs:%+v", errs)
  51. }
  52. return nil
  53. }
  54. func newGetAllProductHandler(ctx *gin.Context) *GetAllProductHandler {
  55. return &GetAllProductHandler{
  56. ctx: ctx,
  57. req: http_model.NewGetAllProductRequest(),
  58. resp: http_model.NewGetAllProductResponse(),
  59. }
  60. }