result.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package search
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. searchMdl "go-common/app/interface/main/tv/model/search"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. "github.com/pkg/errors"
  10. )
  11. // SearchAll gets the search all_tv result
  12. func (d *Dao) SearchAll(ctx context.Context, req *searchMdl.ReqSearch) (result searchMdl.RespAll, common *searchMdl.ResultResponse, err error) {
  13. params := commonParam(req)
  14. if err = d.client.Get(ctx, d.resultURL, "", params, &result); err != nil {
  15. log.Error("[result] SearchAll URL(%s) error[%v]", d.resultURL, err)
  16. return
  17. }
  18. if result.Code != ecode.OK.Code() {
  19. err = errors.Wrap(ecode.Int(result.Code), "Search API Error: "+result.Msg)
  20. log.Error("[result] SearchAll URL(%s) error[%v]", d.resultURL, err)
  21. }
  22. common = result.ResultResponse
  23. return
  24. }
  25. // SearchUgc gets the search tv_ugc result
  26. func (d *Dao) SearchUgc(ctx context.Context, req *searchMdl.ReqSearch) (result searchMdl.RespUgc, common *searchMdl.ResultResponse, err error) {
  27. // common params
  28. params := commonParam(req)
  29. params.Set("category", fmt.Sprintf("%d", req.Category))
  30. if err = d.client.Get(ctx, d.resultURL, "", params, &result); err != nil {
  31. log.Error("[result] SearchUgc URL(%s) error[%v]", d.resultURL, err)
  32. return
  33. }
  34. if result.Code != ecode.OK.Code() {
  35. err = errors.Wrap(ecode.Int(result.Code), "Search API Error: "+result.Msg)
  36. log.Error("[result] SearchUgc Code(%d) URL(%s) error[%v]", result.Code, d.resultURL, err)
  37. }
  38. common = result.ResultResponse
  39. return
  40. }
  41. // SearchPgc gets the search tv_pgc result
  42. func (d *Dao) SearchPgc(ctx context.Context, req *searchMdl.ReqSearch) (result searchMdl.RespPgc, common *searchMdl.ResultResponse, err error) {
  43. params := commonParam(req)
  44. if err = d.client.Get(ctx, d.resultURL, "", params, &result); err != nil {
  45. log.Error("[result] SearchPgc URL(%s) error[%v]", d.resultURL, err)
  46. return
  47. }
  48. if result.Code != ecode.OK.Code() {
  49. log.Error("ClientGet Code Result Not OK [%v]", result)
  50. err = errors.Wrap(ecode.Int(result.Code), "Search API Error: "+result.Msg)
  51. }
  52. common = result.ResultResponse
  53. return
  54. }
  55. func commonParam(req *searchMdl.ReqSearch) (params url.Values) {
  56. params = url.Values{}
  57. params.Set("search_type", req.SearchType)
  58. params.Set("order", req.Order)
  59. params.Set("build", req.Build)
  60. params.Set("mobi_app", req.MobiAPP)
  61. params.Set("platform", req.Platform)
  62. params.Set("device", req.Device)
  63. params.Set("keyword", req.Keyword)
  64. params.Set("page", fmt.Sprintf("%d", req.Page))
  65. if req.Pagesize != 0 {
  66. params.Set("pagesize", fmt.Sprintf("%d", req.Pagesize))
  67. }
  68. return
  69. }