feat: tag system
This commit is contained in:
@ -14,6 +14,9 @@ type Storage interface {
|
||||
Update(id string, n *note.Note)
|
||||
Search(query string) []*note.Note
|
||||
Count() int
|
||||
AddTag(noteID, tag string) error
|
||||
RemoveTag(noteID, tag string) error
|
||||
GetTags(noteID string) []string
|
||||
}
|
||||
|
||||
type NoteStorage struct {
|
||||
@ -70,3 +73,46 @@ func (ns *NoteStorage) Search(query string) []*note.Note {
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
func (ns *NoteStorage) AddTag(noteID, tag string) error {
|
||||
n, ok := ns.Index[noteID]
|
||||
if !ok {
|
||||
return fmt.Errorf("No note with id '%s'", noteID)
|
||||
}
|
||||
|
||||
for _, existing := range n.Tags {
|
||||
if existing == tag {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
n.Tags = append(n.Tags, tag)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ns *NoteStorage) RemoveTag(noteID, tag string) error {
|
||||
n, ok := ns.Index[noteID]
|
||||
if !ok {
|
||||
return fmt.Errorf("No note with id '%s'", noteID)
|
||||
}
|
||||
|
||||
updated := n.Tags[:0]
|
||||
for _, existing := range n.Tags {
|
||||
if existing != tag {
|
||||
updated = append(updated, existing)
|
||||
}
|
||||
}
|
||||
n.Tags = updated
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ns *NoteStorage) GetTags(noteID string) []string {
|
||||
n, ok := ns.Index[noteID]
|
||||
if !ok {
|
||||
return []string{}
|
||||
}
|
||||
|
||||
tags := make([]string, len(n.Tags))
|
||||
copy(tags, n.Tags)
|
||||
return tags
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user