restructuring

This commit is contained in:
2025-01-02 16:21:02 +01:00
parent f80889ff95
commit fbca771479
6 changed files with 66 additions and 54 deletions

View File

@@ -6,6 +6,7 @@ import (
"encoding/hex"
)
// TODO docstring
type Article struct {
SourceUrl string
@@ -16,18 +17,38 @@ type Article struct {
Author string
}
// TODO docstring
type ArticleViewModel struct {
Title string
Author string
PublishDate string
SourceUrl string
Summary string
}
// Generates a hash based on the source url of the article. Can be used to
// identify the article.
func (article *Article) Hash() string {
hash := sha256.Sum256([]byte(article.SourceUrl))
return hex.EncodeToString(hash[:])
// TODO docstring
func (a *Article) ViewModel() *ArticleViewModel {
summary := a.Content
if len(a.Content) > 300 {
summary = summary[:300]
}
return &ArticleViewModel{
Title: a.Title,
Author: a.Author,
PublishDate: a.PublishDate.Local().Format("02.01.2006 03:04"),
SourceUrl: a.SourceUrl,
Summary: summary,
}
}
// --- implement IIdentifiable interface ---
// Using the Identifier attribute as the Id.
// Generates a hash based on the source url of the article. Can be used to
// identify the article.
func (article *Article) Id() string {
return article.Hash()
hash := sha256.Sum256([]byte(article.SourceUrl))
return hex.EncodeToString(hash[:])
}

View File

@@ -1,42 +0,0 @@
package routes
import (
"crowsnest/internal/data"
"crowsnest/internal/model"
"crowsnest/internal/viewmodel"
"html/template"
"net/http"
)
// sort criteria
type articleDateOrder struct {}
func (ord articleDateOrder) Weight(a *model.Article) int {
return int(a.PublishDate.Unix())
}
// List the latest articles using the base template.
func Index(w http.ResponseWriter, req *http.Request) {
// retrieve from repo
fds, err := data.NewFileDatastore("persistence/spiegel100.json")
if err != nil { http.Error(w, "Failed to load datastore", http.StatusInternalServerError); return; }
repo, err := data.NewDefaultRepository[*model.Article](fds, "article")
if err != nil { http.Error(w, "Failed to create repository", http.StatusInternalServerError); return; }
articles, err := repo.GetByCriteria(articleDateOrder{})
if err != nil { http.Error(w, "Failed to get articles", http.StatusInternalServerError); return; }
// truncate
if len(articles) > 10 {
articles = articles[:10]
}
// 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("assets/templates/article.html", "assets/templates/layout.html"))
err = t.ExecuteTemplate(w, "base", articleVMs)
if err != nil { http.Error(w, "Failed to render template", http.StatusInternalServerError); return; }
}

View File

@@ -1,68 +0,0 @@
package routes
import (
"crowsnest/internal/data"
"crowsnest/internal/model"
"crowsnest/internal/viewmodel"
"html/template"
"net/http"
"regexp"
"strings"
)
// sort criteria
type articleTermFrequency struct {
terms []string
}
func (ord articleTermFrequency) Weight(a *model.Article) int {
score := 0
for _, term := range ord.terms {
score += strings.Count(a.Content, term)
}
return score
}
// Enpoint that returns a list of articles given search terms in the post
// request of a search form. Uses the content template.
func UpSearch(w http.ResponseWriter, req *http.Request) {
// parse the form data
err := req.ParseForm()
if err != nil { http.Error(w, "Unable to parse form", http.StatusBadRequest); return; }
// 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")
if err != nil { http.Error(w, "Failed to read datastore", http.StatusInternalServerError); return; }
repo, _ := data.NewDefaultRepository[*model.Article](fds, "article")
if err != nil { http.Error(w, "Failed to create repository", http.StatusInternalServerError); return; }
var articles []*model.Article
if len(searchTerms) != 0 {
articles, err = repo.GetByCriteria(articleTermFrequency{ terms: searchTerms })
} else {
Index(w, req)
return
}
if err != nil { http.Error(w, "Failed to get articles from repo", http.StatusInternalServerError); return; }
// 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("assets/templates/article.html"))
err = t.ExecuteTemplate(w, "content", articleVMs)
if err != nil { http.Error(w, "Failed to render template", http.StatusInternalServerError); return; }
}

View File

@@ -1,27 +0,0 @@
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) > 300 {
summary = summary[:300]
}
return &ArticleViewModel{
Title: a.Title,
Author: a.Author,
PublishDate: a.PublishDate.Local().Format("02.01.2006 03:04"),
SourceUrl: a.SourceUrl,
Summary: summary,
}
}