explore.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package explore
  2. import (
  3. "errors"
  4. "net/http"
  5. "github.com/gin-gonic/gin"
  6. "github.com/metarare/metarare_api/common"
  7. "github.com/metarare/metarare_api/helpers/gauth"
  8. "github.com/metarare/metarare_api/view"
  9. "gorm.io/gorm"
  10. )
  11. type ExploreV1Router struct {
  12. group *gin.RouterGroup
  13. mDB *gorm.DB
  14. rDB *gorm.DB
  15. }
  16. func NewExploreV1Router(r common.Router, basePath string) ExploreV1Router {
  17. e := ExploreV1Router{
  18. group: r.Version.Group(basePath),
  19. mDB: r.Db.MasterDB,
  20. rDB: r.Db.ReadDB,
  21. }
  22. e.group.POST("home", e.getExploreItems)
  23. e.group.POST("search", e.getExploreItemsByName)
  24. e.group.POST("keyword", e.getCollectionFilterList) // 필터링중 컬렉션 이름으로 키워드 검색 할 때 사용하며, filter.collectionName이 빈 값인 경우 tvl 기준 limit 10개 리턴
  25. e.group.POST("hint", e.getHintExploreQuery)
  26. return e
  27. }
  28. func (e ExploreV1Router) extractFilterData(c *gin.Context) (error, common.Filter) {
  29. var _filter common.Filter
  30. if err := c.ShouldBindJSON(&_filter); err != nil {
  31. return err, common.Filter{}
  32. } else {
  33. return nil, _filter
  34. }
  35. }
  36. func (e ExploreV1Router) getExploreItemsByName(c *gin.Context) {
  37. err, _filter := e.extractFilterData(c)
  38. if err != nil {
  39. c.AbortWithStatusJSON(http.StatusBadRequest, _filter)
  40. return
  41. }
  42. userID, _ := gauth.GetCurrentUserIDToInt64(c)
  43. err, items := view.SelectExploreItemsByTokenName(e.rDB, _filter, userID)
  44. if errors.Is(err, gorm.ErrRecordNotFound) {
  45. c.AbortWithStatusJSON(http.StatusOK, gin.H{
  46. "message": "There is no matched items(explore)",
  47. })
  48. return
  49. }
  50. c.JSON(http.StatusOK, items)
  51. }
  52. // extractFilterData godoc
  53. // @Summary explore filter data
  54. // @Description 탐색하기 home data
  55. // @Schemes
  56. // @Tags explore
  57. // @Accept json
  58. // @Produce json
  59. // @Param common.Filter body common.Filter true "filter object"
  60. // @Success 200 {object} []common.ExpItem
  61. // @Router /explore/home [post]
  62. func (e ExploreV1Router) getExploreItems(c *gin.Context) {
  63. userID, _ := gauth.GetCurrentUserIDToInt64(c)
  64. err, _filter := e.extractFilterData(c)
  65. err, items := view.SelectExploreItems(e.rDB, _filter, userID)
  66. if errors.Is(err, gorm.ErrRecordNotFound) {
  67. c.AbortWithStatusJSON(http.StatusOK, gin.H{
  68. "message": "There is no matched items(explore)",
  69. })
  70. return
  71. }
  72. c.JSON(http.StatusOK, items)
  73. }
  74. // getCollectionFilterList godoc
  75. // @Summary explore collection filter list
  76. // @Description 필터링중 컬렉션 이름으로 키워드 검색 할 때 사용하며, filter.collectionName이 빈 값인 경우 tvl 기준 limit 10개 리턴
  77. // @Schemes
  78. // @Tags explore
  79. // @Accept json
  80. // @Produce json
  81. // @Param common.Filter body common.Filter true "filter object"
  82. // @Success 200 {obejct} items
  83. // @Router /explore/search [post]
  84. func (e ExploreV1Router) getCollectionFilterList(c *gin.Context) {
  85. _, _filter := e.extractFilterData(c)
  86. err, items := view.SelectCollectionListByKeyword(e.rDB, _filter)
  87. if errors.Is(err, gorm.ErrRecordNotFound) {
  88. c.AbortWithStatusJSON(http.StatusOK, gin.H{
  89. "message": "There is no matched items(explore)",
  90. })
  91. return
  92. }
  93. c.JSON(http.StatusOK, items)
  94. }
  95. func (e ExploreV1Router) getHintExploreQuery(c *gin.Context) {
  96. _, _filter := e.extractFilterData(c)
  97. err, items := view.HintQueryByToken(e.rDB, _filter)
  98. if errors.Is(err, gorm.ErrRecordNotFound) {
  99. c.AbortWithStatusJSON(http.StatusOK, gin.H{
  100. "message": "There is no matched items(explore)",
  101. })
  102. return
  103. }
  104. c.JSON(http.StatusOK, items)
  105. }