feat(save_note): improved note save
This commit is contained in:
parent
4ca0edf66b
commit
96b51584ff
8 changed files with 68 additions and 92 deletions
|
|
@ -23,10 +23,12 @@ func main() {
|
|||
tagRepo := repositories.NewTagRepository()
|
||||
noteRepo := repositories.NewNoteRepository()
|
||||
tagService := services.NewTagService(tagRepo)
|
||||
linkService := services.NewLinkService(linkRepo)
|
||||
noteService := services.NewNoteService(tagRepo, linkRepo, noteRepo)
|
||||
|
||||
server.RegisterHandler("vault/init", vault.NewInitializeHandler())
|
||||
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())
|
||||
|
||||
if err := server.Run(os.Stdin, os.Stdout); err != nil {
|
||||
|
|
|
|||
|
|
@ -21,13 +21,13 @@ type createNoteCommand struct {
|
|||
type CreateNoteHandler struct {
|
||||
uow *repositories.UnitOfWork
|
||||
tagService services.ITagService
|
||||
noteRepo repositories.NoteRepository
|
||||
noteRepo repositories.INoteRepository
|
||||
}
|
||||
|
||||
func NewCreateNoteHandler(
|
||||
uow *repositories.UnitOfWork,
|
||||
tagRepo services.ITagService,
|
||||
noteRepo repositories.NoteRepository,
|
||||
noteRepo repositories.INoteRepository,
|
||||
) *CreateNoteHandler {
|
||||
return &CreateNoteHandler{uow, tagRepo, noteRepo}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,9 +6,7 @@ import (
|
|||
"log/slog"
|
||||
|
||||
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/persistence"
|
||||
"github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories"
|
||||
)
|
||||
|
||||
|
|
@ -18,20 +16,20 @@ type saveNoteCommand struct {
|
|||
|
||||
type SaveNoteHandler struct {
|
||||
uow *repositories.UnitOfWork
|
||||
linkRepo repositories.ILinkRepository
|
||||
noteRepo repositories.INoteRepository
|
||||
tagService services.ITagService
|
||||
noteService services.INoteService
|
||||
noteRepo repositories.NoteRepository
|
||||
linkService services.ILinkService
|
||||
}
|
||||
|
||||
func NewSaveNoteHandler(
|
||||
uow *repositories.UnitOfWork,
|
||||
linkRepo repositories.ILinkRepository,
|
||||
tagService services.ITagService,
|
||||
noteService services.INoteService,
|
||||
noteRepo repositories.NoteRepository,
|
||||
nr repositories.INoteRepository,
|
||||
ts services.ITagService,
|
||||
ns services.INoteService,
|
||||
ls services.ILinkService,
|
||||
) *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) {
|
||||
|
|
@ -60,86 +58,37 @@ func (h *SaveNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (any,
|
|||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
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
|
||||
}
|
||||
} 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
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
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
|
||||
}
|
||||
|
|
|
|||
25
core/services/link_service.go
Normal file
25
core/services/link_service.go
Normal 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")
|
||||
}
|
||||
|
|
@ -9,25 +9,25 @@ import (
|
|||
)
|
||||
|
||||
type INoteService interface {
|
||||
SaveNote(ctx context.Context, dbCtx persistence.IDbContext, path, title, slug string) (*models.Note, error)
|
||||
DeleteNote(ctx context.Context, dbCtx persistence.IDbContext, noteID string) error
|
||||
CreateNote(ctx context.Context, dbCtx persistence.IDbContext, path, title, slug string) (*models.Note, error)
|
||||
DeleteNoteMetaData(ctx context.Context, dbCtx persistence.IDbContext, noteID string) error
|
||||
}
|
||||
|
||||
type noteService struct {
|
||||
tagRepo repositories.ITagRepository
|
||||
linkRepo repositories.ILinkRepository
|
||||
noteRepo repositories.NoteRepository
|
||||
noteRepo repositories.INoteRepository
|
||||
}
|
||||
|
||||
func NewNoteService(
|
||||
tagRepo repositories.ITagRepository,
|
||||
linkRepo repositories.ILinkRepository,
|
||||
noteRepo repositories.NoteRepository,
|
||||
) *noteService {
|
||||
noteRepo repositories.INoteRepository,
|
||||
) INoteService {
|
||||
return ¬eService{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)
|
||||
if err := s.noteRepo.Insert(ctx, dbCtx, note); err != nil {
|
||||
return nil, err
|
||||
|
|
@ -36,7 +36,7 @@ func (s *noteService) SaveNote(ctx context.Context, dbCtx persistence.IDbContext
|
|||
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 {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ type tagService struct {
|
|||
tagRepo repositories.ITagRepository
|
||||
}
|
||||
|
||||
func NewTagService(tagRepo repositories.ITagRepository) *tagService {
|
||||
func NewTagService(tagRepo repositories.ITagRepository) ITagService {
|
||||
return &tagService{tagRepo}
|
||||
}
|
||||
|
||||
|
|
|
|||
BIN
dendrite
BIN
dendrite
Binary file not shown.
|
|
@ -9,14 +9,14 @@ import (
|
|||
"github.com/KristianJBorgwarth/dendrite.daemon/persistence"
|
||||
)
|
||||
|
||||
type NoteRepository interface {
|
||||
type INoteRepository interface {
|
||||
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{}
|
||||
|
||||
func NewNoteRepository() NoteRepository {
|
||||
func NewNoteRepository() INoteRepository {
|
||||
return ¬eRepository{}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue