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
|
||||
Updated string
|
||||
Author string
|
||||
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"os"
|
||||
|
||||
"github.com/KristianJBorgwarth/dendrite.daemon/core/frontmatter"
|
||||
"github.com/KristianJBorgwarth/dendrite.daemon/core/rpc"
|
||||
"github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories"
|
||||
)
|
||||
|
|
@ -14,21 +17,46 @@ type createNoteCommand struct {
|
|||
Vars map[string]string `json:"vars"`
|
||||
}
|
||||
|
||||
type CreateNoteHandler struct{
|
||||
repository repositories.NoteRepository
|
||||
type CreateNoteHandler struct {
|
||||
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
|
||||
|
||||
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 {
|
||||
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 {
|
||||
Jsonrpc string `json:"jsonrpc"`
|
||||
ID *int `json:"id"`
|
||||
ID *int `json:"id"`
|
||||
Method string `json:"method"`
|
||||
Params json.RawMessage `json:"params"`
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
Jsonrpc string `json:"jsonrpc"`
|
||||
ID *int `json:"id"`
|
||||
Result any `json:"result,omitempty"`
|
||||
Error *Error `json:"error,omitempty"`
|
||||
Jsonrpc string `json:"jsonrpc"`
|
||||
ID *int `json:"id"`
|
||||
Result any `json:"result,omitempty"`
|
||||
Error *Error `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
type Notification struct {
|
||||
|
|
|
|||
|
|
@ -1,10 +1,12 @@
|
|||
|
||||
package repositories
|
||||
|
||||
import "database/sql"
|
||||
import (
|
||||
"database/sql"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type TagRepository interface {
|
||||
Upsert(title string, path string) error
|
||||
Upsert(names []string) error
|
||||
}
|
||||
|
||||
type tagRepository struct {
|
||||
|
|
@ -15,14 +17,34 @@ func NewTagRepository(db *sql.DB) TagRepository {
|
|||
return &tagRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *tagRepository) Upsert(title string, path string) error {
|
||||
query := `
|
||||
INSERT INTO tags (title, path)
|
||||
VALUES ($1, $2)
|
||||
ON CONFLICT (slug) DO UPDATE
|
||||
SET title = EXCLUDED.title,
|
||||
path = EXCLUDED.path;
|
||||
`
|
||||
_, err := r.db.Exec(query, title, path)
|
||||
return err
|
||||
func (r *tagRepository) Upsert(names []string) error {
|
||||
if len(names) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
tx, err := r.db.Begin()
|
||||
if err != nil {
|
||||
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