selection_find_all.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package handler
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/gin-gonic/gin"
  6. "github.com/sirupsen/logrus"
  7. "youngee_m_api/consts"
  8. "youngee_m_api/model/http_model"
  9. "youngee_m_api/pack"
  10. "youngee_m_api/service"
  11. "youngee_m_api/util"
  12. )
  13. func WrapFindAllSelectionHandler(ctx *gin.Context) {
  14. handler := newFindAllSelection(ctx)
  15. BaseRun(handler)
  16. }
  17. type FindAllSelectionHandler struct {
  18. ctx *gin.Context
  19. req *http_model.FindAllSelectionRequest
  20. resp *http_model.CommonResponse
  21. }
  22. func (f FindAllSelectionHandler) getContext() *gin.Context {
  23. return f.ctx
  24. }
  25. func (f FindAllSelectionHandler) getResponse() interface{} {
  26. return f.resp
  27. }
  28. func (f FindAllSelectionHandler) getRequest() interface{} {
  29. return f.req
  30. }
  31. func (f FindAllSelectionHandler) run() {
  32. condition := pack.HttpFindAllSelectionRequestToCondition(f.req)
  33. data, err := service.Selection.GetAllSelection(f.ctx, f.req.EnterpriseId, f.req.PageSize, f.req.PageNum, condition)
  34. if err != nil {
  35. logrus.WithContext(f.ctx).Errorf("[FindAllSelectionHandler] error GetAllSelection, err:%+v", err)
  36. util.HandlerPackErrorResp(f.resp, consts.ErrorInternal, consts.DefaultToast)
  37. return
  38. }
  39. f.resp.Data = data
  40. }
  41. func (f FindAllSelectionHandler) checkParam() error {
  42. var errs []error
  43. if f.req.PageNum < 0 || f.req.PageSize <= 0 {
  44. errs = append(errs, errors.New("page param error"))
  45. }
  46. f.req.PageNum--
  47. if len(errs) != 0 {
  48. return fmt.Errorf("check param errs:%+v", errs)
  49. }
  50. return nil
  51. }
  52. func newFindAllSelection(ctx *gin.Context) *FindAllSelectionHandler {
  53. return &FindAllSelectionHandler{
  54. ctx: ctx,
  55. req: http_model.NewFindAllSelectionRequest(),
  56. resp: http_model.NewFindAllSelectionResponse(),
  57. }
  58. }