ref(repo): improve repo di

This commit is contained in:
Kristian Borgwarth 2026-04-12 02:32:14 +02:00
parent 754a4c5435
commit f3e11149ff
5 changed files with 22 additions and 19 deletions

View file

@ -48,7 +48,7 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (an
defer h.uow.Rollback() defer h.uow.Rollback()
tagRepo := repositories.NewTagRepository(dbCtx) tagRepo := repositories.NewTagRepository(dbCtx)
noteRepo := repositories.NewNoteRepository(dbCtx) noteRepo := repositories.NewNoteRepository()
dbTags, err := tagRepo.GetByNames(ctx, template.FrontMatter.Tags) dbTags, err := tagRepo.GetByNames(ctx, template.FrontMatter.Tags)
if err != nil { 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) 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 return nil, err
} }

View file

@ -7,6 +7,7 @@ import (
filehandling "github.com/KristianJBorgwarth/dendrite.daemon/core/file_handling" filehandling "github.com/KristianJBorgwarth/dendrite.daemon/core/file_handling"
"github.com/KristianJBorgwarth/dendrite.daemon/core/models" "github.com/KristianJBorgwarth/dendrite.daemon/core/models"
"github.com/KristianJBorgwarth/dendrite.daemon/persistence"
"github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories" "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() defer h.uow.Rollback()
noteRepo := repositories.NewNoteRepository(tx) noteRepo := repositories.NewNoteRepository()
linkRepo := repositories.NewLinkRepository(tx) linkRepo := repositories.NewLinkRepository(tx)
tagRepo := repositories.NewTagRepository(tx) tagRepo := repositories.NewTagRepository(tx)
note, err := noteRepo.GetBySlug(ctx, file.Slug) note, err := noteRepo.GetBySlug(ctx, tx, file.Slug)
if err != nil { if err != nil {
slog.Debug("Failed to get note by slug", "slug", file.Slug, "error", err) slog.Debug("Failed to get note by slug", "slug", file.Slug, "error", err)
return nil, err return nil, err
} }
if note == nil { 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 return nil, err
} }
} else { } else {
@ -70,6 +71,7 @@ func (h *SaveNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (any,
func (h *SaveNoteHandler) handleNewNote( func (h *SaveNoteHandler) handleNewNote(
ctx context.Context, ctx context.Context,
IDbContext persistence.IDbContext,
noteRepo repositories.NoteRepository, noteRepo repositories.NoteRepository,
linkRepo repositories.ILinkRepository, linkRepo repositories.ILinkRepository,
tagRepo repositories.ITagRepository, tagRepo repositories.ITagRepository,
@ -78,7 +80,7 @@ func (h *SaveNoteHandler) handleNewNote(
note := models.CreateNote(file.Path, file.Title, file.Slug) 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) 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) slog.Debug("Failed to insert new note", "noteID", note.ID(), "error", err)
return err return err
} }

2
core/services/doc.go Normal file
View file

@ -0,0 +1,2 @@
// Package services provides the implementation of the services defined in the API specification.
package services

View file

@ -0,0 +1 @@
package

View file

@ -10,19 +10,17 @@ import (
) )
type NoteRepository interface { type NoteRepository interface {
Insert(ctx context.Context, note *models.Note) error Insert(ctx context.Context, dbContext persistence.IDbContext, note *models.Note) error
GetBySlug(ctx context.Context, slug string) (*models.Note, error) GetBySlug(ctx context.Context, dbContext persistence.IDbContext, slug string) (*models.Note, error)
} }
type noteRepository struct { type noteRepository struct{}
dbContext persistence.IDbContext
func NewNoteRepository() NoteRepository {
return &noteRepository{}
} }
func NewNoteRepository(ctx persistence.IDbContext) NoteRepository { func (r *noteRepository) Insert(ctx context.Context, dbContext persistence.IDbContext, note *models.Note) error {
return &noteRepository{dbContext: ctx}
}
func (r *noteRepository) Insert(ctx context.Context, note *models.Note) error {
query := ` query := `
INSERT INTO note (id, title, path, slug, created_at, updated_at) INSERT INTO note (id, title, path, slug, created_at, updated_at)
VALUES (?, ?, ?, ?, datetime('now'), datetime('now')) VALUES (?, ?, ?, ?, datetime('now'), datetime('now'))
@ -30,19 +28,19 @@ func (r *noteRepository) Insert(ctx context.Context, note *models.Note) error {
SET title = EXCLUDED.title, SET title = EXCLUDED.title,
path = EXCLUDED.path; 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 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 = ?` 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 var id, title, path, createdAt, updatedAt string
err := row.Scan(&id, &title, &path, &slug, &createdAt, &updatedAt) err := row.Scan(&id, &title, &path, &slug, &createdAt, &updatedAt)
if err != nil { if err != nil {
if errors.Is(err, sql.ErrNoRows) { if errors.Is(err, sql.ErrNoRows) {
return nil, nil return nil, nil
} }
return nil, err return nil, err
} }