feat: filter and search by tag

This commit is contained in:
2026-02-03 09:53:08 +01:00
parent cb11e34798
commit 229223f77a
9 changed files with 147 additions and 14 deletions

View File

@ -54,6 +54,7 @@ type ViewState struct {
RenderedNote template.HTML
SortBy string
SearchTerm string
TagFilter string
LastActive string
}
@ -79,6 +80,7 @@ func (h *Handler) buildViewState(r *http.Request) (*ViewState, error) {
}
searchTerm := query.Get("search")
tagFilter := query.Get("tag")
// Get notes from service
var notes []*note.Note
@ -111,10 +113,15 @@ func (h *Handler) buildViewState(r *http.Request) (*ViewState, error) {
}
}
if tagFilter != "" {
notes = filterNotesByTag(notes, tagFilter)
}
return &ViewState{
Notes: notes,
SortBy: sortBy,
SearchTerm: searchTerm,
TagFilter: tagFilter,
}, nil
}
@ -166,6 +173,24 @@ func (h *Handler) handleNotes(w http.ResponseWriter, r *http.Request) {
h.templates.Render(w, "index", state)
}
func filterNotesByTag(notes []*note.Note, tag string) []*note.Note {
tag = strings.ToLower(strings.TrimSpace(tag))
if tag == "" {
return notes
}
filtered := make([]*note.Note, 0, len(notes))
for _, n := range notes {
for _, t := range n.Tags {
if strings.EqualFold(t, tag) {
filtered = append(filtered, n)
break
}
}
}
return filtered
}
func (h *Handler) handleTags(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)