add viewmodel for article
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
//"fmt"
|
||||
"crowsnest/internal/data"
|
||||
"crowsnest/internal/model"
|
||||
"fmt"
|
||||
"crowsnest/internal/viewmodel"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -21,8 +21,6 @@ type articleTermFrequency struct {
|
||||
func (ord articleTermFrequency) Weight(a *model.Article) int {
|
||||
score := 0
|
||||
for _, term := range ord.terms {
|
||||
term = strings.TrimSpace(term)
|
||||
if term == "" { continue }
|
||||
score += strings.Count(a.Content, term)
|
||||
}
|
||||
return score
|
||||
@@ -30,32 +28,62 @@ func (ord articleTermFrequency) Weight(a *model.Article) int {
|
||||
|
||||
|
||||
func index(w http.ResponseWriter, req *http.Request) {
|
||||
fds, _ := data.NewFileDatastore("./persistence/spiegel100.json")
|
||||
|
||||
repo, _ := data.NewDefaultRepository[*model.Article](fds, "article")
|
||||
articles, _ := repo.GetByCriteria(articleDateOrder{})
|
||||
// retrieve from repo
|
||||
fds, err := data.NewFileDatastore("./persistence/spiegel100.json")
|
||||
if err != nil { panic(err) }
|
||||
repo, err := data.NewDefaultRepository[*model.Article](fds, "article")
|
||||
if err != nil { panic(err) }
|
||||
articles, err := repo.GetByCriteria(articleDateOrder{})
|
||||
if err != nil { panic(err) }
|
||||
|
||||
// convert to viewmodel
|
||||
articleVMs := make([]*viewmodel.ArticleViewModel, 0, len(articles))
|
||||
for _, a := range articles {
|
||||
articleVMs = append(articleVMs, viewmodel.NewArticleViewModel(a))
|
||||
}
|
||||
|
||||
// render template
|
||||
t := template.Must(template.ParseFiles("templates/article.html", "templates/layout.html"))
|
||||
_ = t.ExecuteTemplate(w, "base", articles[:10])
|
||||
err = t.ExecuteTemplate(w, "base", articleVMs[:10])
|
||||
if err != nil { panic(err) }
|
||||
}
|
||||
|
||||
func search(w http.ResponseWriter, req *http.Request) {
|
||||
// Parse the form data
|
||||
// parse the form data
|
||||
err := req.ParseForm()
|
||||
if err != nil {
|
||||
http.Error(w, "Unable to parse form", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
searchTerms := strings.Split(req.FormValue("search"), " ")
|
||||
|
||||
// collect search terms
|
||||
p := regexp.MustCompile("\\s+")
|
||||
searchTerms := make([]string, 0)
|
||||
for _, elem := range p.Split(req.FormValue("search"), -1) {
|
||||
elem = strings.TrimSpace(elem)
|
||||
if elem == "" { continue }
|
||||
searchTerms = append(searchTerms, elem)
|
||||
}
|
||||
|
||||
// retrieve from repo
|
||||
fds, _ := data.NewFileDatastore("./persistence/spiegel100.json")
|
||||
|
||||
repo, _ := data.NewDefaultRepository[*model.Article](fds, "article")
|
||||
articles, _ := repo.GetByCriteria(articleTermFrequency{ terms: searchTerms })
|
||||
var articles []*model.Article
|
||||
if len(searchTerms) != 0 {
|
||||
articles, _ = repo.GetByCriteria(articleTermFrequency{ terms: searchTerms })
|
||||
} else {
|
||||
articles, _ = repo.GetAll()
|
||||
}
|
||||
|
||||
// convert to viewmodel
|
||||
articleVMs := make([]*viewmodel.ArticleViewModel, 0, len(articles))
|
||||
for _, a := range articles {
|
||||
articleVMs = append(articleVMs, viewmodel.NewArticleViewModel(a))
|
||||
}
|
||||
|
||||
t := template.Must(template.ParseFiles("templates/article.html", "templates/layout.html"))
|
||||
_ = t.ExecuteTemplate(w, "base", articles)
|
||||
err = t.ExecuteTemplate(w, "base", articleVMs)
|
||||
if err != nil { panic(err) }
|
||||
}
|
||||
|
||||
func main() {
|
||||
@@ -67,9 +95,6 @@ func main() {
|
||||
fs := http.FileServer(http.Dir("./static"))
|
||||
http.Handle("/static", http.StripPrefix("/", fs))
|
||||
|
||||
t := template.Must(template.ParseFiles("templates/article.html"))
|
||||
fmt.Println(t)
|
||||
|
||||
// start server
|
||||
http.ListenAndServe(":8080", nil)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user