ref(create_note): update handler to use uow with dbcontext
This commit is contained in:
parent
566ba834d4
commit
975986a602
3 changed files with 16 additions and 19 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue