Beispiel #1
0
func (c App) Index(page models.Pagination) revel.Result {

	var savedAuthor string

	if author, ok := c.Session["author"]; ok {
		savedAuthor = author
	}

	params := make(map[string]interface{})

	params["search"] = "%" + page.Search + "%"
	params["search_leading"] = page.Search + "%"
	params["tag"] = page.Tag

	var where string

	if page.Tag != "" {
		where = `
		WHERE QuoteId IN (
			SELECT TagEntry.QuoteId FROM TagEntry
			WHERE TagEntry.Tag = :tag
		) `
	} else {
		where = ` WHERE Quote LIKE :search OR Tags LIKE :search `
	}

	count, err := c.Txn.SelectInt(`SELECT COUNT(*) FROM QdbView `+where, params)

	if err != nil {
		c.Response.Status = http.StatusInternalServerError
		revel.ERROR.Print(err)
		// TODO: redirect to error page
		panic(err)
	}

	order := ` ORDER BY `

	if col, ok := OrderCol[page.Order]; ok {
		order += col
	} else {
		order += OrderCol["date"]
		page.Order = ""
	}

	if page.Asc {
		order += ` ASC `
	} else {
		order += ` DESC `
	}

	var size int

	if page.Size == 0 {
		size = VIEW_SIZE_DEFAULT
	} else {
		size = page.Size
	}

	offset := size * (page.Page - 1)

	params["offset"] = offset
	params["size"] = size

	var entries []models.QdbView

	_, err = c.Txn.Select(&entries,
		`SELECT * FROM QdbView `+where+` `+order+` LIMIT :offset, :size`,
		params,
	)

	if err != nil {
		c.Response.Status = http.StatusInternalServerError
		revel.ERROR.Print(err)
		//TODO: redirect to error page
		panic(err)
	}

	page.HasPrev = offset > 0

	page.HasNext = int64(offset+size) < count

	var tagcloud []string

	_, err = c.Txn.Select(&tagcloud,
		`SELECT Tag From TagCloud LIMIT ?`, TAG_CLOUD_MAX)

	if err != nil {
		c.Response.Status = http.StatusInternalServerError
		revel.ERROR.Print(err)
		//TODO: redirect to error page
		panic(err)
	}

	return c.Render(entries, page, tagcloud, savedAuthor)
}
Beispiel #2
0
				p.Size = VIEW_SIZE_DEFAULT
			}

			params.Bind(&p.Search, "search")
			p.Search = strings.TrimSpace(p.Search)

			params.Bind(&p.Tag, "tag")
			p.Tag = strings.TrimSpace(p.Tag)

			params.Bind(&p.Order, "order")
			p.Order = strings.TrimSpace(p.Order)

			params.Bind(&p.Asc, "asc")

			p.HasNext = false
			p.HasPrev = false

			return reflect.ValueOf(p)
		},
		Unbind: func(output map[string]string, key string, val interface{}) {
			p := val.(models.Pagination)

			if p.Page != 0 && p.Page != 1 {
				revel.Unbind(output, "page", p.Page)
			}

			if p.Size != 0 && p.Size != VIEW_SIZE_DEFAULT {
				revel.Unbind(output, "size", p.Size)
			}

			if p.Search != "" {