feat(cfe): add cfe on create (#44)
This commit is contained in:
parent
f1bc87e3ad
commit
87536b474a
9 changed files with 117 additions and 13 deletions
|
|
@ -30,12 +30,13 @@ func main() {
|
||||||
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)
|
||||||
|
cfeSvc := services.NewCfeService(repositories.NewCfeRepository(persistence.NewReadContext()))
|
||||||
idxr := services.NewIndexRebuilder(uow, noteRepo, linkRepo, tagRepo, indexRepo )
|
idxr := services.NewIndexRebuilder(uow, noteRepo, linkRepo, tagRepo, indexRepo )
|
||||||
|
|
||||||
server.RegisterHandler("vault/init", vault.NewInitializeHandler(idxr, noteService))
|
server.RegisterHandler("vault/init", vault.NewInitializeHandler(idxr, noteService))
|
||||||
server.RegisterHandler("vault/rebuild", vault.NewRebuildIndexHandler(idxr))
|
server.RegisterHandler("vault/rebuild", vault.NewRebuildIndexHandler(idxr))
|
||||||
|
|
||||||
server.RegisterHandler("note/create", note.NewCreateNoteHandler(uow, tagService, noteRepo))
|
server.RegisterHandler("note/create", note.NewCreateNoteHandler(uow, tagService, noteRepo, cfeSvc))
|
||||||
server.RegisterHandler("note/save", note.NewSaveNoteHandler(uow, noteRepo, tagService, noteService, linkService))
|
server.RegisterHandler("note/save", note.NewSaveNoteHandler(uow, noteRepo, tagService, noteService, linkService))
|
||||||
server.RegisterHandler("note/delete", note.NewDeleteNoteHandler(uow, tagService, noteService))
|
server.RegisterHandler("note/delete", note.NewDeleteNoteHandler(uow, tagService, noteService))
|
||||||
server.RegisterHandler("note/goto", note.NewGotoNoteHandler(noteRepo))
|
server.RegisterHandler("note/goto", note.NewGotoNoteHandler(noteRepo))
|
||||||
|
|
|
||||||
|
|
@ -7,12 +7,12 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type FrontMatter struct {
|
type FrontMatter struct {
|
||||||
Title string `yaml:"title"`
|
Title string `yaml:"title"`
|
||||||
Tags []string `yaml:"tags"`
|
Tags []string `yaml:"tags"`
|
||||||
Created string `yaml:"created"`
|
Created string `yaml:"created"`
|
||||||
Updated string `yaml:"updated"`
|
Updated string `yaml:"updated"`
|
||||||
Date string `yaml:"date"`
|
Date string `yaml:"date"`
|
||||||
Author string `yaml:"author"`
|
Custom map[string]any `yaml:",inline"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseFrontMatter(file []byte) (*FrontMatter, error) {
|
func ParseFrontMatter(file []byte) (*FrontMatter, error) {
|
||||||
|
|
|
||||||
|
|
@ -20,16 +20,18 @@ type createNoteCommand struct {
|
||||||
|
|
||||||
type CreateNoteHandler struct {
|
type CreateNoteHandler struct {
|
||||||
uow *repositories.UnitOfWork
|
uow *repositories.UnitOfWork
|
||||||
tagService services.ITagService
|
tagSvc services.ITagService
|
||||||
noteRepo repositories.INoteRepository
|
noteRepo repositories.INoteRepository
|
||||||
|
cfeSvc services.ICfeService
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCreateNoteHandler(
|
func NewCreateNoteHandler(
|
||||||
uow *repositories.UnitOfWork,
|
uow *repositories.UnitOfWork,
|
||||||
tagRepo services.ITagService,
|
tagSvc services.ITagService,
|
||||||
noteRepo repositories.INoteRepository,
|
noteRepo repositories.INoteRepository,
|
||||||
|
cfeSvc services.ICfeService,
|
||||||
) *CreateNoteHandler {
|
) *CreateNoteHandler {
|
||||||
return &CreateNoteHandler{uow, tagRepo, noteRepo}
|
return &CreateNoteHandler{uow, tagSvc, noteRepo, cfeSvc}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (any, error) {
|
func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (any, error) {
|
||||||
|
|
@ -62,7 +64,7 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (an
|
||||||
return notePath, nil
|
return notePath, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
tagModels, err := h.tagService.CreateTags(ctx, dbCtx, template.FrontMatter.Tags)
|
tagModels, err := h.tagSvc.CreateTags(ctx, dbCtx, template.FrontMatter.Tags)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -73,7 +75,11 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (an
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = h.tagService.CreateNoteTags(ctx, dbCtx, note.ID(), tagModels); err != nil {
|
if err = h.tagSvc.CreateNoteTags(ctx, dbCtx, note.ID(), tagModels); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = h.cfeSvc.AddCfe(ctx, dbCtx, note.ID(), template.FrontMatter.Custom); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,3 +9,21 @@ func MapToLinkModel(noteID string, extractedLinks []*filehandling.ExtractedLink)
|
||||||
}
|
}
|
||||||
return links
|
return links
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func MapToCfe(noteID string, extractedCfe map[string]any) []*CustomFronMatter {
|
||||||
|
var cfe []*CustomFronMatter
|
||||||
|
for key, value := range extractedCfe {
|
||||||
|
if valueStr, ok := value.(string); ok {
|
||||||
|
cfe = append(cfe, NewCustomFrontMatter(noteID, key, valueStr))
|
||||||
|
continue
|
||||||
|
} else if valueArr, ok := value.([]string); ok {
|
||||||
|
for _, v := range valueArr {
|
||||||
|
cfe = append(cfe, NewCustomFrontMatter(noteID, key, v))
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
cfe = append(cfe, NewCustomFrontMatter(noteID, key, ""))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return cfe
|
||||||
|
}
|
||||||
|
|
|
||||||
34
core/services/cfe_service.go
Normal file
34
core/services/cfe_service.go
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
package services
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/KristianJBorgwarth/dendrite.daemon/core/models"
|
||||||
|
"github.com/KristianJBorgwarth/dendrite.daemon/persistence"
|
||||||
|
"github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ICfeService interface {
|
||||||
|
AddCfe(ctx context.Context, dbCtx persistence.IDbContext, noteID string, cfe map[string]any) error
|
||||||
|
}
|
||||||
|
|
||||||
|
type cfeService struct {
|
||||||
|
cfeRepo repositories.ICfeRepository
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewCfeService(cfeRepo repositories.ICfeRepository) ICfeService {
|
||||||
|
return &cfeService{cfeRepo}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *cfeService) AddCfe(
|
||||||
|
ctx context.Context,
|
||||||
|
dbCtx persistence.IDbContext,
|
||||||
|
noteID string,
|
||||||
|
cfe map[string]any,
|
||||||
|
) error {
|
||||||
|
if len(cfe) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
cfeModels := models.MapToCfe(noteID, cfe)
|
||||||
|
return s.cfeRepo.InsertRange(ctx, dbCtx, cfeModels)
|
||||||
|
}
|
||||||
3
makefile
3
makefile
|
|
@ -7,3 +7,6 @@ test-unit:
|
||||||
|
|
||||||
test-integration:
|
test-integration:
|
||||||
go test ./test/test_integration/...
|
go test ./test/test_integration/...
|
||||||
|
|
||||||
|
build:
|
||||||
|
go build -o dendrite ./cmd
|
||||||
|
|
|
||||||
|
|
@ -52,7 +52,7 @@ func (vc *VaultConfig) TemplateDirectory() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (vc *VaultConfig) ExcludeIndexFiles() []string {
|
func (vc *VaultConfig) ExcludeIndexFiles() []string {
|
||||||
defaultIgnores := []string{"index.md", "index"}
|
defaultIgnores := []string{"index.md", "index", ".templates"}
|
||||||
if vc.overrideDefaultIgnores {
|
if vc.overrideDefaultIgnores {
|
||||||
return vc.excludeIndexFiles
|
return vc.excludeIndexFiles
|
||||||
}
|
}
|
||||||
|
|
|
||||||
41
persistence/repositories/cfe_repository.go
Normal file
41
persistence/repositories/cfe_repository.go
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
package repositories
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/KristianJBorgwarth/dendrite.daemon/core/models"
|
||||||
|
"github.com/KristianJBorgwarth/dendrite.daemon/persistence"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ICfeRepository interface {
|
||||||
|
InsertRange(ctx context.Context, dbContext persistence.IDbContext, cfe []*models.CustomFronMatter) error
|
||||||
|
}
|
||||||
|
|
||||||
|
type cfeRepository struct {
|
||||||
|
readDBContext persistence.ReadContext
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewCfeRepository(rdb persistence.ReadContext) ICfeRepository {
|
||||||
|
return &cfeRepository{readDBContext: rdb}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *cfeRepository) InsertRange(
|
||||||
|
ctx context.Context,
|
||||||
|
dbCtx persistence.IDbContext,
|
||||||
|
cfe []*models.CustomFronMatter,
|
||||||
|
) error {
|
||||||
|
statement, err := dbCtx.Prepare(
|
||||||
|
`INSERT INTO custom_frontmatter (note_id, key, value)
|
||||||
|
VALUES (?, ?, ?) ON CONFLICT DO NOTHING;`,)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, c := range cfe {
|
||||||
|
if _, err := statement.ExecContext(ctx, c.NodeID(), c.Key(), c.Value()); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
@ -19,6 +19,7 @@ func newCreateNoteHandler() *note.CreateNoteHandler {
|
||||||
repositories.NewUnitOfWork(),
|
repositories.NewUnitOfWork(),
|
||||||
services.NewTagService(repositories.NewTagRepository(persistence.NewReadContext())),
|
services.NewTagService(repositories.NewTagRepository(persistence.NewReadContext())),
|
||||||
repositories.NewNoteRepository(persistence.NewReadContext()),
|
repositories.NewNoteRepository(persistence.NewReadContext()),
|
||||||
|
services.NewCfeService(repositories.NewCfeRepository(persistence.NewReadContext())),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue