Esempio n. 1
0
// UpdateController updates category information
func UpdateController(c *gin.Context) {
	var err error
	var uf updateForm

	err = c.Bind(&uf)
	if err != nil {
		c.Error(err).SetMeta("category.UpdateController.Bind")
		c.HTML(http.StatusInternalServerError, "error.tmpl", nil)
		return
	}

	// start transaction
	tx, err := u.Storm.Begin(true)
	if err != nil {
		c.Error(err).SetMeta("category.UpdateController.Begin")
		c.HTML(http.StatusInternalServerError, "error.tmpl", nil)
		return
	}
	defer tx.Rollback()

	var category m.CategoryType

	// get category details
	err = tx.One("ID", uf.ID, &category)
	if err != nil {
		c.Error(err).SetMeta("category.UpdateController.One")
		c.HTML(http.StatusInternalServerError, "error.tmpl", nil)
		return
	}

	category.Title = uf.Title
	category.Desc = uf.Desc

	// save with updated info
	err = tx.Save(&category)
	if err != nil {
		c.Error(err).SetMeta("category.UpdateController.Save")
		c.HTML(http.StatusInternalServerError, "error.tmpl", nil)
		return
	}

	// commit
	tx.Commit()

	c.Redirect(http.StatusFound, "/admin/panel")

	return

}
Esempio n. 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

}