feat(save_note): improved note save

This commit is contained in:
Kristian Borgwarth 2026-04-12 20:49:08 +02:00
parent 4ca0edf66b
commit 96b51584ff
8 changed files with 68 additions and 92 deletions

View file

@ -23,10 +23,12 @@ func main() {
tagRepo := repositories.NewTagRepository() tagRepo := repositories.NewTagRepository()
noteRepo := repositories.NewNoteRepository() noteRepo := repositories.NewNoteRepository()
tagService := services.NewTagService(tagRepo) tagService := services.NewTagService(tagRepo)
linkService := services.NewLinkService(linkRepo)
noteService := services.NewNoteService(tagRepo, linkRepo, noteRepo)
server.RegisterHandler("vault/init", vault.NewInitializeHandler()) server.RegisterHandler("vault/init", vault.NewInitializeHandler())
server.RegisterHandler("note/create", note.NewCreateNoteHandler(uow, tagService, noteRepo)) server.RegisterHandler("note/create", note.NewCreateNoteHandler(uow, tagService, noteRepo))
server.RegisterHandler("note/save", note.NewSaveNoteHandler(uow, linkRepo, tagRepo, noteRepo)) server.RegisterHandler("note/save", note.NewSaveNoteHandler(uow, noteRepo, tagService, noteService, linkService))
server.RegisterHandler("completion/link", completion.NewCompleteLinkHandler()) server.RegisterHandler("completion/link", completion.NewCompleteLinkHandler())
if err := server.Run(os.Stdin, os.Stdout); err != nil { if err := server.Run(os.Stdin, os.Stdout); err != nil {

View file

@ -21,13 +21,13 @@ type createNoteCommand struct {
type CreateNoteHandler struct { type CreateNoteHandler struct {
uow *repositories.UnitOfWork uow *repositories.UnitOfWork
tagService services.ITagService tagService services.ITagService
noteRepo repositories.NoteRepository noteRepo repositories.INoteRepository
} }
func NewCreateNoteHandler( func NewCreateNoteHandler(
uow *repositories.UnitOfWork, uow *repositories.UnitOfWork,
tagRepo services.ITagService, tagRepo services.ITagService,
noteRepo repositories.NoteRepository, noteRepo repositories.INoteRepository,
) *CreateNoteHandler { ) *CreateNoteHandler {
return &CreateNoteHandler{uow, tagRepo, noteRepo} return &CreateNoteHandler{uow, tagRepo, noteRepo}
} }

View file

@ -6,9 +6,7 @@ import (
"log/slog" "log/slog"
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/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"
) )
@ -18,20 +16,20 @@ type saveNoteCommand struct {
type SaveNoteHandler struct { type SaveNoteHandler struct {
uow *repositories.UnitOfWork uow *repositories.UnitOfWork
linkRepo repositories.ILinkRepository noteRepo repositories.INoteRepository
tagService services.ITagService tagService services.ITagService
noteService services.INoteService noteService services.INoteService
noteRepo repositories.NoteRepository linkService services.ILinkService
} }
func NewSaveNoteHandler( func NewSaveNoteHandler(
uow *repositories.UnitOfWork, uow *repositories.UnitOfWork,
linkRepo repositories.ILinkRepository, nr repositories.INoteRepository,
tagService services.ITagService, ts services.ITagService,
noteService services.INoteService, ns services.INoteService,
noteRepo repositories.NoteRepository, ls services.ILinkService,
) *SaveNoteHandler { ) *SaveNoteHandler {
return &SaveNoteHandler{uow, linkRepo, tagService, noteService, noteRepo} return &SaveNoteHandler{uow, nr, ts, ns, ls}
} }
func (h *SaveNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (any, error) { func (h *SaveNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (any, error) {
@ -60,86 +58,37 @@ func (h *SaveNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (any,
return nil, err return nil, err
} }
if note == nil { if note == nil {
if err := h.handleNewNote(ctx, tx, file); err != nil { note, err = h.noteService.CreateNote(ctx, tx, file.Path, file.Title, file.Slug)
if err != nil {
slog.Debug("Failed to save new note", "slug", file.Slug, "error", err)
return nil, err return nil, err
} }
} else { } else {
if err := h.handleExistingNote(ctx, tx, note, file); err != nil { if err = h.noteService.DeleteNoteMetaData(ctx, tx, note.ID()); err != nil {
slog.Debug("Failed to delete existing note metadata", "noteID", note.ID(), "error", err)
return nil, err return nil, err
} }
} }
tagModels, err := h.tagService.CreateTags(ctx, tx, file.FrontMatter.Tags)
if err != nil {
slog.Debug("Failed to create tags", "tags", file.FrontMatter.Tags, "error", err)
return nil, err
}
if err = h.tagService.CreateNoteTags(ctx, tx, note.ID(), tagModels); err != nil {
slog.Debug("Failed to create note tags", "noteID", note.ID(), "tags", file.FrontMatter.Tags, "error", err)
return nil, err
}
if err = h.linkService.CreateLinks(ctx, tx, note.ID(), file.ExtractedLinks); err != nil {
slog.Debug("Failed to create links", "noteID", note.ID(), "links", file.ExtractedLinks, "error", err)
return nil, err
}
if err := h.uow.Commit(); err != nil { if err := h.uow.Commit(); err != nil {
return nil, err return nil, err
} }
return nil, nil return nil, nil
} }
func (h *SaveNoteHandler) handleNewNote(
ctx context.Context,
dbCtx persistence.IDbContext,
file *filehandling.File,
) error {
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 := h.noteRepo.Insert(ctx, dbCtx, note); err != nil {
slog.Debug("Failed to insert new note", "noteID", note.ID(), "error", err)
return err
}
links := models.MapToLinkModel(note.ID(), file.ExtractedLinks)
if err := h.linkRepo.Insert(ctx, dbCtx, links); err != nil {
slog.Debug("Failed to insert links for new note", "noteID", note.ID(), "error", err)
return err
}
tagModels, err := h.tagService.CreateTags(ctx, dbCtx, file.FrontMatter.Tags)
if err != nil {
slog.Debug("Failed to create tags for new note", "noteID", note.ID(), "error", err)
return err
}
if err := h.tagService.CreateNoteTags(ctx, dbCtx, note.ID(), tagModels); err != nil {
slog.Debug("Failed to insert note tags for new note", "noteID", note.ID(), "error", err)
return err
}
return nil
}
func (h *SaveNoteHandler) handleExistingNote(
ctx context.Context,
dbCtx persistence.IDbContext,
note *models.Note,
file *filehandling.File,
) error {
err := h.noteService.DeleteNote(ctx, dbCtx, note.ID())
if err != nil {
slog.Debug("Failed to delete existing note relations", "noteID", note.ID(), "error", err)
return err
}
links := models.MapToLinkModel(note.ID(), file.ExtractedLinks)
if err = h.linkRepo.Insert(ctx, dbCtx, links); err != nil {
slog.Debug("Failed to insert links for existing note", "noteID", note.ID(), "error", err)
return err
}
tagModels, err := h.tagService.CreateTags(ctx, dbCtx, file.FrontMatter.Tags)
if err != nil {
slog.Debug("Failed to create tags for existing note", "noteID", note.ID(), "error", err)
return err
}
if err = h.tagService.CreateNoteTags(ctx, dbCtx, note.ID(), tagModels); err != nil {
slog.Debug("Failed to insert note tags for existing note", "noteID", note.ID(), "error", err)
return err
}
return nil
}

View file

@ -0,0 +1,25 @@
package services
import (
"context"
filehandling "github.com/KristianJBorgwarth/dendrite.daemon/core/file_handling"
"github.com/KristianJBorgwarth/dendrite.daemon/persistence"
"github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories"
)
type ILinkService interface {
CreateLinks(ctx context.Context, dbCtx persistence.IDbContext, noteID string, links []*filehandling.ExtractedLink) error
}
type linkService struct {
linkRepo repositories.ILinkRepository
}
func NewLinkService(linkRepo repositories.ILinkRepository) ILinkService {
return &linkService{linkRepo}
}
func (s *linkService) CreateLinks(ctx context.Context, dbCtx persistence.IDbContext, noteID string, links []*filehandling.ExtractedLink) error {
panic("not implemented")
}

View file

@ -9,25 +9,25 @@ import (
) )
type INoteService interface { type INoteService interface {
SaveNote(ctx context.Context, dbCtx persistence.IDbContext, path, title, slug string) (*models.Note, error) CreateNote(ctx context.Context, dbCtx persistence.IDbContext, path, title, slug string) (*models.Note, error)
DeleteNote(ctx context.Context, dbCtx persistence.IDbContext, noteID string) error DeleteNoteMetaData(ctx context.Context, dbCtx persistence.IDbContext, noteID string) error
} }
type noteService struct { type noteService struct {
tagRepo repositories.ITagRepository tagRepo repositories.ITagRepository
linkRepo repositories.ILinkRepository linkRepo repositories.ILinkRepository
noteRepo repositories.NoteRepository noteRepo repositories.INoteRepository
} }
func NewNoteService( func NewNoteService(
tagRepo repositories.ITagRepository, tagRepo repositories.ITagRepository,
linkRepo repositories.ILinkRepository, linkRepo repositories.ILinkRepository,
noteRepo repositories.NoteRepository, noteRepo repositories.INoteRepository,
) *noteService { ) INoteService {
return &noteService{tagRepo, linkRepo, noteRepo} return &noteService{tagRepo, linkRepo, noteRepo}
} }
func (s *noteService) SaveNote(ctx context.Context, dbCtx persistence.IDbContext, path, title, slug string) (*models.Note, error) { func (s *noteService) CreateNote(ctx context.Context, dbCtx persistence.IDbContext, path, title, slug string) (*models.Note, error) {
note:= models.CreateNote(path, title, slug) note:= models.CreateNote(path, title, slug)
if err := s.noteRepo.Insert(ctx, dbCtx, note); err != nil { if err := s.noteRepo.Insert(ctx, dbCtx, note); err != nil {
return nil, err return nil, err
@ -36,7 +36,7 @@ func (s *noteService) SaveNote(ctx context.Context, dbCtx persistence.IDbContext
return note, nil return note, nil
} }
func (s *noteService) DeleteNote(ctx context.Context, dbCtx persistence.IDbContext, noteID string) error { func (s *noteService) DeleteNoteMetaData(ctx context.Context, dbCtx persistence.IDbContext, noteID string) error {
if err := s.linkRepo.Delete(ctx, dbCtx, noteID); err != nil { if err := s.linkRepo.Delete(ctx, dbCtx, noteID); err != nil {
return err return err
} }

View file

@ -18,7 +18,7 @@ type tagService struct {
tagRepo repositories.ITagRepository tagRepo repositories.ITagRepository
} }
func NewTagService(tagRepo repositories.ITagRepository) *tagService { func NewTagService(tagRepo repositories.ITagRepository) ITagService {
return &tagService{tagRepo} return &tagService{tagRepo}
} }

BIN
dendrite

Binary file not shown.

View file

@ -9,14 +9,14 @@ import (
"github.com/KristianJBorgwarth/dendrite.daemon/persistence" "github.com/KristianJBorgwarth/dendrite.daemon/persistence"
) )
type NoteRepository 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
GetBySlug(ctx context.Context, dbContext persistence.IDbContext, slug string) (*models.Note, error) GetBySlug(ctx context.Context, dbContext persistence.IDbContext, slug string) (*models.Note, error)
} }
type noteRepository struct{} type noteRepository struct{}
func NewNoteRepository() NoteRepository { func NewNoteRepository() INoteRepository {
return &noteRepository{} return &noteRepository{}
} }