Example #1
0
File: random.go Project: firba1/irq
func Random(db model.Model, r render.Render, req *http.Request, isJson IsJson) {
	qs := req.URL.Query()

	search := qs.Get("query")

	maxLines, err := strconv.Atoi(qs.Get("max-lines"))
	if err != nil || maxLines < 1 {
		maxLines = 0
	}

	quotes, err := db.GetQuotes(model.Query{
		Limit:       1,
		Search:      search,
		MaxLines:    maxLines,
		OrderBy:     []string{"rand()"},
		IncludeTags: qs["tags"],
		ExcludeTags: qs["exclude-tags"],
	})

	if err != nil || len(quotes) == 0 {
		RenderError(r, 500, isJson, fmt.Sprint("quote not found", err))
		return
	}

	quote := quotes[0]

	if isJson {
		r.JSON(200, quote)
	} else {
		r.Redirect(fmt.Sprintf("/quote/%d", quote.ID))
	}
}