diff --git a/persistence/repositories/note_repository.go b/persistence/repositories/note_repository.go index b90bda5..6832f41 100644 --- a/persistence/repositories/note_repository.go +++ b/persistence/repositories/note_repository.go @@ -10,3 +10,19 @@ 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 index ea07afc..c198560 100644 --- a/persistence/repositories/tag_repository.go +++ b/persistence/repositories/tag_repository.go @@ -4,7 +4,7 @@ package repositories import "database/sql" type TagRepository interface { - Upsert(title string, path string, slug string) error + Upsert(title string, path string) error } type tagRepository struct { @@ -15,3 +15,14 @@ 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 +}