From f3e11149ffebf4e20427d15228e0902140cf9316 Mon Sep 17 00:00:00 2001 From: Kristian Borgwarth <10348902@pm.me> Date: Sun, 12 Apr 2026 02:32:14 +0200 Subject: [PATCH] ref(repo): improve repo di --- core/handlers/note/create_note_handler.go | 4 ++-- core/handlers/note/save_note.go | 10 +++++---- core/services/doc.go | 2 ++ core/services/tag_service.go | 1 + persistence/repositories/note_repository.go | 24 ++++++++++----------- 5 files changed, 22 insertions(+), 19 deletions(-) create mode 100644 core/services/doc.go create mode 100644 core/services/tag_service.go diff --git a/core/handlers/note/create_note_handler.go b/core/handlers/note/create_note_handler.go index 72866a2..a99c319 100644 --- a/core/handlers/note/create_note_handler.go +++ b/core/handlers/note/create_note_handler.go @@ -48,7 +48,7 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (an defer h.uow.Rollback() tagRepo := repositories.NewTagRepository(dbCtx) - noteRepo := repositories.NewNoteRepository(dbCtx) + noteRepo := repositories.NewNoteRepository() dbTags, err := tagRepo.GetByNames(ctx, template.FrontMatter.Tags) if err != nil { @@ -77,7 +77,7 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (an note := models.CreateNote(notePath, cmd.Title, template.Slug) - if err = noteRepo.Insert(ctx, note); err != nil { + if err = noteRepo.Insert(ctx, dbCtx, note); err != nil { return nil, err } diff --git a/core/handlers/note/save_note.go b/core/handlers/note/save_note.go index 405f2aa..d1eac64 100644 --- a/core/handlers/note/save_note.go +++ b/core/handlers/note/save_note.go @@ -7,6 +7,7 @@ import ( filehandling "github.com/KristianJBorgwarth/dendrite.daemon/core/file_handling" "github.com/KristianJBorgwarth/dendrite.daemon/core/models" + "github.com/KristianJBorgwarth/dendrite.daemon/persistence" "github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories" ) @@ -42,18 +43,18 @@ func (h *SaveNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (any, } defer h.uow.Rollback() - noteRepo := repositories.NewNoteRepository(tx) + noteRepo := repositories.NewNoteRepository() linkRepo := repositories.NewLinkRepository(tx) tagRepo := repositories.NewTagRepository(tx) - note, err := noteRepo.GetBySlug(ctx, file.Slug) + note, err := noteRepo.GetBySlug(ctx, tx, file.Slug) if err != nil { slog.Debug("Failed to get note by slug", "slug", file.Slug, "error", err) return nil, err } if note == nil { - if err := h.handleNewNote(ctx, noteRepo, linkRepo, tagRepo, file); err != nil { + if err := h.handleNewNote(ctx, tx, noteRepo, linkRepo, tagRepo, file); err != nil { return nil, err } } else { @@ -70,6 +71,7 @@ func (h *SaveNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (any, func (h *SaveNoteHandler) handleNewNote( ctx context.Context, + IDbContext persistence.IDbContext, noteRepo repositories.NoteRepository, linkRepo repositories.ILinkRepository, tagRepo repositories.ITagRepository, @@ -78,7 +80,7 @@ func (h *SaveNoteHandler) handleNewNote( note := models.CreateNote(file.Path, file.Title, file.Slug) slog.Debug("EXTRACTED FILE", "path", file.Path, "title", file.Title, "slug", file.Slug, "links", file.ExtractedLinks, "tags", file.FrontMatter.Tags) - if err := noteRepo.Insert(ctx, note); err != nil { + if err := noteRepo.Insert(ctx, IDbContext, note); err != nil { slog.Debug("Failed to insert new note", "noteID", note.ID(), "error", err) return err } diff --git a/core/services/doc.go b/core/services/doc.go new file mode 100644 index 0000000..13e0de6 --- /dev/null +++ b/core/services/doc.go @@ -0,0 +1,2 @@ +// Package services provides the implementation of the services defined in the API specification. +package services diff --git a/core/services/tag_service.go b/core/services/tag_service.go new file mode 100644 index 0000000..c75b6f3 --- /dev/null +++ b/core/services/tag_service.go @@ -0,0 +1 @@ +package diff --git a/persistence/repositories/note_repository.go b/persistence/repositories/note_repository.go index a690b1d..c04edd9 100644 --- a/persistence/repositories/note_repository.go +++ b/persistence/repositories/note_repository.go @@ -10,19 +10,17 @@ import ( ) type NoteRepository interface { - Insert(ctx context.Context, note *models.Note) error - GetBySlug(ctx context.Context, slug string) (*models.Note, error) + Insert(ctx context.Context, dbContext persistence.IDbContext, note *models.Note) error + GetBySlug(ctx context.Context, dbContext persistence.IDbContext, slug string) (*models.Note, error) } -type noteRepository struct { - dbContext persistence.IDbContext +type noteRepository struct{} + +func NewNoteRepository() NoteRepository { + return ¬eRepository{} } -func NewNoteRepository(ctx persistence.IDbContext) NoteRepository { - return ¬eRepository{dbContext: ctx} -} - -func (r *noteRepository) Insert(ctx context.Context, note *models.Note) error { +func (r *noteRepository) Insert(ctx context.Context, dbContext persistence.IDbContext, note *models.Note) error { query := ` INSERT INTO note (id, title, path, slug, created_at, updated_at) VALUES (?, ?, ?, ?, datetime('now'), datetime('now')) @@ -30,19 +28,19 @@ func (r *noteRepository) Insert(ctx context.Context, note *models.Note) error { SET title = EXCLUDED.title, path = EXCLUDED.path; ` - _, err := r.dbContext.ExecContext(ctx, query, note.ID(), note.Title(), note.Path(), note.Slug()) + _, err := dbContext.ExecContext(ctx, query, note.ID(), note.Title(), note.Path(), note.Slug()) return err } -func (r *noteRepository) GetBySlug(ctx context.Context, slug string) (*models.Note, error) { +func (r *noteRepository) GetBySlug(ctx context.Context, dbContext persistence.IDbContext, slug string) (*models.Note, error) { query := `SELECT id, title, path, slug, created_at, updated_at FROM note WHERE slug = ?` - row := r.dbContext.QueryRowContext(ctx, query, slug) + row := dbContext.QueryRowContext(ctx, query, slug) var id, title, path, createdAt, updatedAt string err := row.Scan(&id, &title, &path, &slug, &createdAt, &updatedAt) if err != nil { if errors.Is(err, sql.ErrNoRows) { - return nil, nil + return nil, nil } return nil, err }