Пример #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)
}
Пример #2
0
	TagsBinder = revel.Binder{

		Bind: revel.ValueBinder(func(val string, typ reflect.Type) reflect.Value {
			if len(val) == 0 {
				return reflect.Zero(typ)
			}
			s := strings.Split(val, INPUT_TAG_DELIM)

			return reflect.ValueOf(s)
		}),
		Unbind: nil,
	}

	PaginationBinder = revel.Binder{
		Bind: func(params *revel.Params, name string, typ reflect.Type) reflect.Value {
			var p models.Pagination

			params.Bind(&p.Page, "page")

			if p.Page == 0 {
				p.Page = 1
			}

			params.Bind(&p.Size, "size")

			if p.Size != 0 && p.Size > VIEW_SIZE_MAX {
				p.Size = VIEW_SIZE_DEFAULT
			}

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