Exemplo n.º 1
0
func getIndex(w http.ResponseWriter, r *http.Request, ctx routes.Context) error {
	tpl, err := routes.LoadTemplates("base.tpl", "index.tpl")
	if err != nil {
		return tracederror.New(err)
	}

	recent, err := models.GetRecentBlogPosts(ctx.DB, 4, 0)
	if err != nil {
		return tracederror.New(err)
	}

	featured, err := models.GetRecentPostsInCategory(ctx.DB, "featured", 1, 0)
	if err != nil {
		return tracederror.New(err)
	}

	if len(featured) == 0 {
		return tracederror.New(fmt.Errorf("No featured posts?"))
	}

	data := indexPage{
		Posts:    recent,
		Featured: featured[0],
	}

	return routes.RenderTemplateWithData(w, r, tpl, &data)
}
Exemplo n.º 2
0
func getCategoryArchive(w http.ResponseWriter, r *http.Request, ctx routes.Context) error {
	vars := mux.Vars(r)

	page, err := strconv.ParseInt(vars["page"], 10, 32)
	if err != nil {
		return tracederror.New(err)
	}

	if page <= 0 {
		return routes.MakeHttpError(fmt.Errorf("Page must be larger than 0"), http.StatusBadRequest, r)
	}

	category := vars["category"]
	valid, err := models.IsValidCategory(ctx.DB, category)

	if err != nil {
		return tracederror.New(err)
	}

	if !valid {
		return routes.MakeHttpError(fmt.Errorf("Invalid category %s", category), http.StatusBadRequest, r)
	}

	tpl, err := routes.LoadTemplates("base.tpl", "archive.tpl")
	if err != nil {
		return routes.MakeHttpError(err, http.StatusNotFound, r)
	}

	posts, err := models.GetRecentPostsInCategory(ctx.DB, category, 100, int32((page-1)*100))
	if err != nil {
		return tracederror.New(err)
	}

	data := archivePage{
		Page:     int32(page),
		Posts:    posts,
		Category: strings.Title(category),
	}

	return routes.RenderTemplateWithData(w, r, tpl, data)
}