package router

import (
	"demo/data/dao"
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
)

func ArticleRouter(engine *gin.RouterGroup) {
	user := engine.Group("/article")
	PushRouter(user, "GET", "/collection", GetArticleCollection)
	PushRouter(user, "GET", "/topic", GetArticleTopicList)
}

func GetArticleCollection(c *gin.Context) {
	pageNum := c.Query("pageNum")
	pageSize := c.Query("pageSize")
	list, err := dao.GetArticleList(cast.ToInt(pageNum), cast.ToInt(pageSize))
	if err != nil {
		c.JSON(200, CreateResultError(500, "数据读取失败"))
		return
	}
	c.JSON(200, CreateResultData(list))
}

func GetArticleTopicList(c *gin.Context) {
	topicId := c.Query("topicId")
	pageNum := c.Query("pageNum")
	pageSize := c.Query("pageSize")

	topic, vo, err := dao.GetArticleListByTopicId(cast.ToInt64(topicId), cast.ToInt(pageNum), cast.ToInt(pageSize))
	if err != nil {
		c.JSON(200, CreateResultError(500, "数据读取失败"))
		return
	}

	m := make(map[string]interface{})
	m["topic"] = topic
	m["list"] = vo.List
	m["total"] = vo.Total

	c.JSON(200, CreateResultData(m))
}
