add sorting; implement sort criteria in default repo ; adding html header

This commit is contained in:
2025-01-01 23:28:12 +01:00
parent db91ebeece
commit fe9ac81281
6 changed files with 43 additions and 9 deletions

View File

@@ -9,14 +9,19 @@ import (
"net/http" "net/http"
) )
type articleDateOrder struct {}
func (ord articleDateOrder) Weight(a *model.Article) int {
return int(a.PublishDate.Unix()) * -1
}
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")
repo, _ := data.NewDefaultRepository[*model.Article](fds, "article") repo, _ := data.NewDefaultRepository[*model.Article](fds, "article")
articles, _ := repo.GetAll() articles, _ := repo.GetByCriteria(articleDateOrder{})
t := template.Must(template.ParseFiles("templates/article.html", "templates/layout.html")) t := template.Must(template.ParseFiles("templates/article.html", "templates/layout.html"))
_ = t.ExecuteTemplate(w, "base", articles) _ = t.ExecuteTemplate(w, "base", articles[:10])
} }
func main() { func main() {

View File

@@ -3,6 +3,7 @@ package data
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"slices"
"strings" "strings"
) )
@@ -111,7 +112,24 @@ func (repo *DefaultRepository[T]) GetById(id string) (T, error) {
return obj, nil return obj, nil
} }
func (repo *DefaultRepository[T]) GetByCriteria(c ISearchCriteria[T]) ([]T, error) { // Returns a slice of all elememts in the repo sort by the given an
// TODO // ISearchCriteria. Throws an error when the elememts cannot be retrieved from
return nil, nil // 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 {
wa, wb := c.Weight(a), c.Weight(b)
switch {
case wa > wb:
return 1
case wa < wb:
return -1
default:
return 0
}
})
return all, nil
} }

View File

@@ -9,5 +9,5 @@ type IRepository[T IIdentifiable] interface {
Delete(t T) error Delete(t T) error
GetAll() ([]T, error) GetAll() ([]T, error)
GetById(id string) (T, error) GetById(id string) (T, error)
GetByCriteria(c ISearchCriteria[T]) ([]T, error) GetByCriteria(c ISortCriteria[T]) ([]T, error)
} }

View File

@@ -1,6 +1,6 @@
package data package data
// TODO docstring // TODO docstring
type ISearchCriteria[T any] interface { type ISortCriteria[T any] interface {
Matches(t T) bool Weight(t T) int
} }

View File

@@ -1,10 +1,11 @@
{{ define "content" }} {{ define "content" }}
{{ range . }} {{ range . }}
<div class="card" style=""> <div class="card p-2 m-4" style="">
<div class="card-body"> <div class="card-body">
<h5 class="card-title">{{ .Title }}</h5> <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">{{ .Author }}</h6>
<h6 class="card-subtitle mb-2 text-body-secondary">{{ .PublishDate }}</h6>
<p class="card-text">{{ .Content }}</p> <p class="card-text">{{ .Content }}</p>
<a href="{{ .SourceUrl }}" class="card-link">Link</a> <a href="{{ .SourceUrl }}" class="card-link">Link</a>
</div> </div>

View File

@@ -5,6 +5,16 @@
<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">
<body> <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">
<button class="btn btn-outline-success" type="submit">Suchen</button>
</form>
</div>
</nav>
{{ 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>