gophertype/pkg/controllers/search.go

37 lines
791 B
Go

package controllers
import (
"net/http"
"strconv"
"git.kirsle.net/apps/gophertype/pkg/models"
"git.kirsle.net/apps/gophertype/pkg/responses"
"git.kirsle.net/apps/gophertype/pkg/session"
)
// BlogSearch at "/blog/search" for searching blog entries.
func BlogSearch(w http.ResponseWriter, r *http.Request) {
var (
query = r.FormValue("q")
pageStr = r.FormValue("page")
page int
)
if a, err := strconv.Atoi(pageStr); err == nil {
page = a
}
pp, err := models.Posts.SearchPosts(query, page, 24)
if err != nil {
session.FlashError(w, r, "Error searching posts: %s", err)
responses.Redirect(w, r, "/")
return
}
v := responses.NewTemplateVars(w, r)
v.V["posts"] = pp
v.V["search"] = query
responses.RenderTemplate(w, r, "_builtin/blog/search.gohtml", v)
}