Ejemplo n.º 1
0
func articleListHandler(w http.ResponseWriter, redis *models.RedisLogger, form articleListForm) {
	if ok, err := checkToken(redis, form.Token); !ok {
		writeResponse(w, err)
		return
	}

	if form.PageCount == 0 {
		form.PageCount = 50
	}
	total, articles, _ := models.ArticleList(form.Sort, form.PageIndex, form.PageCount)
	list := make([]*articleInfo, len(articles))
	for i, _ := range articles {
		list[i] = convertArticle(&articles[i], redis)
	}
	pages := total / form.PageCount
	if total%form.PageCount > 0 {
		pages++
	}
	resp := map[string]interface{}{
		"articles":     list,
		"page_index":   form.PageIndex,
		"page_total":   pages,
		"total_number": total,
	}

	writeResponse(w, resp)
}
Ejemplo n.º 2
0
func latestArticlesHandler(w http.ResponseWriter, p pagination) {
	if p.PageIndex == 0 {
		p.PageIndex = 1
	}
	total, articles, _ := models.ArticleList("", p.PageIndex-1, p.PageSize)

	var list []*Article
	for _, article := range articles {
		list = append(list, convertArticle(&article))
	}

	p.Total = total

	writeResponse(w, true, list, &p, nil)
}