From fe9ac81281f77006647f8a1b8cf9ef3b125b27f8 Mon Sep 17 00:00:00 2001 From: Elias Kohout Date: Wed, 1 Jan 2025 23:28:12 +0100 Subject: [PATCH] add sorting; implement sort criteria in default repo ; adding html header --- cmd/frontend/main.go | 9 +++++++-- internal/data/DefaultRepository.go | 24 +++++++++++++++++++++--- internal/data/IRepository.go | 2 +- internal/data/ISearchCriteria.go | 4 ++-- templates/article.html | 3 ++- templates/layout.html | 10 ++++++++++ 6 files changed, 43 insertions(+), 9 deletions(-) diff --git a/cmd/frontend/main.go b/cmd/frontend/main.go index f8f26bf..55c20de 100644 --- a/cmd/frontend/main.go +++ b/cmd/frontend/main.go @@ -9,14 +9,19 @@ import ( "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) { fds, _ := data.NewFileDatastore("./persistence/spiegel100.json") 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.ExecuteTemplate(w, "base", articles) + _ = t.ExecuteTemplate(w, "base", articles[:10]) } func main() { diff --git a/internal/data/DefaultRepository.go b/internal/data/DefaultRepository.go index 90884d8..ddc7d73 100644 --- a/internal/data/DefaultRepository.go +++ b/internal/data/DefaultRepository.go @@ -3,6 +3,7 @@ package data import ( "encoding/json" "errors" + "slices" "strings" ) @@ -111,7 +112,24 @@ func (repo *DefaultRepository[T]) GetById(id string) (T, error) { return obj, nil } -func (repo *DefaultRepository[T]) GetByCriteria(c ISearchCriteria[T]) ([]T, error) { - // TODO - return nil, 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. +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 } diff --git a/internal/data/IRepository.go b/internal/data/IRepository.go index c27e570..aa08b5d 100644 --- a/internal/data/IRepository.go +++ b/internal/data/IRepository.go @@ -9,5 +9,5 @@ type IRepository[T IIdentifiable] interface { Delete(t T) error GetAll() ([]T, error) GetById(id string) (T, error) - GetByCriteria(c ISearchCriteria[T]) ([]T, error) + GetByCriteria(c ISortCriteria[T]) ([]T, error) } diff --git a/internal/data/ISearchCriteria.go b/internal/data/ISearchCriteria.go index 08f2d75..678adcd 100644 --- a/internal/data/ISearchCriteria.go +++ b/internal/data/ISearchCriteria.go @@ -1,6 +1,6 @@ package data // TODO docstring -type ISearchCriteria[T any] interface { - Matches(t T) bool +type ISortCriteria[T any] interface { + Weight(t T) int } diff --git a/templates/article.html b/templates/article.html index 08c6e3d..0bfb650 100644 --- a/templates/article.html +++ b/templates/article.html @@ -1,10 +1,11 @@ {{ define "content" }} {{ range . }} -
+
{{ .Title }}
{{ .Author }}
+
{{ .PublishDate }}

{{ .Content }}

Link
diff --git a/templates/layout.html b/templates/layout.html index 8d47367..05178f4 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -4,6 +4,16 @@ + + {{ template "content" . }}