Contextualise all the things ....
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package repository
|
package repository
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -18,7 +19,7 @@ func NewNoteRepository(store NoteStore) *NoteRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateNote creates a new note with the given content.
|
// 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 == "" {
|
if content == "" {
|
||||||
return models.Note{}, fmt.Errorf("content cannot be empty")
|
return models.Note{}, fmt.Errorf("content cannot be empty")
|
||||||
}
|
}
|
||||||
@@ -26,34 +27,34 @@ func (r *NoteRepository) CreateNote(content string) (models.Note, error) {
|
|||||||
Content: content,
|
Content: content,
|
||||||
LastUpdate: time.Now(),
|
LastUpdate: time.Now(),
|
||||||
}
|
}
|
||||||
return r.store.SaveNote(note)
|
return r.store.SaveNote(ctx, note)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetNote retrieves a note by its ID.
|
// GetNote retrieves a note by its ID.
|
||||||
func (r *NoteRepository) GetNote(id int) (models.Note, error) {
|
func (r *NoteRepository) GetNote(ctx context.Context, id int) (models.Note, error) {
|
||||||
return r.store.GetNoteByID(id)
|
return r.store.GetNoteByID(ctx, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListNotes retrieves all notes.
|
// ListNotes retrieves all notes.
|
||||||
func (r *NoteRepository) ListNotes() ([]models.Note, error) {
|
func (r *NoteRepository) ListNotes(ctx context.Context) ([]models.Note, error) {
|
||||||
return r.store.GetAllNotes()
|
return r.store.GetAllNotes(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateNote updates the content of an existing note.
|
// 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 == "" {
|
if content == "" {
|
||||||
return models.Note{}, fmt.Errorf("content cannot be empty")
|
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 {
|
if err != nil {
|
||||||
return models.Note{}, err
|
return models.Note{}, err
|
||||||
}
|
}
|
||||||
note.Content = content
|
note.Content = content
|
||||||
note.LastUpdate = time.Now()
|
note.LastUpdate = time.Now()
|
||||||
return r.store.SaveNote(note)
|
return r.store.SaveNote(ctx, note)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteNote deletes a note by its ID.
|
// DeleteNote deletes a note by its ID.
|
||||||
func (r *NoteRepository) DeleteNote(id int) error {
|
func (r *NoteRepository) DeleteNote(ctx context.Context, id int) error {
|
||||||
return r.store.DeleteNoteByID(id)
|
return r.store.DeleteNoteByID(ctx, id)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"testing/synctest"
|
"testing/synctest"
|
||||||
|
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -49,7 +50,7 @@ func TestCreateNote(t *testing.T) {
|
|||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
synctest.Test(t, 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 tc.expectedError {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("expected an error but got none")
|
t.Errorf("expected an error but got none")
|
||||||
@@ -118,7 +119,7 @@ func TestGetNotes(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
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 tc.expectedError {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("expected an error but got none")
|
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) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
store := &mockNoteStore{Notes: tc.notes}
|
store := &mockNoteStore{Notes: tc.notes}
|
||||||
repo := repository.NewNoteRepository(store)
|
repo := repository.NewNoteRepository(store)
|
||||||
notes, err := repo.ListNotes()
|
notes, err := repo.ListNotes(context.Background())
|
||||||
if tc.expectedError {
|
if tc.expectedError {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("expected an error but got none")
|
t.Errorf("expected an error but got none")
|
||||||
@@ -241,7 +242,7 @@ func TestUpdateNote(t *testing.T) {
|
|||||||
for _, tc := range testcases {
|
for _, tc := range testcases {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
synctest.Test(t, 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 tc.expectedError {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("expected an error but got none")
|
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) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
store := &mockNoteStore{Notes: tc.setupNotes}
|
store := &mockNoteStore{Notes: tc.setupNotes}
|
||||||
repo := repository.NewNoteRepository(store)
|
repo := repository.NewNoteRepository(store)
|
||||||
err := repo.DeleteNote(tc.id)
|
err := repo.DeleteNote(context.Background(), tc.id)
|
||||||
if tc.expectedError {
|
if tc.expectedError {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("expected an error but got none")
|
t.Errorf("expected an error but got none")
|
||||||
@@ -314,7 +315,7 @@ func TestDeleteNote(t *testing.T) {
|
|||||||
t.Errorf("unexpected error: %v", err)
|
t.Errorf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
notes, _ := repo.ListNotes()
|
notes, _ := repo.ListNotes(context.Background())
|
||||||
if len(notes) != len(tc.expectedNotes) {
|
if len(notes) != len(tc.expectedNotes) {
|
||||||
t.Errorf("expected %d notes but got %d", len(tc.expectedNotes), len(notes))
|
t.Errorf("expected %d notes but got %d", len(tc.expectedNotes), len(notes))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,18 @@
|
|||||||
package repository
|
package repository
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"git.hrafn.xyz/aether/notes/internal/models"
|
"git.hrafn.xyz/aether/notes/internal/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NoteStore defines the interface for persisting and retrieving notes.
|
// NoteStore defines the interface for persisting and retrieving notes.
|
||||||
type NoteStore interface {
|
type NoteStore interface {
|
||||||
// SaveNote saves a note to the store and returns the saved note.
|
// 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 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 retrieves all notes from the store.
|
||||||
GetAllNotes() ([]models.Note, error)
|
GetAllNotes(context.Context) ([]models.Note, error)
|
||||||
// DeleteNoteByID deletes a note by its ID.
|
// DeleteNoteByID deletes a note by its ID.
|
||||||
DeleteNoteByID(int) error
|
DeleteNoteByID(context.Context, int) error
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package repository_test
|
package repository_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
// Context imported for interface matching but not used
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"git.hrafn.xyz/aether/notes/internal/models"
|
"git.hrafn.xyz/aether/notes/internal/models"
|
||||||
@@ -10,7 +12,7 @@ type mockNoteStore struct {
|
|||||||
Notes []models.Note
|
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
|
// Save a note
|
||||||
if note.ID == 0 {
|
if note.ID == 0 {
|
||||||
note.ID = len(m.Notes) + 1
|
note.ID = len(m.Notes) + 1
|
||||||
@@ -26,7 +28,7 @@ func (m *mockNoteStore) SaveNote(note models.Note) (models.Note, error) {
|
|||||||
return note, nil
|
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)
|
note, _, err := m.getNoteAndIndexByID(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return models.Note{}, err
|
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)
|
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
|
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)
|
_, index, err := m.getNoteAndIndexByID(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
Reference in New Issue
Block a user