draft shits
This commit is contained in:
67
internal/storage/storage.go
Normal file
67
internal/storage/storage.go
Normal file
@ -0,0 +1,67 @@
|
||||
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
|
||||
}
|
||||
|
||||
type NoteStorage struct {
|
||||
Index map[string]*note.Note
|
||||
}
|
||||
|
||||
func NewNoteStorage() *NoteStorage {
|
||||
return &NoteStorage{Index: make(map[string]*note.Note)}
|
||||
}
|
||||
|
||||
func (ns *NoteStorage) GetAll() []*note.Note {
|
||||
notes := make([]*note.Note, 0, len(ns.Index))
|
||||
|
||||
// Step 3: Iterate over the map
|
||||
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) 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) {
|
||||
results = append(results, note)
|
||||
}
|
||||
}
|
||||
return results
|
||||
}
|
||||
53
internal/storage/storage_test.go
Normal file
53
internal/storage/storage_test.go
Normal file
@ -0,0 +1,53 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"donniemarko/internal/note"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var ns *NoteStorage
|
||||
var n1, n2 *note.Note
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
ns = NewNoteStorage()
|
||||
n1 = note.NewNote()
|
||||
n1.Path = "test/note1.md"
|
||||
n1.ID = note.GenerateNoteID(n1.Path)
|
||||
n1.Content = "# hola amigo"
|
||||
|
||||
n2 = note.NewNote()
|
||||
n2.Path = "note2.md"
|
||||
n2.ID = note.GenerateNoteID(n2.Path)
|
||||
n2.Content = "# ah si ?"
|
||||
m.Run()
|
||||
}
|
||||
|
||||
func TestNoteStorageCreate(t *testing.T) {
|
||||
ns.Create(n1)
|
||||
ns.Create(n2)
|
||||
|
||||
if len(ns.Index) < 2 {
|
||||
t.Errorf("Creating notes should add them to the storage. Wanted 2, got '%v'", len(ns.Index))
|
||||
}
|
||||
}
|
||||
|
||||
func TestNoteStorageDelete(t *testing.T) {
|
||||
ns.Delete(n1.ID)
|
||||
|
||||
if len(ns.Index) > 1 {
|
||||
t.Errorf("Deleting notes should remove from to the storage. Wanted 1, got '%v'", len(ns.Index))
|
||||
}
|
||||
}
|
||||
|
||||
func TestNoteStorageGetUpdate(t *testing.T) {
|
||||
ns.Update(n2.ID, n1)
|
||||
|
||||
nn2, err := ns.Get(n2.ID)
|
||||
if err != nil {
|
||||
t.Errorf("Error retrieving note with id '%s': '%v'", n2.ID, err)
|
||||
}
|
||||
|
||||
if nn2.Content != n1.Content {
|
||||
t.Errorf("Updating a note should reflect it in storage. Wanted '%s', got '%s'\n", n1.Content, nn2.Content)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user