feat(release): v0.1.0
commit06ed2c3cbeAuthor: adminoo <git@kadath.corp> Date: Tue Feb 3 11:34:24 2026 +0100 fix: changed detected by scanner but no updated by render layer commit01dcaf882aAuthor: adminoo <git@kadath.corp> Date: Tue Feb 3 10:19:05 2026 +0100 feat: VERSION bumb commit229223f77aAuthor: adminoo <git@kadath.corp> Date: Tue Feb 3 09:53:08 2026 +0100 feat: filter and search by tag commitcb11e34798Author: adminoo <git@kadath.corp> Date: Tue Feb 3 09:41:03 2026 +0100 feat: tag system commit3f5cf0d673Author: adminoo <git@kadath.corp> Date: Tue Feb 3 09:15:29 2026 +0100 feat: sqlite storage draft commitd6617cec02Author: adminoo <git@kadath.corp> Date: Tue Feb 3 09:04:11 2026 +0100 feat: metadata draft commit7238d02a13Author: adminoo <git@kadath.corp> Date: Mon Feb 2 10:18:42 2026 +0100 fix: body overflowing commit16ff836274Author: adminoo <git@kadath.corp> Date: Mon Feb 2 10:09:01 2026 +0100 feat: tests for http handlers and render package commit36ac3f03aaAuthor: adminoo <git@kadath.corp> Date: Mon Feb 2 09:45:29 2026 +0100 feat: Dark theme, placeholder metadata panel commite6923fa4f5Author: adminoo <git@kadath.corp> Date: Sun Feb 1 18:26:59 2026 +0100 fix: uneeded func + uneeded bogus note creation logic commit4458ba2d15Author: adminoo <git@kadath.corp> Date: Sun Feb 1 18:26:21 2026 +0100 feat: log when changing note states commit92a6f84540Author: adminoo <git@kadath.corp> Date: Sun Feb 1 16:55:40 2026 +0100 possibly first working draft commite27aadc603Author: adminoo <git@kadath.corp> Date: Sun Feb 1 11:55:16 2026 +0100 draft shits
This commit is contained in:
130
internal/storage/storage.go
Normal file
130
internal/storage/storage.go
Normal file
@ -0,0 +1,130 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"donniemarko/internal/note"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Storage interface {
|
||||
GetAll() []*note.Note
|
||||
Get(id string) (*note.Note, error)
|
||||
Create(n *note.Note) error
|
||||
Delete(id string)
|
||||
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 {
|
||||
Index map[string]*note.Note
|
||||
}
|
||||
|
||||
func NewNoteStorage() *NoteStorage {
|
||||
return &NoteStorage{Index: make(map[string]*note.Note)}
|
||||
}
|
||||
|
||||
// GetAll returns all notes stored in the index
|
||||
func (ns *NoteStorage) GetAll() []*note.Note {
|
||||
var notes []*note.Note
|
||||
|
||||
for _, value := range ns.Index {
|
||||
notes = append(notes, value)
|
||||
}
|
||||
return notes
|
||||
}
|
||||
|
||||
func (ns *NoteStorage) Get(id string) (*note.Note, error) {
|
||||
n, ok := ns.Index[id]
|
||||
if ok {
|
||||
return n, nil
|
||||
}
|
||||
return nil, fmt.Errorf("No note with id '%s'", id)
|
||||
}
|
||||
|
||||
func (ns *NoteStorage) Create(n *note.Note) error {
|
||||
ns.Index[n.ID] = n
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ns *NoteStorage) Count() int {
|
||||
return len(ns.Index)
|
||||
}
|
||||
|
||||
func (ns *NoteStorage) Delete(id string) {
|
||||
delete(ns.Index, id)
|
||||
}
|
||||
|
||||
func (ns *NoteStorage) Update(id string, n *note.Note) {
|
||||
ns.Index[id] = n
|
||||
}
|
||||
|
||||
func (ns *NoteStorage) Search(query string) []*note.Note {
|
||||
results := []*note.Note{}
|
||||
for _, note := range ns.Index {
|
||||
lowContent := strings.ToLower(string(note.Content))
|
||||
lowQuery := strings.ToLower(query)
|
||||
if strings.Contains(lowContent, lowQuery) || tagsContain(note.Tags, lowQuery) {
|
||||
results = append(results, note)
|
||||
}
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
func tagsContain(tags []string, query string) bool {
|
||||
if query == "" {
|
||||
return false
|
||||
}
|
||||
for _, tag := range tags {
|
||||
if strings.Contains(strings.ToLower(tag), query) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
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