From 25371400536bb4b17ff4a072f0969d630450d6f2 Mon Sep 17 00:00:00 2001 From: Kristian Borgwarth <10348902@pm.me> Date: Fri, 3 Apr 2026 21:03:36 +0200 Subject: [PATCH] ref(handlers): uow passsed to method --- cmd/main.go | 2 +- core/handlers/create_note_handler.go | 20 ++++++------ core/handlers/initialize_handler.go | 6 +++- core/server/server.go | 7 ++++- persistence/db_migrator.go | 11 +++---- .../create_note_handler_test.go | 31 ++++++++++--------- test/test_integration/main_test.go | 7 +---- 7 files changed, 44 insertions(+), 40 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 4b4890f..658f0f5 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -12,7 +12,7 @@ import ( func main() { server := server.NewServer() - server.Register("initialize", handlers.InitializeHandler{}) + server.RegisterHandler("initialize", handlers.NewInitializeHandler()) 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 a61c7fe..cb78af9 100644 --- a/core/handlers/create_note_handler.go +++ b/core/handlers/create_note_handler.go @@ -18,15 +18,13 @@ type createNoteCommand struct { Vars map[string]string `json:"vars"` } -type CreateNoteHandler struct { - uow *repositories.UnitOfWork +type CreateNoteHandler struct{} + +func NewCreateNoteHandler() *CreateNoteHandler { + return &CreateNoteHandler{} } -func NewCreateNoteHandler(uow *repositories.UnitOfWork) *CreateNoteHandler { - return &CreateNoteHandler{uow: uow} -} - -func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (any, error) { +func (h *CreateNoteHandler) Handle(ctx context.Context, uow *repositories.UnitOfWork, raw json.RawMessage) (any, error) { var cmd createNoteCommand if err := json.Unmarshal(raw, &cmd); err != nil { @@ -45,12 +43,12 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (an return nil, err } - tx, err := h.uow.Begin() + tx, err := uow.Begin() if err != nil { return nil, err } - defer h.uow.Rollback() + defer uow.Rollback() tagRepo := repositories.NewTagRepository(tx) noteRepo := repositories.NewNoteRepository(tx) @@ -74,9 +72,9 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (an return nil, err } - h.uow.FileStore.Stage(cmd.Path, data) + uow.FileStore.Stage(cmd.Path, data) - if err = h.uow.Commit(); err != nil { + if err = uow.Commit(); err != nil { return nil, err } diff --git a/core/handlers/initialize_handler.go b/core/handlers/initialize_handler.go index 6e33f12..ee43b28 100644 --- a/core/handlers/initialize_handler.go +++ b/core/handlers/initialize_handler.go @@ -12,6 +12,10 @@ type initializeCommand struct { type InitializeHandler struct{} +func NewInitializeHandler() *InitializeHandler { + return &InitializeHandler{} +} + func (h InitializeHandler) Handle(ctx context.Context, raw json.RawMessage) (any, error) { var params initializeCommand @@ -19,7 +23,7 @@ func (h InitializeHandler) Handle(ctx context.Context, raw json.RawMessage) (any return nil, err } - err := persistence.InitializeIndex(params.VaultPath) + _, err := persistence.InitializeIndex(params.VaultPath) if err != nil { return nil, err } diff --git a/core/server/server.go b/core/server/server.go index 6561a5d..c7aa004 100644 --- a/core/server/server.go +++ b/core/server/server.go @@ -4,6 +4,7 @@ package server import ( "bufio" "context" + "database/sql" "encoding/json" "fmt" "io" @@ -13,6 +14,7 @@ import ( ) type Server struct { + db *sql.DB handlers map[string]handlers.Handler } @@ -22,7 +24,7 @@ func NewServer() *Server { } } -func (s *Server) Register(method string, handler handlers.Handler) { +func (s *Server) RegisterHandler(method string, handler handlers.Handler) { s.handlers[method] = handler } @@ -73,6 +75,9 @@ func (s *Server) handle(w io.Writer, req rpc.Request) { s.respond(w, req.ID, result, nil) } +func (s *Server) initalize(){ +} + func (s *Server) respond(w io.Writer, id *int, result any, err *rpc.Error) { resultJSON, _ := json.Marshal(result) resp := rpc.Response{ diff --git a/persistence/db_migrator.go b/persistence/db_migrator.go index 2de7a75..1ddc700 100644 --- a/persistence/db_migrator.go +++ b/persistence/db_migrator.go @@ -12,12 +12,12 @@ import ( //go:embed migrations/*.sql var migrationsFS embed.FS -func InitializeIndex(vaultPath string) error { +func InitializeIndex(vaultPath string) (*sql.DB, error) { indexDir := filepath.Join(vaultPath, ".index") if err := os.MkdirAll(indexDir, 0755); err != nil { slog.Error("failed to create index directory", "error", err) - return err + return nil, err } dbPath := filepath.Join(indexDir, "index.db") @@ -25,17 +25,16 @@ func InitializeIndex(vaultPath string) error { db, err := sql.Open("sqlite", dbPath) if err != nil { slog.Error("failed to open database", "error", err) - return err + return nil, err } - defer db.Close() if err := applyMigrations(db); err != nil { slog.Error("failed to apply migrations", "error", err) - return err + return nil, err } - return nil + return db, nil } func applyMigrations(db *sql.DB) error { diff --git a/test/test_integration/create_note_handler_test.go b/test/test_integration/create_note_handler_test.go index 093539b..02db85c 100644 --- a/test/test_integration/create_note_handler_test.go +++ b/test/test_integration/create_note_handler_test.go @@ -12,14 +12,11 @@ import ( "github.com/stretchr/testify/require" ) -func newCreateNoteHandler() *handlers.CreateNoteHandler { - uow := repositories.NewUnitOfWork(Fixture.DB) - return handlers.NewCreateNoteHandler(uow) -} func TestCreateNoteHandler_NoTemplate_CreatesNoteFileAndReturnsPath(t *testing.T) { // Arrange - handler := newCreateNoteHandler() + handler := handlers.NewCreateNoteHandler() + uow := repositories.NewUnitOfWork(Fixture.DB) notePath := filepath.Join(t.TempDir(), "my-note.md") params, _ := json.Marshal(map[string]any{ "title": "My Note", @@ -27,7 +24,7 @@ func TestCreateNoteHandler_NoTemplate_CreatesNoteFileAndReturnsPath(t *testing.T }) // Act - result, err := handler.Handle(Fixture.TestContext, params) + result, err := handler.Handle(Fixture.TestContext, uow, params) // Assert require.NoError(t, err) @@ -43,7 +40,8 @@ func TestCreateNoteHandler_NoTemplate_CreatesNoteFileAndReturnsPath(t *testing.T func TestCreateNoteHandler_WithTemplate_CreatesNoteFileWithTagsAndReturnsPath(t *testing.T) { // Arrange - handler := newCreateNoteHandler() + handler := handlers.NewCreateNoteHandler() + uow := repositories.NewUnitOfWork(Fixture.DB) dir := t.TempDir() templatePath := filepath.Join(dir, "template.md") @@ -57,7 +55,7 @@ func TestCreateNoteHandler_WithTemplate_CreatesNoteFileWithTagsAndReturnsPath(t }) // Act - result, err := handler.Handle(Fixture.TestContext, params) + result, err := handler.Handle(Fixture.TestContext, uow, params) // Assert require.NoError(t, err) @@ -86,17 +84,20 @@ func TestCreateNoteHandler_WithTemplate_CreatesNoteFileWithTagsAndReturnsPath(t func TestCreateNoteHandler_DuplicateSlug_Upserts(t *testing.T) { // Arrange dir := t.TempDir() + handler := handlers.NewCreateNoteHandler() + uowInit := repositories.NewUnitOfWork(Fixture.DB) + secondUow := repositories.NewUnitOfWork(Fixture.DB) path1 := filepath.Join(dir, "dup-note.md") params1, _ := json.Marshal(map[string]any{"title": "Dup Note", "path": path1}) - _, err := newCreateNoteHandler().Handle(Fixture.TestContext, params1) + _, err := handler.Handle(Fixture.TestContext, uowInit, 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 = newCreateNoteHandler().Handle(Fixture.TestContext, params2) + _, err = handler.Handle(Fixture.TestContext, secondUow, params2) // Assert require.NoError(t, err) @@ -112,10 +113,11 @@ func TestCreateNoteHandler_DuplicateSlug_Upserts(t *testing.T) { func TestCreateNoteHandler_InvalidJSON_ReturnsError(t *testing.T) { // Arrange - handler := newCreateNoteHandler() + handler := handlers.NewCreateNoteHandler() + uow := repositories.NewUnitOfWork(Fixture.DB) // Act - _, err := handler.Handle(Fixture.TestContext, json.RawMessage(`{invalid json}`)) + _, err := handler.Handle(Fixture.TestContext, uow, json.RawMessage(`{invalid json}`)) // Assert assert.Error(t, err) @@ -123,7 +125,8 @@ func TestCreateNoteHandler_InvalidJSON_ReturnsError(t *testing.T) { func TestCreateNoteHandler_NonExistentTemplatePath_ReturnsError(t *testing.T) { // Arrange - handler := newCreateNoteHandler() + handler := handlers.NewCreateNoteHandler() + uow := repositories.NewUnitOfWork(Fixture.DB) params, _ := json.Marshal(map[string]any{ "title": "Ghost Note", "path": filepath.Join(t.TempDir(), "ghost.md"), @@ -131,7 +134,7 @@ func TestCreateNoteHandler_NonExistentTemplatePath_ReturnsError(t *testing.T) { }) // Act - _, err := handler.Handle(Fixture.TestContext, params) + _, err := handler.Handle(Fixture.TestContext, uow, params) // Assert assert.Error(t, err) diff --git a/test/test_integration/main_test.go b/test/test_integration/main_test.go index 363ed8f..c14b459 100644 --- a/test/test_integration/main_test.go +++ b/test/test_integration/main_test.go @@ -22,18 +22,13 @@ type DBFixture struct { func NewDBFixture() *DBFixture { vaultPath := os.TempDir() - err := persistence.InitializeIndex(vaultPath) + db, err := persistence.InitializeIndex(vaultPath) if err != nil { panic(err) } dbPath := filepath.Join(os.TempDir(), ".index", "index.db") - db, err := sql.Open("sqlite", dbPath) - if err != nil { - panic(err) - } - return &DBFixture{ DB: db, DBPath: dbPath,