add search

This commit is contained in:
2025-01-02 00:35:41 +01:00
parent fe9ac81281
commit f0fc6820f3
4 changed files with 54 additions and 16 deletions

View File

@@ -7,13 +7,28 @@ import (
"fmt"
"html/template"
"net/http"
"strings"
)
type articleDateOrder struct {}
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) {
fds, _ := data.NewFileDatastore("./persistence/spiegel100.json")
@@ -24,9 +39,29 @@ func index(w http.ResponseWriter, req *http.Request) {
_ = 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() {
// routes
http.HandleFunc("/", index)
http.HandleFunc("/search", search)
// serve files from the "static" directory
fs := http.FileServer(http.Dir("./static"))

View File

@@ -112,24 +112,23 @@ func (repo *DefaultRepository[T]) GetById(id string) (T, error) {
return obj, nil
}
// Returns a slice of all elememts in the repo sort by the given an
// ISearchCriteria. Throws an error when the elememts cannot be retrieved from
// the repo.
// Returns a slice of all elememts in the repo that have a
// ISearchCriteria.Weight greater than 0 sort by that weight. Throws an error
// when the elememts cannot be retrieved from the repo.
func (repo *DefaultRepository[T]) GetByCriteria(c ISortCriteria[T]) ([]T, error) {
all, err := repo.GetAll()
if err != nil { return nil, err }
slices.SortFunc(all, func(a, b T) int {
filtered := make([]T, 0)
for _, elem := range all {
if c.Weight(elem) > 0 { filtered = append(filtered, elem) }
}
slices.SortFunc(filtered, func(a, b T) int {
wa, wb := c.Weight(a), c.Weight(b)
switch {
case wa > wb:
return 1
case wa < wb:
return -1
default:
return 0
}
if wa > wb { return -1 }
return 1
})
return all, nil
return filtered, nil
}

View File

@@ -1,5 +1,6 @@
{{ define "content" }}
<div class="content">
{{ range . }}
<div class="card p-2 m-4" style="">
<div class="card-body">
@@ -11,5 +12,6 @@
</div>
</div>
{{ end }}
</div>
{{ end }}

View File

@@ -3,13 +3,14 @@
<html lang="de">
<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 rel="stylesheet" href="https://cdn.jsdelivr.net/npm/unpoly@3.9.5/unpoly.min.css">
<body>
<nav class="navbar sticky-top bg-dark">
<div class="container-fluid">
<a class="navbar-brand">crowsnest</a>
<form class="d-flex" role="search">
<input class="form-control me-2" type="search" placeholder="Suche" aria-label="Suche">
<form class="d-flex" role="search" method="post" action="/search" up-submit up-autosubmit up-target=".content">
<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>
</form>
</div>
@@ -18,6 +19,7 @@
{{ 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/unpoly@3.9.5/unpoly.min.js"></script>
</body>
</html>
{{ end }}