ref(repo): improve repo di
This commit is contained in:
parent
754a4c5435
commit
f3e11149ff
5 changed files with 22 additions and 19 deletions
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
2
core/services/doc.go
Normal file
2
core/services/doc.go
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
// Package services provides the implementation of the services defined in the API specification.
|
||||
package services
|
||||
1
core/services/tag_service.go
Normal file
1
core/services/tag_service.go
Normal file
|
|
@ -0,0 +1 @@
|
|||
package
|
||||
|
|
@ -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,13 +28,13 @@ 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)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue