diff --git a/core/handlers/create_note.go b/core/handlers/create_note.go new file mode 100644 index 0000000..df71e3e --- /dev/null +++ b/core/handlers/create_note.go @@ -0,0 +1,34 @@ +package handlers + +import ( + "encoding/json" + + "github.com/KristianJBorgwarth/dendrite.daemon/core/rpc" + "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{ + repository repositories.NoteRepository +} + +func (h CreateNoteHandler) Handle(params []byte) (any, *rpc.Error) { + var cmd createNoteCommand + if err := json.Unmarshal(params, &cmd); err != nil { + return nil, &rpc.Error{Code: -32602, Message: "invalid params"} + } + + err := h.repository.Upsert(cmd.Title, cmd.Path, cmd.Path) + + if err != nil { + return nil, &rpc.Error{Code: -1, Message: err.Error()} + } + + return map[string]any{"status": "ok"}, nil +} diff --git a/core/handlers/init.go b/core/handlers/init.go deleted file mode 100644 index bf35334..0000000 --- a/core/handlers/init.go +++ /dev/null @@ -1,64 +0,0 @@ -package handlers - -import ( - "encoding/json" - "github.com/KristianJBorgwarth/dendrite.daemon/config" - "github.com/KristianJBorgwarth/dendrite.daemon/core/rpc" - "github.com/KristianJBorgwarth/dendrite.daemon/persistence" -) - -type initializeCommand struct { - VaultPath string `json:"vaultPath"` - TemplateDir string `json:"templateDir"` - - ScratchNote struct { - Dir string `json:"dir"` - TemplateName string `json:"templateName"` - } `json:"scratchNote"` - - DailyNote struct { - Dir string `json:"dir"` - TemplateName string `json:"templateName"` - FilenameFormat string `json:"filenameFormat"` - } `json:"dailyNote"` -} - -type InitializeHandler struct{} - -func (h InitializeHandler) Handle(raw json.RawMessage) (any, *rpc.Error) { - var params initializeCommand - - if err := json.Unmarshal(raw, ¶ms); err != nil { - return nil, &rpc.Error{ - Code: -32602, - Message: "invalid params: " + err.Error(), - } - } - - cfg := &config.Config{ - VaultPath: params.VaultPath, - TemplateDir: params.TemplateDir, - ScratchNote: config.ScratchConfig{ - Dir: params.ScratchNote.Dir, - TemplateName: params.ScratchNote.TemplateName, - }, - DailyNote: config.DailyConfig{ - Dir: params.DailyNote.Dir, - TemplateName: params.DailyNote.TemplateName, - FilenameFormat: params.DailyNote.FilenameFormat, - }, - } - - cfg.SetDefaults() - - err := persistence.InitializeIndex(cfg.VaultPath) - if err != nil { - return nil, &rpc.Error{ - Code: -1, - Message: "failed to initialize index: " + err.Error(), - } - } - - return cfg, nil -} - diff --git a/core/handlers/initialize.go b/core/handlers/initialize.go new file mode 100644 index 0000000..c6cf01f --- /dev/null +++ b/core/handlers/initialize.go @@ -0,0 +1,36 @@ +package handlers + +import ( + "encoding/json" + "github.com/KristianJBorgwarth/dendrite.daemon/core/rpc" + "github.com/KristianJBorgwarth/dendrite.daemon/persistence" +) + +type initializeCommand struct { + VaultPath string `json:"vaultPath"` +} + +type InitializeHandler struct{} + +func (h InitializeHandler) Handle(raw json.RawMessage) (any, *rpc.Error) { + var params initializeCommand + + if err := json.Unmarshal(raw, ¶ms); err != nil { + return nil, &rpc.Error{ + Code: -32602, + Message: "invalid params: " + err.Error(), + } + } + + + err := persistence.InitializeIndex(params.VaultPath) + if err != nil { + return nil, &rpc.Error{ + Code: -1, + Message: "failed to initialize index: " + err.Error(), + } + } + + return nil, nil +} + diff --git a/core/models/links.go b/core/models/links.go index 412cd0f..538a2d8 100644 --- a/core/models/links.go +++ b/core/models/links.go @@ -1,7 +1,27 @@ package models type Link struct { - FromNoteID int - ToNoteID int - Raw string + fromNoteID int + toNoteID int + raw string +} + +func NewLink(fromNoteID, toNoteID int, raw string) *Link { + return &Link{ + fromNoteID: fromNoteID, + toNoteID: toNoteID, + raw: raw, + } +} + +func (l *Link) FromNoteID() int { + return l.fromNoteID +} + +func (l *Link) ToNoteID() int { + return l.toNoteID +} + +func (l *Link) Raw() string { + return l.raw } diff --git a/core/models/note.go b/core/models/note.go index 4d478b9..c95b71b 100644 --- a/core/models/note.go +++ b/core/models/note.go @@ -1,11 +1,45 @@ - package models type Note struct { - ID int - Path string - Title string - Slug string - CreatedAt string - UpdatedAt string + id int + path string + title string + slug string + createdAt string + updatedAt string +} + +func NewNote(id int, path, title, slug, createdAt, updatedAt string) *Note { + return &Note{ + id: id, + path: path, + title: title, + slug: slug, + createdAt: createdAt, + updatedAt: updatedAt, + } +} + +func (n *Note) ID() int { + return n.id +} + +func (n *Note) Path() string { + return n.path +} + +func (n *Note) Title() string { + return n.title +} + +func (n *Note) Slug() string { + return n.slug +} + +func (n *Note) CreatedAt() string { + return n.createdAt +} + +func (n *Note) UpdatedAt() string { + return n.updatedAt } diff --git a/core/models/note_tag.go b/core/models/note_tag.go index a74d771..ec36634 100644 --- a/core/models/note_tag.go +++ b/core/models/note_tag.go @@ -1,6 +1,21 @@ package models type NoteTag struct { - NoteID int - TagID int + noteID int + tagID int +} + +func NewNoteTag(noteID, tagID int) *NoteTag { + return &NoteTag{ + noteID: noteID, + tagID: tagID, + } +} + +func (nt *NoteTag) NoteID() int { + return nt.noteID +} + +func (nt *NoteTag) TagID() int { + return nt.tagID } diff --git a/core/models/tag.go b/core/models/tag.go index 0bc7531..e36d306 100644 --- a/core/models/tag.go +++ b/core/models/tag.go @@ -1,6 +1,21 @@ package models type Tag struct { - ID int - Name string + id int + name string +} + +func NewTag(id int, name string) *Tag { + return &Tag{ + id: id, + name: name, + } +} + +func (t *Tag) ID() int { + return t.id +} + +func (t *Tag) Name() string { + return t.name } diff --git a/core/template/doc.go b/core/template/doc.go new file mode 100644 index 0000000..66c2031 --- /dev/null +++ b/core/template/doc.go @@ -0,0 +1,2 @@ +// Package template provides implementations for generating notes from templates +package template diff --git a/persistence/repositories/doc.go b/persistence/repositories/doc.go index 0660717..aa98412 100644 --- a/persistence/repositories/doc.go +++ b/persistence/repositories/doc.go @@ -1,6 +1,3 @@ // Package repositories provides an interface for accessing dendrite index and configuration package repositories - - - diff --git a/persistence/repositories/note_repository.go b/persistence/repositories/note_repository.go new file mode 100644 index 0000000..6832f41 --- /dev/null +++ b/persistence/repositories/note_repository.go @@ -0,0 +1,28 @@ +package repositories + +import "database/sql" + +type NoteRepository interface { + Upsert(title string, path string, slug string) error +} + +type noteRepository struct { + db *sql.DB +} + +func NewNoteRepository(db *sql.DB) NoteRepository { + return ¬eRepository{db: db} +} + + +func (r *noteRepository) Upsert(title string, path string, slug string) error { + query := ` + INSERT INTO notes (title, path, slug) + VALUES ($1, $2, $3) + ON CONFLICT (slug) DO UPDATE + SET title = EXCLUDED.title, + path = EXCLUDED.path; + ` + _, err := r.db.Exec(query, title, path, slug) + return err +} diff --git a/persistence/repositories/tag_repository.go b/persistence/repositories/tag_repository.go new file mode 100644 index 0000000..c198560 --- /dev/null +++ b/persistence/repositories/tag_repository.go @@ -0,0 +1,28 @@ + +package repositories + +import "database/sql" + +type TagRepository interface { + Upsert(title string, path string) error +} + +type tagRepository struct { + db *sql.DB +} + +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 +}