Contextualise all the things ....

This commit is contained in:
Micheal Wilkinson
2026-03-17 21:59:40 +00:00
parent dd718c9a73
commit 5c53e7e968
4 changed files with 30 additions and 25 deletions

View File

@@ -1,6 +1,7 @@
package repository
import (
"context"
"fmt"
"time"
@@ -18,7 +19,7 @@ func NewNoteRepository(store NoteStore) *NoteRepository {
}
// CreateNote creates a new note with the given content.
func (r *NoteRepository) CreateNote(content string) (models.Note, error) {
func (r *NoteRepository) CreateNote(ctx context.Context, content string) (models.Note, error) {
if content == "" {
return models.Note{}, fmt.Errorf("content cannot be empty")
}
@@ -26,34 +27,34 @@ func (r *NoteRepository) CreateNote(content string) (models.Note, error) {
Content: content,
LastUpdate: time.Now(),
}
return r.store.SaveNote(note)
return r.store.SaveNote(ctx, note)
}
// GetNote retrieves a note by its ID.
func (r *NoteRepository) GetNote(id int) (models.Note, error) {
return r.store.GetNoteByID(id)
func (r *NoteRepository) GetNote(ctx context.Context, id int) (models.Note, error) {
return r.store.GetNoteByID(ctx, id)
}
// ListNotes retrieves all notes.
func (r *NoteRepository) ListNotes() ([]models.Note, error) {
return r.store.GetAllNotes()
func (r *NoteRepository) ListNotes(ctx context.Context) ([]models.Note, error) {
return r.store.GetAllNotes(ctx)
}
// UpdateNote updates the content of an existing note.
func (r *NoteRepository) UpdateNote(id int, content string) (models.Note, error) {
func (r *NoteRepository) UpdateNote(ctx context.Context, id int, content string) (models.Note, error) {
if content == "" {
return models.Note{}, fmt.Errorf("content cannot be empty")
}
note, err := r.store.GetNoteByID(id)
note, err := r.store.GetNoteByID(ctx, id)
if err != nil {
return models.Note{}, err
}
note.Content = content
note.LastUpdate = time.Now()
return r.store.SaveNote(note)
return r.store.SaveNote(ctx, note)
}
// DeleteNote deletes a note by its ID.
func (r *NoteRepository) DeleteNote(id int) error {
return r.store.DeleteNoteByID(id)
func (r *NoteRepository) DeleteNote(ctx context.Context, id int) error {
return r.store.DeleteNoteByID(ctx, id)
}

View File

@@ -4,6 +4,7 @@ import (
"testing"
"testing/synctest"
"context"
"fmt"
"time"
@@ -49,7 +50,7 @@ func TestCreateNote(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
note, err := repo.CreateNote(tc.content)
note, err := repo.CreateNote(context.Background(), tc.content)
if tc.expectedError {
if err == nil {
t.Errorf("expected an error but got none")
@@ -118,7 +119,7 @@ func TestGetNotes(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
note, err := repo.GetNote(tc.id)
note, err := repo.GetNote(context.Background(), tc.id)
if tc.expectedError {
if err == nil {
t.Errorf("expected an error but got none")
@@ -172,7 +173,7 @@ func TestListNotes(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
store := &mockNoteStore{Notes: tc.notes}
repo := repository.NewNoteRepository(store)
notes, err := repo.ListNotes()
notes, err := repo.ListNotes(context.Background())
if tc.expectedError {
if err == nil {
t.Errorf("expected an error but got none")
@@ -241,7 +242,7 @@ func TestUpdateNote(t *testing.T) {
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
note, err := repo.UpdateNote(tc.id, tc.content)
note, err := repo.UpdateNote(context.Background(), tc.id, tc.content)
if tc.expectedError {
if err == nil {
t.Errorf("expected an error but got none")
@@ -304,7 +305,7 @@ func TestDeleteNote(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
store := &mockNoteStore{Notes: tc.setupNotes}
repo := repository.NewNoteRepository(store)
err := repo.DeleteNote(tc.id)
err := repo.DeleteNote(context.Background(), tc.id)
if tc.expectedError {
if err == nil {
t.Errorf("expected an error but got none")
@@ -314,7 +315,7 @@ func TestDeleteNote(t *testing.T) {
t.Errorf("unexpected error: %v", err)
}
}
notes, _ := repo.ListNotes()
notes, _ := repo.ListNotes(context.Background())
if len(notes) != len(tc.expectedNotes) {
t.Errorf("expected %d notes but got %d", len(tc.expectedNotes), len(notes))
}

View File

@@ -1,17 +1,18 @@
package repository
import (
"context"
"git.hrafn.xyz/aether/notes/internal/models"
)
// NoteStore defines the interface for persisting and retrieving notes.
type NoteStore interface {
// SaveNote saves a note to the store and returns the saved note.
SaveNote(models.Note) (models.Note, error)
SaveNote(context.Context, models.Note) (models.Note, error)
// GetNoteByID retrieves a note by its ID.
GetNoteByID(int) (models.Note, error)
GetNoteByID(context.Context, int) (models.Note, error)
// GetAllNotes retrieves all notes from the store.
GetAllNotes() ([]models.Note, error)
GetAllNotes(context.Context) ([]models.Note, error)
// DeleteNoteByID deletes a note by its ID.
DeleteNoteByID(int) error
DeleteNoteByID(context.Context, int) error
}

View File

@@ -1,6 +1,8 @@
package repository_test
import (
// Context imported for interface matching but not used
"context"
"fmt"
"git.hrafn.xyz/aether/notes/internal/models"
@@ -10,7 +12,7 @@ type mockNoteStore struct {
Notes []models.Note
}
func (m *mockNoteStore) SaveNote(note models.Note) (models.Note, error) {
func (m *mockNoteStore) SaveNote(ctx context.Context, note models.Note) (models.Note, error) {
// Save a note
if note.ID == 0 {
note.ID = len(m.Notes) + 1
@@ -26,7 +28,7 @@ func (m *mockNoteStore) SaveNote(note models.Note) (models.Note, error) {
return note, nil
}
func (m *mockNoteStore) GetNoteByID(id int) (models.Note, error) {
func (m *mockNoteStore) GetNoteByID(ctx context.Context, id int) (models.Note, error) {
note, _, err := m.getNoteAndIndexByID(id)
if err != nil {
return models.Note{}, err
@@ -43,11 +45,11 @@ func (m *mockNoteStore) getNoteAndIndexByID(id int) (models.Note, int, error) {
return models.Note{}, -1, fmt.Errorf("note with ID %d not found", id)
}
func (m *mockNoteStore) GetAllNotes() ([]models.Note, error) {
func (m *mockNoteStore) GetAllNotes(ctx context.Context) ([]models.Note, error) {
return m.Notes, nil
}
func (m *mockNoteStore) DeleteNoteByID(id int) error {
func (m *mockNoteStore) DeleteNoteByID(ctx context.Context, id int) error {
_, index, err := m.getNoteAndIndexByID(id)
if err != nil {
return err