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)
|
||||
}
|
||||
|
||||
27
internal/viewmodel/ArticleViewModel.go
Normal file
27
internal/viewmodel/ArticleViewModel.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package viewmodel
|
||||
|
||||
import "crowsnest/internal/model"
|
||||
|
||||
|
||||
type ArticleViewModel struct {
|
||||
Title string
|
||||
Author string
|
||||
PublishDate string
|
||||
SourceUrl string
|
||||
Summary string
|
||||
}
|
||||
|
||||
func NewArticleViewModel(a *model.Article) *ArticleViewModel {
|
||||
summary := a.Content
|
||||
if len(a.Content) > 200 {
|
||||
summary = summary[:200]
|
||||
}
|
||||
|
||||
return &ArticleViewModel{
|
||||
Title: a.Title,
|
||||
Author: a.Author,
|
||||
PublishDate: a.PublishDate.Local().Format("02.01.2006 03:04"),
|
||||
SourceUrl: a.SourceUrl,
|
||||
Summary: summary,
|
||||
}
|
||||
}
|
||||
@@ -5,9 +5,8 @@
|
||||
<div class="card p-2 m-4" style="">
|
||||
<div class="card-body">
|
||||
<h5 class="card-title">{{ .Title }}</h5>
|
||||
<h6 class="card-subtitle mb-2 text-body-secondary">{{ .Author }}</h6>
|
||||
<h6 class="card-subtitle mb-2 text-body-secondary">{{ .PublishDate }}</h6>
|
||||
<p class="card-text">{{ .Content }}</p>
|
||||
<h6 class="card-subtitle mb-2 text-body-secondary">{{ .Author }} - {{ .PublishDate }}</h6>
|
||||
<p class="card-text">{{ .Summary }}</p>
|
||||
<a href="{{ .SourceUrl }}" class="card-link">Link</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user