add sorting; implement sort criteria in default repo ; adding html header
This commit is contained in:
@@ -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() {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
{{ define "content" }}
|
||||
|
||||
{{ range . }}
|
||||
<div class="card" style="">
|
||||
<div class="card p-2 m-4" style="">
|
||||
<div class="card-body">
|
||||
<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">{{ .PublishDate }}</h6>
|
||||
<p class="card-text">{{ .Content }}</p>
|
||||
<a href="{{ .SourceUrl }}" class="card-link">Link</a>
|
||||
</div>
|
||||
|
||||
@@ -4,6 +4,16 @@
|
||||
<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">
|
||||
<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" . }}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user