Merge pull request #3 from KristianJBorgwarth/feat/create-note
ref(models): change model property accessibility
This commit is contained in:
commit
e27a564445
11 changed files with 226 additions and 81 deletions
34
core/handlers/create_note.go
Normal file
34
core/handlers/create_note.go
Normal file
|
|
@ -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
|
||||||
|
}
|
||||||
|
|
@ -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
|
|
||||||
}
|
|
||||||
|
|
||||||
36
core/handlers/initialize.go
Normal file
36
core/handlers/initialize.go
Normal file
|
|
@ -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
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -1,7 +1,27 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
type Link struct {
|
type Link struct {
|
||||||
FromNoteID int
|
fromNoteID int
|
||||||
ToNoteID int
|
toNoteID int
|
||||||
Raw string
|
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
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,45 @@
|
||||||
|
|
||||||
package models
|
package models
|
||||||
|
|
||||||
type Note struct {
|
type Note struct {
|
||||||
ID int
|
id int
|
||||||
Path string
|
path string
|
||||||
Title string
|
title string
|
||||||
Slug string
|
slug string
|
||||||
CreatedAt string
|
createdAt string
|
||||||
UpdatedAt 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
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,21 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
type NoteTag struct {
|
type NoteTag struct {
|
||||||
NoteID int
|
noteID int
|
||||||
TagID 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
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,21 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
type Tag struct {
|
type Tag struct {
|
||||||
ID int
|
id int
|
||||||
Name string
|
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
|
||||||
}
|
}
|
||||||
|
|
|
||||||
2
core/template/doc.go
Normal file
2
core/template/doc.go
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
// Package template provides implementations for generating notes from templates
|
||||||
|
package template
|
||||||
|
|
@ -1,6 +1,3 @@
|
||||||
// Package repositories provides an interface for accessing dendrite index and configuration
|
// Package repositories provides an interface for accessing dendrite index and configuration
|
||||||
package repositories
|
package repositories
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
28
persistence/repositories/note_repository.go
Normal file
28
persistence/repositories/note_repository.go
Normal file
|
|
@ -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
|
||||||
|
}
|
||||||
28
persistence/repositories/tag_repository.go
Normal file
28
persistence/repositories/tag_repository.go
Normal file
|
|
@ -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
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue