feat(goto): added goto_note_handler
This commit is contained in:
parent
7e45dd493f
commit
f8ba52e62e
5 changed files with 19 additions and 11 deletions
|
|
@ -22,7 +22,7 @@ func main() {
|
||||||
uow := repositories.NewUnitOfWork();
|
uow := repositories.NewUnitOfWork();
|
||||||
linkRepo := repositories.NewLinkRepository(*persistence.NewReadContext())
|
linkRepo := repositories.NewLinkRepository(*persistence.NewReadContext())
|
||||||
tagRepo := repositories.NewTagRepository()
|
tagRepo := repositories.NewTagRepository()
|
||||||
noteRepo := repositories.NewNoteRepository()
|
noteRepo := repositories.NewNoteRepository(*persistence.NewReadContext())
|
||||||
tagService := services.NewTagService(tagRepo)
|
tagService := services.NewTagService(tagRepo)
|
||||||
linkService := services.NewLinkService(linkRepo)
|
linkService := services.NewLinkService(linkRepo)
|
||||||
noteService := services.NewNoteService(tagRepo, linkRepo, noteRepo)
|
noteService := services.NewNoteService(tagRepo, linkRepo, noteRepo)
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,10 @@
|
||||||
package note
|
package note
|
||||||
|
|
||||||
import "github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories"
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories"
|
||||||
|
)
|
||||||
|
|
||||||
type gotoNoteCommand struct {
|
type gotoNoteCommand struct {
|
||||||
Slug string `json:"slug"`
|
Slug string `json:"slug"`
|
||||||
|
|
@ -10,13 +14,14 @@ type gotoNoteResult struct {
|
||||||
Path string `json:"path"`
|
Path string `json:"path"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type GotoNoteHandler struct{
|
type GotoNoteHandler struct {
|
||||||
noteRepo repositories.INoteRepository
|
noteRepo repositories.INoteRepository
|
||||||
}
|
}
|
||||||
|
|
||||||
func(h *GotoNoteHandler) Handle(command gotoNoteCommand) (gotoNoteResult, error) {
|
func (h *GotoNoteHandler) Handle(ctx context.Context, command gotoNoteCommand) (gotoNoteResult, error) {
|
||||||
note, err := h.noteRepo.GetBySlug(command.Slug)
|
note, err := h.noteRepo.GetBySlug(ctx, command.Slug)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return gotoNoteResult{}, err
|
return gotoNoteResult{}, err
|
||||||
}
|
}
|
||||||
|
return gotoNoteResult{Path: note.Path()}, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -54,7 +54,7 @@ func (h *SaveNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (any,
|
||||||
}
|
}
|
||||||
defer h.uow.Rollback()
|
defer h.uow.Rollback()
|
||||||
|
|
||||||
note, err := h.noteRepo.GetBySlug(ctx, tx, file.Slug)
|
note, err := h.noteRepo.GetBySlug(ctx, 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
|
||||||
|
|
|
||||||
|
|
@ -12,10 +12,12 @@ import (
|
||||||
type INoteRepository interface {
|
type INoteRepository interface {
|
||||||
Insert(ctx context.Context, dbContext persistence.IDbContext, note *models.Note) error
|
Insert(ctx context.Context, dbContext persistence.IDbContext, note *models.Note) error
|
||||||
Update(ctx context.Context, dbContext persistence.IDbContext, noteID, path, title, slug string) error
|
Update(ctx context.Context, dbContext persistence.IDbContext, noteID, path, title, slug string) error
|
||||||
GetBySlug(ctx context.Context, dbContext persistence.IDbContext, slug string) (*models.Note, error)
|
GetBySlug(ctx context.Context, slug string) (*models.Note, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type noteRepository struct{}
|
type noteRepository struct{
|
||||||
|
readDBContext persistence.ReadContext
|
||||||
|
}
|
||||||
|
|
||||||
func NewNoteRepository(persistence.ReadContext) INoteRepository {
|
func NewNoteRepository(persistence.ReadContext) INoteRepository {
|
||||||
return ¬eRepository{}
|
return ¬eRepository{}
|
||||||
|
|
@ -46,9 +48,9 @@ func (r *noteRepository) Update(ctx context.Context, dbContext persistence.IDbCo
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *noteRepository) GetBySlug(ctx context.Context, dbContext persistence.IDbContext, slug string) (*models.Note, error) {
|
func (r *noteRepository) GetBySlug(ctx context.Context, 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 := dbContext.QueryRowContext(ctx, query, slug)
|
row := r.readDBContext.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)
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import (
|
||||||
|
|
||||||
"github.com/KristianJBorgwarth/dendrite.daemon/core/handlers/note"
|
"github.com/KristianJBorgwarth/dendrite.daemon/core/handlers/note"
|
||||||
"github.com/KristianJBorgwarth/dendrite.daemon/core/services"
|
"github.com/KristianJBorgwarth/dendrite.daemon/core/services"
|
||||||
|
"github.com/KristianJBorgwarth/dendrite.daemon/persistence"
|
||||||
"github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories"
|
"github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
@ -17,7 +18,7 @@ func newCreateNoteHandler() *note.CreateNoteHandler {
|
||||||
return note.NewCreateNoteHandler(
|
return note.NewCreateNoteHandler(
|
||||||
repositories.NewUnitOfWork(),
|
repositories.NewUnitOfWork(),
|
||||||
services.NewTagService(repositories.NewTagRepository()),
|
services.NewTagService(repositories.NewTagRepository()),
|
||||||
repositories.NewNoteRepository(),
|
repositories.NewNoteRepository(*persistence.NewReadContext()),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue