product_find.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package handler
  2. import (
  3. "youngee_b_api/consts"
  4. "youngee_b_api/model/http_model"
  5. "youngee_b_api/service"
  6. "youngee_b_api/util"
  7. "github.com/gin-gonic/gin"
  8. "github.com/sirupsen/logrus"
  9. log "github.com/sirupsen/logrus"
  10. )
  11. func WrapFindProductHandler(ctx *gin.Context) {
  12. handler := newFindProductHandler(ctx)
  13. baseRun(handler)
  14. }
  15. func newFindProductHandler(ctx *gin.Context) *FindProductHandler {
  16. return &FindProductHandler{
  17. req: http_model.NewFindProductRequest(),
  18. resp: http_model.NewFindProductResponse(),
  19. ctx: ctx,
  20. }
  21. }
  22. type FindProductHandler struct {
  23. req *http_model.FindProductRequest
  24. resp *http_model.CommonResponse
  25. ctx *gin.Context
  26. }
  27. func (h *FindProductHandler) getRequest() interface{} {
  28. return h.req
  29. }
  30. func (h *FindProductHandler) getContext() *gin.Context {
  31. return h.ctx
  32. }
  33. func (h *FindProductHandler) getResponse() interface{} {
  34. return h.resp
  35. }
  36. func (h *FindProductHandler) run() {
  37. data := *&http_model.FindProductRequest{}
  38. data = *h.req
  39. res, err := service.Product.FindByID(h.ctx, data.ProductID)
  40. if err != nil {
  41. // 数据库查询失败,返回5001
  42. logrus.Errorf("[FindProductHandler] call FindByID err:%+v\n", err)
  43. util.HandlerPackErrorResp(h.resp, consts.ErrorInternal, "")
  44. log.Info("FindProduct fail,req:%+v", h.req)
  45. h.resp.Message = "err"
  46. h.resp.Status = 40000
  47. return
  48. }
  49. h.resp.Data = res
  50. h.resp.Message = "ok"
  51. h.resp.Status = 20000
  52. }
  53. func (h *FindProductHandler) checkParam() error {
  54. return nil
  55. }