43 lines
1.0 KiB
Go
43 lines
1.0 KiB
Go
package app
|
|
|
|
import (
|
|
"html/template"
|
|
"net/http"
|
|
)
|
|
|
|
// Enpoint that returns a list of articles given search terms in the post
|
|
// request of a search form. Uses the content template.
|
|
func (app *App) UpSearch(w http.ResponseWriter, req *http.Request) {
|
|
// construct search query
|
|
searchTerms := req.FormValue("search")
|
|
if searchTerms == "" {
|
|
app.Index(w, req)
|
|
return
|
|
}
|
|
|
|
// get articles
|
|
articleVMs, err := app.articles.SearchArticleViewModel(searchTerms)
|
|
if err != nil {
|
|
// treat as no result
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
// render template
|
|
t := template.Must(template.ParseFiles(
|
|
"assets/templates/article.html",
|
|
"assets/templates/layout.html",
|
|
"assets/templates/components/pagination.html"))
|
|
|
|
data := map[string]interface{}{
|
|
"SelectedNavItemArticle": true,
|
|
"ArticleVMs": &articleVMs,
|
|
"Paginations": nil,
|
|
}
|
|
err = t.ExecuteTemplate(w, "base", data)
|
|
if err != nil {
|
|
http.Error(w, "Failed to render template", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|