home.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package home
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/http"
  6. "strconv"
  7. "github.com/gin-gonic/gin"
  8. "github.com/metarare/metarare_api/common"
  9. "github.com/metarare/metarare_api/helpers/gauth"
  10. "github.com/metarare/metarare_api/view"
  11. "gorm.io/gorm"
  12. )
  13. type HoemV1Router struct {
  14. group *gin.RouterGroup
  15. mDB *gorm.DB
  16. rDB *gorm.DB
  17. }
  18. func NewHomeV1Router(r common.Router, basePath string) HoemV1Router {
  19. h := HoemV1Router{
  20. group: r.Version.Group(basePath),
  21. mDB: r.Db.MasterDB,
  22. rDB: r.Db.ReadDB,
  23. }
  24. h.group.GET("", h.getHomeItems)
  25. h.group.GET("artist", h.getArtist)
  26. return h
  27. }
  28. // getHomeItems godoc
  29. // @Summary get index data
  30. // @Description index 페이지 데이터
  31. // @Schemes
  32. // @Tags home
  33. // @Accept json
  34. // @Produce json
  35. // @Success 200 {object} view.TokenSet
  36. // @Router /home [get]
  37. func (h HoemV1Router) getHomeItems(c *gin.Context) {
  38. userID, _ := gauth.GetCurrentUserIDToInt64(c)
  39. err, items := view.SelectHomeItems(h.rDB, userID)
  40. if errors.Is(err, gorm.ErrRecordNotFound) {
  41. c.AbortWithStatusJSON(http.StatusOK, gin.H{
  42. "message": "There is no matched items(explore)",
  43. })
  44. return
  45. }
  46. c.JSON(http.StatusOK, items)
  47. }
  48. func (h HoemV1Router) getArtist(c *gin.Context) {
  49. limit, ok := c.GetQuery("limit")
  50. if !ok {
  51. limit = "10"
  52. }
  53. limited, _ := strconv.Atoi(fmt.Sprintf("%v", limit))
  54. items := view.SelectArtistsItems(h.rDB, limited)
  55. c.JSON(http.StatusOK, items)
  56. }