feat(note_create): tag repo and note repo
This commit is contained in:
parent
5e0dbe5873
commit
2080821382
5 changed files with 96 additions and 28 deletions
|
|
@ -14,7 +14,6 @@ type FrontMatter struct {
|
||||||
Created string
|
Created string
|
||||||
Updated string
|
Updated string
|
||||||
Author string
|
Author string
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseFrontMatter(r io.Reader) (map[string]string, error) {
|
func ParseFrontMatter(r io.Reader) (map[string]string, error) {
|
||||||
|
|
|
||||||
19
core/frontmatter/slug.go
Normal file
19
core/frontmatter/slug.go
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
package frontmatter
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Slugify(s string) string {
|
||||||
|
var slug bytes.Buffer
|
||||||
|
for _, r := range s {
|
||||||
|
if (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') {
|
||||||
|
slug.WriteRune(r)
|
||||||
|
} else if r >= 'A' && r <= 'Z' {
|
||||||
|
slug.WriteRune(r + 32) // Convert to lowercase
|
||||||
|
} else {
|
||||||
|
slug.WriteRune('-')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return slug.String()
|
||||||
|
}
|
||||||
|
|
@ -1,8 +1,11 @@
|
||||||
package handlers
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/KristianJBorgwarth/dendrite.daemon/core/frontmatter"
|
||||||
"github.com/KristianJBorgwarth/dendrite.daemon/core/rpc"
|
"github.com/KristianJBorgwarth/dendrite.daemon/core/rpc"
|
||||||
"github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories"
|
"github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories"
|
||||||
)
|
)
|
||||||
|
|
@ -14,21 +17,46 @@ type createNoteCommand struct {
|
||||||
Vars map[string]string `json:"vars"`
|
Vars map[string]string `json:"vars"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CreateNoteHandler struct{
|
type CreateNoteHandler struct {
|
||||||
repository repositories.NoteRepository
|
noteRepo repositories.NoteRepository
|
||||||
|
tagRepo repositories.TagRepository
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h CreateNoteHandler) Handle(params []byte) (any, *rpc.Error) {
|
func (h CreateNoteHandler) Handle(params []byte) (*rpc.Response, *rpc.Error) {
|
||||||
var cmd createNoteCommand
|
var cmd createNoteCommand
|
||||||
|
|
||||||
if err := json.Unmarshal(params, &cmd); err != nil {
|
if err := json.Unmarshal(params, &cmd); err != nil {
|
||||||
return nil, &rpc.Error{Code: -32602, Message: "invalid params"}
|
return nil, h.ReturnError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err := h.repository.Upsert(cmd.Title, cmd.Path, cmd.Path)
|
data, err := os.ReadFile(cmd.TemplatePath)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, &rpc.Error{Code: -1, Message: err.Error()}
|
return nil, h.ReturnError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return map[string]any{"status": "ok"}, nil
|
feMatter, err := frontmatter.ParseFrontMatter(bytes.NewReader(data))
|
||||||
|
if err != nil {
|
||||||
|
return nil, h.ReturnError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
tags, err := frontmatter.ExtractTags(feMatter)
|
||||||
|
if err != nil {
|
||||||
|
return nil, h.ReturnError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = h.tagRepo.Upsert(tags)
|
||||||
|
if err != nil {
|
||||||
|
return nil, h.ReturnError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = h.noteRepo.Upsert(cmd.Title, cmd.Path, frontmatter.Slugify(cmd.Title))
|
||||||
|
if err != nil {
|
||||||
|
return nil, h.ReturnError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &rpc.Response{Jsonrpc: "2.0", Result: cmd.Path}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *CreateNoteHandler) ReturnError(err error) *rpc.Error {
|
||||||
|
return &rpc.Error{Code: -1, Message: err.Error()}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,16 +7,16 @@ import (
|
||||||
|
|
||||||
type Request struct {
|
type Request struct {
|
||||||
Jsonrpc string `json:"jsonrpc"`
|
Jsonrpc string `json:"jsonrpc"`
|
||||||
ID *int `json:"id"`
|
ID *int `json:"id"`
|
||||||
Method string `json:"method"`
|
Method string `json:"method"`
|
||||||
Params json.RawMessage `json:"params"`
|
Params json.RawMessage `json:"params"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Response struct {
|
type Response struct {
|
||||||
Jsonrpc string `json:"jsonrpc"`
|
Jsonrpc string `json:"jsonrpc"`
|
||||||
ID *int `json:"id"`
|
ID *int `json:"id"`
|
||||||
Result any `json:"result,omitempty"`
|
Result any `json:"result,omitempty"`
|
||||||
Error *Error `json:"error,omitempty"`
|
Error *Error `json:"error,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Notification struct {
|
type Notification struct {
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,12 @@
|
||||||
|
|
||||||
package repositories
|
package repositories
|
||||||
|
|
||||||
import "database/sql"
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
type TagRepository interface {
|
type TagRepository interface {
|
||||||
Upsert(title string, path string) error
|
Upsert(names []string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type tagRepository struct {
|
type tagRepository struct {
|
||||||
|
|
@ -15,14 +17,34 @@ func NewTagRepository(db *sql.DB) TagRepository {
|
||||||
return &tagRepository{db: db}
|
return &tagRepository{db: db}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *tagRepository) Upsert(title string, path string) error {
|
func (r *tagRepository) Upsert(names []string) error {
|
||||||
query := `
|
if len(names) == 0 {
|
||||||
INSERT INTO tags (title, path)
|
return nil
|
||||||
VALUES ($1, $2)
|
}
|
||||||
ON CONFLICT (slug) DO UPDATE
|
|
||||||
SET title = EXCLUDED.title,
|
tx, err := r.db.Begin()
|
||||||
path = EXCLUDED.path;
|
if err != nil {
|
||||||
`
|
return err
|
||||||
_, err := r.db.Exec(query, title, path)
|
}
|
||||||
return err
|
defer tx.Rollback()
|
||||||
|
|
||||||
|
placeholders := make([]string, 0, len(names))
|
||||||
|
args := make([]any, 0, len(names))
|
||||||
|
|
||||||
|
for _, name := range names {
|
||||||
|
placeholders = append(placeholders, "(?)")
|
||||||
|
args = append(args, name)
|
||||||
|
}
|
||||||
|
|
||||||
|
query := "WITH input(name) AS (VALUES " +
|
||||||
|
strings.Join(placeholders, ",") +
|
||||||
|
") INSERT INTO tags(name) " +
|
||||||
|
"SELECT name FROM input " +
|
||||||
|
"ON CONFLICT(name) DO NOTHING;"
|
||||||
|
|
||||||
|
if _, err := tx.Exec(query, args...); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return tx.Commit()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue