add search
This commit is contained in:
@@ -7,13 +7,28 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type articleDateOrder struct {}
|
type articleDateOrder struct {}
|
||||||
func (ord articleDateOrder) Weight(a *model.Article) int {
|
func (ord articleDateOrder) Weight(a *model.Article) int {
|
||||||
return int(a.PublishDate.Unix()) * -1
|
return int(a.PublishDate.Unix())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type articleTermFrequency struct {
|
||||||
|
terms []string
|
||||||
|
}
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
func index(w http.ResponseWriter, req *http.Request) {
|
func index(w http.ResponseWriter, req *http.Request) {
|
||||||
fds, _ := data.NewFileDatastore("./persistence/spiegel100.json")
|
fds, _ := data.NewFileDatastore("./persistence/spiegel100.json")
|
||||||
|
|
||||||
@@ -24,9 +39,29 @@ func index(w http.ResponseWriter, req *http.Request) {
|
|||||||
_ = t.ExecuteTemplate(w, "base", articles[:10])
|
_ = t.ExecuteTemplate(w, "base", articles[:10])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func search(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
|
||||||
|
}
|
||||||
|
searchTerms := strings.Split(req.FormValue("search"), " ")
|
||||||
|
|
||||||
|
|
||||||
|
fds, _ := data.NewFileDatastore("./persistence/spiegel100.json")
|
||||||
|
|
||||||
|
repo, _ := data.NewDefaultRepository[*model.Article](fds, "article")
|
||||||
|
articles, _ := repo.GetByCriteria(articleTermFrequency{ terms: searchTerms })
|
||||||
|
|
||||||
|
t := template.Must(template.ParseFiles("templates/article.html", "templates/layout.html"))
|
||||||
|
_ = t.ExecuteTemplate(w, "base", articles)
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// routes
|
// routes
|
||||||
http.HandleFunc("/", index)
|
http.HandleFunc("/", index)
|
||||||
|
http.HandleFunc("/search", search)
|
||||||
|
|
||||||
// serve files from the "static" directory
|
// serve files from the "static" directory
|
||||||
fs := http.FileServer(http.Dir("./static"))
|
fs := http.FileServer(http.Dir("./static"))
|
||||||
|
|||||||
@@ -112,24 +112,23 @@ func (repo *DefaultRepository[T]) GetById(id string) (T, error) {
|
|||||||
return obj, nil
|
return obj, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns a slice of all elememts in the repo sort by the given an
|
// Returns a slice of all elememts in the repo that have a
|
||||||
// ISearchCriteria. Throws an error when the elememts cannot be retrieved from
|
// ISearchCriteria.Weight greater than 0 sort by that weight. Throws an error
|
||||||
// the repo.
|
// when the elememts cannot be retrieved from the repo.
|
||||||
func (repo *DefaultRepository[T]) GetByCriteria(c ISortCriteria[T]) ([]T, error) {
|
func (repo *DefaultRepository[T]) GetByCriteria(c ISortCriteria[T]) ([]T, error) {
|
||||||
all, err := repo.GetAll()
|
all, err := repo.GetAll()
|
||||||
if err != nil { return nil, err }
|
if err != nil { return nil, err }
|
||||||
|
|
||||||
slices.SortFunc(all, func(a, b T) int {
|
filtered := make([]T, 0)
|
||||||
wa, wb := c.Weight(a), c.Weight(b)
|
for _, elem := range all {
|
||||||
switch {
|
if c.Weight(elem) > 0 { filtered = append(filtered, elem) }
|
||||||
case wa > wb:
|
|
||||||
return 1
|
|
||||||
case wa < wb:
|
|
||||||
return -1
|
|
||||||
default:
|
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
slices.SortFunc(filtered, func(a, b T) int {
|
||||||
|
wa, wb := c.Weight(a), c.Weight(b)
|
||||||
|
if wa > wb { return -1 }
|
||||||
|
return 1
|
||||||
})
|
})
|
||||||
|
|
||||||
return all, nil
|
return filtered, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
{{ define "content" }}
|
{{ define "content" }}
|
||||||
|
|
||||||
|
<div class="content">
|
||||||
{{ range . }}
|
{{ range . }}
|
||||||
<div class="card p-2 m-4" style="">
|
<div class="card p-2 m-4" style="">
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
@@ -11,5 +12,6 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
</div>
|
||||||
|
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|||||||
@@ -3,13 +3,14 @@
|
|||||||
<html lang="de">
|
<html lang="de">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
|
||||||
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/unpoly@3.9.5/unpoly.min.css">
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
<nav class="navbar sticky-top bg-dark">
|
<nav class="navbar sticky-top bg-dark">
|
||||||
<div class="container-fluid">
|
<div class="container-fluid">
|
||||||
<a class="navbar-brand">crowsnest</a>
|
<a class="navbar-brand">crowsnest</a>
|
||||||
<form class="d-flex" role="search">
|
<form class="d-flex" role="search" method="post" action="/search" up-submit up-autosubmit up-target=".content">
|
||||||
<input class="form-control me-2" type="search" placeholder="Suche" aria-label="Suche">
|
<input name="search" class="form-control me-2" type="search" placeholder="Suche" aria-label="Suche">
|
||||||
<button class="btn btn-outline-success" type="submit">Suchen</button>
|
<button class="btn btn-outline-success" type="submit">Suchen</button>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
@@ -18,6 +19,7 @@
|
|||||||
{{ template "content" . }}
|
{{ template "content" . }}
|
||||||
|
|
||||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
|
||||||
|
<script src="https://cdn.jsdelivr.net/npm/unpoly@3.9.5/unpoly.min.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|||||||
Reference in New Issue
Block a user