package handlers import ( "context" "encoding/json" "github.com/KristianJBorgwarth/dendrite.daemon/core/frontmatter" "github.com/KristianJBorgwarth/dendrite.daemon/core/models" "github.com/KristianJBorgwarth/dendrite.daemon/core/template" "github.com/KristianJBorgwarth/dendrite.daemon/core/utils" "github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories" ) type createNoteCommand struct { Title string `json:"title"` TemplatePath string `json:"templatePath"` Path string `json:"path"` Vars map[string]string `json:"vars"` } type CreateNoteHandler struct{ uow *repositories.UnitOfWork } func NewCreateNoteHandler() *CreateNoteHandler { return &CreateNoteHandler{repositories.NewUnitOfWork()} } func (h *CreateNoteHandler) Handle(ctx context.Context, uow *repositories.UnitOfWork, raw json.RawMessage) (any, error) { var cmd createNoteCommand if err := json.Unmarshal(raw, &cmd); err != nil { return nil, err } slug := frontmatter.Slugify(cmd.Title) data, err := template.RenderTemplate(cmd.TemplatePath, cmd.Title, slug) if err != nil { return nil, err } tags, err := frontmatter.ParseTags(data) if err != nil { return nil, err } tx, err := uow.Begin() if err != nil { return nil, err } defer uow.Rollback() tagRepo := repositories.NewTagRepository(tx) noteRepo := repositories.NewNoteRepository(tx) tagModels, err := models.CreateTags(tags) if err != nil { return nil, err } if err = tagRepo.Upsert(ctx, tagModels); err != nil { return nil, err } note := models.CreateNote(cmd.Path, cmd.Title, slug) if err = noteRepo.Upsert(ctx, note); err != nil { return nil, err } if err = tagRepo.UpsertNoteTags(ctx, note.ID(), utils.Select(tagModels, func(t *models.Tag) string { return t.ID() })); err != nil { return nil, err } uow.FileStore.Stage(cmd.Path, data) if err = uow.Commit(); err != nil { return nil, err } return cmd.Path, nil }