package explore import ( "errors" "net/http" "github.com/gin-gonic/gin" "github.com/metarare/metarare_api/common" "github.com/metarare/metarare_api/helpers/gauth" "github.com/metarare/metarare_api/view" "gorm.io/gorm" ) type ExploreV1Router struct { group *gin.RouterGroup mDB *gorm.DB rDB *gorm.DB } func NewExploreV1Router(r common.Router, basePath string) ExploreV1Router { e := ExploreV1Router{ group: r.Version.Group(basePath), mDB: r.Db.MasterDB, rDB: r.Db.ReadDB, } e.group.POST("home", e.getExploreItems) e.group.POST("search", e.getExploreItemsByName) e.group.POST("keyword", e.getCollectionFilterList) // 필터링중 컬렉션 이름으로 키워드 검색 할 때 사용하며, filter.collectionName이 빈 값인 경우 tvl 기준 limit 10개 리턴 e.group.POST("hint", e.getHintExploreQuery) return e } func (e ExploreV1Router) extractFilterData(c *gin.Context) (error, common.Filter) { var _filter common.Filter if err := c.ShouldBindJSON(&_filter); err != nil { return err, common.Filter{} } else { return nil, _filter } } func (e ExploreV1Router) getExploreItemsByName(c *gin.Context) { err, _filter := e.extractFilterData(c) if err != nil { c.AbortWithStatusJSON(http.StatusBadRequest, _filter) return } userID, _ := gauth.GetCurrentUserIDToInt64(c) err, items := view.SelectExploreItemsByTokenName(e.rDB, _filter, userID) if errors.Is(err, gorm.ErrRecordNotFound) { c.AbortWithStatusJSON(http.StatusOK, gin.H{ "message": "There is no matched items(explore)", }) return } c.JSON(http.StatusOK, items) } // extractFilterData godoc // @Summary explore filter data // @Description 탐색하기 home data // @Schemes // @Tags explore // @Accept json // @Produce json // @Param common.Filter body common.Filter true "filter object" // @Success 200 {object} []common.ExpItem // @Router /explore/home [post] func (e ExploreV1Router) getExploreItems(c *gin.Context) { userID, _ := gauth.GetCurrentUserIDToInt64(c) err, _filter := e.extractFilterData(c) err, items := view.SelectExploreItems(e.rDB, _filter, userID) if errors.Is(err, gorm.ErrRecordNotFound) { c.AbortWithStatusJSON(http.StatusOK, gin.H{ "message": "There is no matched items(explore)", }) return } c.JSON(http.StatusOK, items) } // getCollectionFilterList godoc // @Summary explore collection filter list // @Description 필터링중 컬렉션 이름으로 키워드 검색 할 때 사용하며, filter.collectionName이 빈 값인 경우 tvl 기준 limit 10개 리턴 // @Schemes // @Tags explore // @Accept json // @Produce json // @Param common.Filter body common.Filter true "filter object" // @Success 200 {obejct} items // @Router /explore/search [post] func (e ExploreV1Router) getCollectionFilterList(c *gin.Context) { _, _filter := e.extractFilterData(c) err, items := view.SelectCollectionListByKeyword(e.rDB, _filter) if errors.Is(err, gorm.ErrRecordNotFound) { c.AbortWithStatusJSON(http.StatusOK, gin.H{ "message": "There is no matched items(explore)", }) return } c.JSON(http.StatusOK, items) } func (e ExploreV1Router) getHintExploreQuery(c *gin.Context) { _, _filter := e.extractFilterData(c) err, items := view.HintQueryByToken(e.rDB, _filter) if errors.Is(err, gorm.ErrRecordNotFound) { c.AbortWithStatusJSON(http.StatusOK, gin.H{ "message": "There is no matched items(explore)", }) return } c.JSON(http.StatusOK, items) }