From f8ba52e62eca44b5ee8b72d8955dce14a0f164d8 Mon Sep 17 00:00:00 2001 From: Kristian Borgwarth <10348902@pm.me> Date: Tue, 14 Apr 2026 18:26:38 +0200 Subject: [PATCH] feat(goto): added goto_note_handler --- cmd/main.go | 2 +- core/handlers/note/goto_note_handler.go | 13 +++++++++---- core/handlers/note/save_note_handler.go | 2 +- persistence/repositories/note_repository.go | 10 ++++++---- test/test_integration/create_note_handler_test.go | 3 ++- 5 files changed, 19 insertions(+), 11 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index a950bc3..0b59c28 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -22,7 +22,7 @@ func main() { uow := repositories.NewUnitOfWork(); linkRepo := repositories.NewLinkRepository(*persistence.NewReadContext()) tagRepo := repositories.NewTagRepository() - noteRepo := repositories.NewNoteRepository() + noteRepo := repositories.NewNoteRepository(*persistence.NewReadContext()) tagService := services.NewTagService(tagRepo) linkService := services.NewLinkService(linkRepo) noteService := services.NewNoteService(tagRepo, linkRepo, noteRepo) diff --git a/core/handlers/note/goto_note_handler.go b/core/handlers/note/goto_note_handler.go index 3c0838e..64a6639 100644 --- a/core/handlers/note/goto_note_handler.go +++ b/core/handlers/note/goto_note_handler.go @@ -1,6 +1,10 @@ package note -import "github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories" +import ( + "context" + + "github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories" +) type gotoNoteCommand struct { Slug string `json:"slug"` @@ -10,13 +14,14 @@ type gotoNoteResult struct { Path string `json:"path"` } -type GotoNoteHandler struct{ +type GotoNoteHandler struct { noteRepo repositories.INoteRepository } -func(h *GotoNoteHandler) Handle(command gotoNoteCommand) (gotoNoteResult, error) { - note, err := h.noteRepo.GetBySlug(command.Slug) +func (h *GotoNoteHandler) Handle(ctx context.Context, command gotoNoteCommand) (gotoNoteResult, error) { + note, err := h.noteRepo.GetBySlug(ctx, command.Slug) if err != nil { return gotoNoteResult{}, err } + return gotoNoteResult{Path: note.Path()}, nil } diff --git a/core/handlers/note/save_note_handler.go b/core/handlers/note/save_note_handler.go index cdd4e1e..6fb7534 100644 --- a/core/handlers/note/save_note_handler.go +++ b/core/handlers/note/save_note_handler.go @@ -54,7 +54,7 @@ func (h *SaveNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (any, } defer h.uow.Rollback() - note, err := h.noteRepo.GetBySlug(ctx, tx, file.Slug) + note, err := h.noteRepo.GetBySlug(ctx, file.Slug) if err != nil { slog.Debug("Failed to get note by slug", "slug", file.Slug, "error", err) return nil, err diff --git a/persistence/repositories/note_repository.go b/persistence/repositories/note_repository.go index b599966..5f8741f 100644 --- a/persistence/repositories/note_repository.go +++ b/persistence/repositories/note_repository.go @@ -12,10 +12,12 @@ import ( type INoteRepository interface { Insert(ctx context.Context, dbContext persistence.IDbContext, note *models.Note) error Update(ctx context.Context, dbContext persistence.IDbContext, noteID, path, title, slug string) error - GetBySlug(ctx context.Context, dbContext persistence.IDbContext, slug string) (*models.Note, error) + GetBySlug(ctx context.Context, slug string) (*models.Note, error) } -type noteRepository struct{} +type noteRepository struct{ + readDBContext persistence.ReadContext +} func NewNoteRepository(persistence.ReadContext) INoteRepository { return ¬eRepository{} @@ -46,9 +48,9 @@ func (r *noteRepository) Update(ctx context.Context, dbContext persistence.IDbCo return nil } -func (r *noteRepository) GetBySlug(ctx context.Context, dbContext persistence.IDbContext, slug string) (*models.Note, error) { +func (r *noteRepository) GetBySlug(ctx context.Context, slug string) (*models.Note, error) { query := `SELECT id, title, path, slug, created_at, updated_at FROM note WHERE slug = ?` - row := dbContext.QueryRowContext(ctx, query, slug) + row := r.readDBContext.QueryRowContext(ctx, query, slug) var id, title, path, createdAt, updatedAt string err := row.Scan(&id, &title, &path, &slug, &createdAt, &updatedAt) diff --git a/test/test_integration/create_note_handler_test.go b/test/test_integration/create_note_handler_test.go index fcbdeda..66a607d 100644 --- a/test/test_integration/create_note_handler_test.go +++ b/test/test_integration/create_note_handler_test.go @@ -8,6 +8,7 @@ import ( "github.com/KristianJBorgwarth/dendrite.daemon/core/handlers/note" "github.com/KristianJBorgwarth/dendrite.daemon/core/services" + "github.com/KristianJBorgwarth/dendrite.daemon/persistence" "github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -17,7 +18,7 @@ func newCreateNoteHandler() *note.CreateNoteHandler { return note.NewCreateNoteHandler( repositories.NewUnitOfWork(), services.NewTagService(repositories.NewTagRepository()), - repositories.NewNoteRepository(), + repositories.NewNoteRepository(*persistence.NewReadContext()), ) }