Sort notes by title for list
This commit is contained in:
parent
8eaa0afda1
commit
38506f7e8b
2 changed files with 19 additions and 9 deletions
|
|
@ -5,13 +5,16 @@ package notes
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"cmp"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
|
"maps"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -98,14 +101,26 @@ func Init() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ns *noteSet) Sort() []*Note {
|
||||||
|
orderByTitle := func(a, b *Note) int {
|
||||||
|
return cmp.Compare(a.Title, b.Title)
|
||||||
|
}
|
||||||
|
return slices.SortedFunc(maps.Keys(*ns), orderByTitle)
|
||||||
|
}
|
||||||
|
|
||||||
func (ns *NoteStore) Get(owner string) noteSet {
|
func (ns *NoteStore) Get(owner string) noteSet {
|
||||||
return ns.notes[owner]
|
return ns.notes[owner]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ns *NoteStore) GetOrdered(owner string) []*Note {
|
||||||
|
notes := ns.Get(owner)
|
||||||
|
return notes.Sort()
|
||||||
|
}
|
||||||
|
|
||||||
func (ns *NoteStore) GetOne(owner string, uid string) (*Note, bool) {
|
func (ns *NoteStore) GetOne(owner string, uid string) (*Note, bool) {
|
||||||
notes := ns.Get(owner)
|
notes := ns.Get(owner)
|
||||||
|
|
||||||
for note, _ := range notes {
|
for note := range notes {
|
||||||
if note.Uid == uid {
|
if note.Uid == uid {
|
||||||
log.Printf("Found single note during GetOne %s", note.Title)
|
log.Printf("Found single note during GetOne %s", note.Title)
|
||||||
return note, true
|
return note, true
|
||||||
|
|
@ -408,10 +423,5 @@ func (n *Note) ToggleBox(nthBox int) (bool, bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Note) HasTag(tag string) bool {
|
func (n *Note) HasTag(tag string) bool {
|
||||||
for _, tag_ := range n.Tags {
|
return slices.Contains(n.Tags, tag)
|
||||||
if tag_ == tag {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -215,11 +215,11 @@ func list(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
titlesAndUrls := make([]titleAndURL, 0)
|
titlesAndUrls := make([]titleAndURL, 0)
|
||||||
|
|
||||||
ns := notes.Notes.Get(user)
|
ns := notes.Notes.GetOrdered(user)
|
||||||
log.Printf("Notes: %+v", notes.Notes)
|
log.Printf("Notes: %+v", notes.Notes)
|
||||||
log.Printf("Notes for %s: %+v", user, ns)
|
log.Printf("Notes for %s: %+v", user, ns)
|
||||||
|
|
||||||
for note := range ns {
|
for _, note := range ns {
|
||||||
if tag == "" || note.HasTag(tag) {
|
if tag == "" || note.HasTag(tag) {
|
||||||
titlesAndUrls = append(
|
titlesAndUrls = append(
|
||||||
titlesAndUrls,
|
titlesAndUrls,
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue