Пример #1
0
// ViewController handles the blog index page
func ViewController(c *gin.Context) {
	var err error

	currentPage, err := strconv.Atoi(c.Param("page"))
	if currentPage < 1 {
		currentPage = 1
	}

	// holds out page metadata from settings
	metadata, err := u.GetMetadata()
	if err != nil {
		c.Error(err).SetMeta("blog.ViewController.GetMetadata")
		c.HTML(http.StatusInternalServerError, "error.tmpl", nil)
		return
	}

	// get a count of the blogs
	total, err := u.Storm.Count(&m.BlogType{})
	if err != nil {
		c.Error(err).SetMeta("blog.ViewController.Count")
		c.HTML(http.StatusInternalServerError, "error.tmpl", nil)
		return
	}

	// holds our pagination data
	paginate := u.Paged{}

	paginate.Path = "/blog"
	paginate.CurrentPage = currentPage
	paginate.Total = total
	paginate.PerPage = 10
	paginate.Desc()

	var posts m.Blogs

	// get all the blog posts with a limit
	err = u.Storm.All(&posts, storm.Limit(paginate.PerPage), storm.Skip(paginate.Skip), storm.Reverse())
	if err != nil {
		c.Error(err).SetMeta("blog.ViewController.All")
		c.HTML(http.StatusInternalServerError, "error.tmpl", nil)
		return
	}

	for idx, post := range posts {
		// format post with markdown
		posts[idx].ContentOut = u.Markdown(post.Content)
		// convert time
		posts[idx].HumanTime = post.StoredTime.Format("2006-01-02")
	}

	// values for template
	vals := struct {
		Meta  m.Metadata
		Paged u.Paged
		Posts m.Blogs
	}{
		Meta:  metadata,
		Paged: paginate,
		Posts: posts,
	}

	c.HTML(http.StatusOK, "blog.tmpl", vals)

	return

}
Пример #2
0
// IndexController handles the galleries index page
func IndexController(c *gin.Context) {
	var err error

	categoryID, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		c.Error(err).SetMeta("gallery.IndexController")
		c.HTML(http.StatusInternalServerError, "error.tmpl", nil)
		return
	}

	currentPage, _ := strconv.Atoi(c.Param("page"))
	if currentPage < 1 {
		currentPage = 1
	}

	// holds out page metadata from settings
	metadata, err := u.GetMetadata()
	if err != nil {
		c.Error(err).SetMeta("gallery.IndexController.GetMetadata")
		c.HTML(http.StatusInternalServerError, "error.tmpl", nil)
		return
	}

	var total []m.GalleryType

	err = u.Storm.Find("Category", categoryID, &total)
	if err != nil {
		c.Error(err).SetMeta("gallery.IndexController.Find")
		c.HTML(http.StatusInternalServerError, "error.tmpl", nil)
		return
	}

	// holds our pagination data
	paginate := u.Paged{}

	paginate.Path = "/comics"
	paginate.CurrentPage = currentPage
	paginate.Total = len(total)
	paginate.PerPage = 6
	paginate.Desc()

	var category m.CategoryType

	// get category info
	err = u.Storm.One("ID", categoryID, &category)
	if err != nil {
		c.Error(err).SetMeta("gallery.IndexController")
		c.HTML(http.StatusInternalServerError, "error.tmpl", nil)
		return
	}

	// convert the gallery desc
	category.DescOut = u.Markdown(category.Desc)

	var galleries []m.GalleryType

	// get all the galleries with a limit
	err = u.Storm.Find("Category", categoryID, &galleries, storm.Limit(paginate.PerPage), storm.Skip(paginate.Skip), storm.Reverse())
	if err != nil {
		c.Error(err).SetMeta("gallery.IndexController.Find")
		c.HTML(http.StatusInternalServerError, "error.tmpl", nil)
		return
	}

	for idx := range galleries {
		// cover image is the first image in the slice
		galleries[idx].Cover = galleries[idx].Files[0].Filename
	}

	// values for template
	vals := struct {
		Meta      m.Metadata
		Paged     u.Paged
		Category  m.CategoryType
		Galleries []m.GalleryType
		All       bool
	}{
		Meta:      metadata,
		Paged:     paginate,
		Category:  category,
		Galleries: galleries,
		All:       true,
	}

	c.HTML(http.StatusOK, "gallery.tmpl", vals)

	return

}
Пример #3
0
// ViewController handles the comic image pages
func ViewController(c *gin.Context) {
	var err error

	// the key for private galleries
	privateKey := c.Query("key")

	comicID, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		c.Error(err).SetMeta("image.ViewController")
		c.HTML(http.StatusInternalServerError, "error.tmpl", nil)
		return
	}

	currentPage, err := strconv.Atoi(c.Param("page"))
	if currentPage < 1 {
		currentPage = 1
	}

	// holds out page metadata from settings
	metadata, err := u.GetMetadata()
	if err != nil {
		c.Error(err).SetMeta("image.ViewController.GetMetadata")
		c.HTML(http.StatusInternalServerError, "error.tmpl", nil)
		return
	}

	var gallery m.GalleryType
	var image m.FileType
	var title string

	err = u.Storm.One("ID", comicID, &gallery)
	if err != nil {
		c.Error(err).SetMeta("image.ViewController.One")
		c.HTML(http.StatusInternalServerError, "error.tmpl", nil)
		return
	}

	// holds our pagination data
	paginate := u.Paged{}

	paginate.Key = privateKey
	paginate.Path = "/image/" + c.Param("id")
	paginate.CurrentPage = currentPage
	paginate.Total = len(gallery.Files)
	paginate.PerPage = 1
	paginate.Desc()

	sort.Sort(gallery.Files)

	for i, c := range gallery.Files {
		i++
		if i == currentPage {
			image = c
		}
	}

	// convert the gallery desc
	image.DescOut = u.Markdown(image.Desc)

	title = gallery.Title

	// values for template
	vals := struct {
		Meta  m.Metadata
		Paged u.Paged
		Comic int
		Title string
		Image m.FileType
	}{
		Meta:  metadata,
		Paged: paginate,
		Comic: comicID,
		Title: title,
		Image: image,
	}

	c.HTML(http.StatusOK, "image.tmpl", vals)

	return

}