package home import ( "errors" "fmt" "net/http" "strconv" "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 HoemV1Router struct { group *gin.RouterGroup mDB *gorm.DB rDB *gorm.DB } func NewHomeV1Router(r common.Router, basePath string) HoemV1Router { h := HoemV1Router{ group: r.Version.Group(basePath), mDB: r.Db.MasterDB, rDB: r.Db.ReadDB, } h.group.GET("", h.getHomeItems) h.group.GET("artist", h.getArtist) return h } // getHomeItems godoc // @Summary get index data // @Description index 페이지 데이터 // @Schemes // @Tags home // @Accept json // @Produce json // @Success 200 {object} view.TokenSet // @Router /home [get] func (h HoemV1Router) getHomeItems(c *gin.Context) { userID, _ := gauth.GetCurrentUserIDToInt64(c) err, items := view.SelectHomeItems(h.rDB, 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) } func (h HoemV1Router) getArtist(c *gin.Context) { limit, ok := c.GetQuery("limit") if !ok { limit = "10" } limited, _ := strconv.Atoi(fmt.Sprintf("%v", limit)) items := view.SelectArtistsItems(h.rDB, limited) c.JSON(http.StatusOK, items) }