2025-01-11 19:10:18 +01:00
|
|
|
package app
|
2025-01-02 15:05:20 +01:00
|
|
|
|
|
|
|
|
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.
|
2025-01-02 16:21:02 +01:00
|
|
|
func (app *App) UpSearch(w http.ResponseWriter, req *http.Request) {
|
2025-01-07 09:32:57 +01:00
|
|
|
// construct search query
|
|
|
|
|
searchTerms := req.FormValue("search")
|
|
|
|
|
if searchTerms == "" {
|
|
|
|
|
app.Index(w, req)
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-01-02 15:05:20 +01:00
|
|
|
|
2025-01-07 09:32:57 +01:00
|
|
|
// get articles
|
2025-01-20 20:34:23 +01:00
|
|
|
articleVMs, err := app.articles.SearchArticleViewModel(searchTerms)
|
2025-01-07 09:32:57 +01:00
|
|
|
if err != nil {
|
|
|
|
|
// treat as no result
|
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// render template
|
2025-01-12 01:12:14 +01:00
|
|
|
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)
|
2025-01-07 09:32:57 +01:00
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, "Failed to render template", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-01-02 15:05:20 +01:00
|
|
|
}
|