getAllSelection.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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/model/http_model"
  9. "youngee_b_api/pack"
  10. "youngee_b_api/service/selection_service"
  11. "youngee_b_api/util"
  12. )
  13. func WrapGetAllSelectionHandler(ctx *gin.Context) {
  14. handler := newGetAllSelectionHandler(ctx)
  15. baseRun(handler)
  16. }
  17. type AllSelectionHandler struct {
  18. ctx *gin.Context
  19. req *http_model.GetAllSelectionRequest
  20. resp *http_model.CommonResponse
  21. }
  22. func (a AllSelectionHandler) getContext() *gin.Context {
  23. return a.ctx
  24. }
  25. func (a AllSelectionHandler) getResponse() interface{} {
  26. return a.resp
  27. }
  28. func (a AllSelectionHandler) getRequest() interface{} {
  29. return a.req
  30. }
  31. func (a AllSelectionHandler) run() {
  32. condition := pack.GetSelectionSquareCondition(*a.req)
  33. data, err := selection_service.Selection.SelectionSquare(a.ctx, *a.req, condition)
  34. if err != nil {
  35. logrus.WithContext(a.ctx).Errorf("[FindAllSelectionHandler] error GetAllSelection, err:%+v", err)
  36. util.HandlerPackErrorResp(a.resp, consts.ErrorInternal, consts.DefaultToast)
  37. return
  38. }
  39. a.resp.Data = data
  40. a.resp.Status = 20000
  41. a.resp.Message = "ok"
  42. return
  43. }
  44. func (a AllSelectionHandler) checkParam() error {
  45. var errs []error
  46. if a.req.PageNum < 0 || a.req.PageSize <= 0 {
  47. errs = append(errs, errors.New("page param error"))
  48. }
  49. a.req.PageNum--
  50. if len(errs) != 0 {
  51. return fmt.Errorf("check param errs:%+v", errs)
  52. }
  53. return nil
  54. }
  55. func newGetAllSelectionHandler(ctx *gin.Context) *AllSelectionHandler {
  56. return &AllSelectionHandler{
  57. ctx: ctx,
  58. req: http_model.NewGetAllSelectionRequest(),
  59. resp: http_model.NewGetAllSelectionResponse(),
  60. }
  61. }