selection_find_all.go 1.7 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/pack"
  11. "youngee_b_api/service"
  12. "youngee_b_api/util"
  13. )
  14. func WrapFindAllSelectionHandler(ctx *gin.Context) {
  15. handler := newFindAllSelection(ctx)
  16. baseRun(handler)
  17. }
  18. type FindAllSelectionHandler struct {
  19. ctx *gin.Context
  20. req *http_model.FindAllSelectionRequest
  21. resp *http_model.CommonResponse
  22. }
  23. func (f FindAllSelectionHandler) getContext() *gin.Context {
  24. return f.ctx
  25. }
  26. func (f FindAllSelectionHandler) getResponse() interface{} {
  27. return f.resp
  28. }
  29. func (f FindAllSelectionHandler) getRequest() interface{} {
  30. return f.req
  31. }
  32. func (f FindAllSelectionHandler) run() {
  33. enterpriseID := middleware.GetSessionAuth(f.ctx).EnterpriseID
  34. condition := pack.HttpFindAllSelectionRequestToCondition(f.req)
  35. data, err := service.Selection.GetAllSelection(f.ctx, enterpriseID, f.req.PageSize, f.req.PageNum, condition)
  36. if err != nil {
  37. logrus.WithContext(f.ctx).Errorf("[FindAllSelectionHandler] error GetAllSelection, err:%+v", err)
  38. util.HandlerPackErrorResp(f.resp, consts.ErrorInternal, consts.DefaultToast)
  39. return
  40. }
  41. f.resp.Data = data
  42. }
  43. func (f FindAllSelectionHandler) checkParam() error {
  44. var errs []error
  45. if f.req.PageNum < 0 || f.req.PageSize <= 0 {
  46. errs = append(errs, errors.New("page param error"))
  47. }
  48. f.req.PageNum--
  49. if len(errs) != 0 {
  50. return fmt.Errorf("check param errs:%+v", errs)
  51. }
  52. return nil
  53. }
  54. func newFindAllSelection(ctx *gin.Context) *FindAllSelectionHandler {
  55. return &FindAllSelectionHandler{
  56. ctx: ctx,
  57. req: http_model.NewFindAllSelectionRequest(),
  58. resp: http_model.NewFindAllSelectionResponse(),
  59. }
  60. }