From 975986a6020e150c29d91c15795d6135e3241be4 Mon Sep 17 00:00:00 2001 From: Kristian Borgwarth <10348902@pm.me> Date: Sat, 4 Apr 2026 00:19:09 +0200 Subject: [PATCH] ref(create_note): update handler to use uow with dbcontext --- cmd/main.go | 1 + core/handlers/create_note_handler.go | 10 ++++---- .../create_note_handler_test.go | 24 ++++++++----------- 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 658f0f5..b2e2e3d 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -13,6 +13,7 @@ func main() { server := server.NewServer() server.RegisterHandler("initialize", handlers.NewInitializeHandler()) + server.RegisterHandler("createNote", handlers.NewCreateNoteHandler()) if err := server.Run(os.Stdin, os.Stdout); err != nil { slog.Error("server error", "error", err) diff --git a/core/handlers/create_note_handler.go b/core/handlers/create_note_handler.go index 18e17ea..43a2199 100644 --- a/core/handlers/create_note_handler.go +++ b/core/handlers/create_note_handler.go @@ -26,7 +26,7 @@ func NewCreateNoteHandler() *CreateNoteHandler { return &CreateNoteHandler{repositories.NewUnitOfWork()} } -func (h *CreateNoteHandler) Handle(ctx context.Context, uow *repositories.UnitOfWork, raw json.RawMessage) (any, error) { +func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (any, error) { var cmd createNoteCommand if err := json.Unmarshal(raw, &cmd); err != nil { @@ -45,12 +45,12 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, uow *repositories.UnitOf return nil, err } - tx, err := uow.Begin() + tx, err := h.uow.Begin() if err != nil { return nil, err } - defer uow.Rollback() + defer h.uow.Rollback() tagRepo := repositories.NewTagRepository(tx) noteRepo := repositories.NewNoteRepository(tx) @@ -74,9 +74,9 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, uow *repositories.UnitOf return nil, err } - uow.FileStore.Stage(cmd.Path, data) + h.uow.FileStore.Stage(cmd.Path, data) - if err = uow.Commit(); err != nil { + if err = h.uow.Commit(); err != nil { return nil, err } diff --git a/test/test_integration/create_note_handler_test.go b/test/test_integration/create_note_handler_test.go index 20787d2..28f085e 100644 --- a/test/test_integration/create_note_handler_test.go +++ b/test/test_integration/create_note_handler_test.go @@ -7,16 +7,14 @@ import ( "testing" "github.com/KristianJBorgwarth/dendrite.daemon/core/handlers" - "github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) - func TestCreateNoteHandler_NoTemplate_CreatesNoteFileAndReturnsPath(t *testing.T) { // Arrange handler := handlers.NewCreateNoteHandler() - uow := repositories.NewUnitOfWork() + notePath := filepath.Join(t.TempDir(), "my-note.md") params, _ := json.Marshal(map[string]any{ "title": "My Note", @@ -24,7 +22,7 @@ func TestCreateNoteHandler_NoTemplate_CreatesNoteFileAndReturnsPath(t *testing.T }) // Act - result, err := handler.Handle(Fixture.TestContext, uow, params) + result, err := handler.Handle(Fixture.TestContext, params) // Assert require.NoError(t, err) @@ -41,7 +39,6 @@ func TestCreateNoteHandler_NoTemplate_CreatesNoteFileAndReturnsPath(t *testing.T func TestCreateNoteHandler_WithTemplate_CreatesNoteFileWithTagsAndReturnsPath(t *testing.T) { // Arrange handler := handlers.NewCreateNoteHandler() - uow := repositories.NewUnitOfWork() dir := t.TempDir() templatePath := filepath.Join(dir, "template.md") @@ -55,7 +52,7 @@ func TestCreateNoteHandler_WithTemplate_CreatesNoteFileWithTagsAndReturnsPath(t }) // Act - result, err := handler.Handle(Fixture.TestContext, uow, params) + result, err := handler.Handle(Fixture.TestContext, params) // Assert require.NoError(t, err) @@ -85,19 +82,18 @@ func TestCreateNoteHandler_DuplicateSlug_Upserts(t *testing.T) { // Arrange dir := t.TempDir() handler := handlers.NewCreateNoteHandler() - uowInit := repositories.NewUnitOfWork() - secondUow := repositories.NewUnitOfWork() + path1 := filepath.Join(dir, "dup-note.md") params1, _ := json.Marshal(map[string]any{"title": "Dup Note", "path": path1}) - _, err := handler.Handle(Fixture.TestContext, uowInit, params1) + _, err := handler.Handle(Fixture.TestContext, params1) require.NoError(t, err) path2 := filepath.Join(dir, "dup-note-moved.md") params2, _ := json.Marshal(map[string]any{"title": "Dup Note", "path": path2}) // Act - _, err = handler.Handle(Fixture.TestContext, secondUow, params2) + _, err = handler.Handle(Fixture.TestContext, params2) // Assert require.NoError(t, err) @@ -114,10 +110,10 @@ func TestCreateNoteHandler_DuplicateSlug_Upserts(t *testing.T) { func TestCreateNoteHandler_InvalidJSON_ReturnsError(t *testing.T) { // Arrange handler := handlers.NewCreateNoteHandler() - uow := repositories.NewUnitOfWork() + // Act - _, err := handler.Handle(Fixture.TestContext, uow, json.RawMessage(`{invalid json}`)) + _, err := handler.Handle(Fixture.TestContext, json.RawMessage(`{invalid json}`)) // Assert assert.Error(t, err) @@ -126,7 +122,7 @@ func TestCreateNoteHandler_InvalidJSON_ReturnsError(t *testing.T) { func TestCreateNoteHandler_NonExistentTemplatePath_ReturnsError(t *testing.T) { // Arrange handler := handlers.NewCreateNoteHandler() - uow := repositories.NewUnitOfWork() + params, _ := json.Marshal(map[string]any{ "title": "Ghost Note", "path": filepath.Join(t.TempDir(), "ghost.md"), @@ -134,7 +130,7 @@ func TestCreateNoteHandler_NonExistentTemplatePath_ReturnsError(t *testing.T) { }) // Act - _, err := handler.Handle(Fixture.TestContext, uow, params) + _, err := handler.Handle(Fixture.TestContext, params) // Assert assert.Error(t, err)