feat(models): updated model ids

This commit is contained in:
Kristian Borgwarth 2026-04-03 12:02:31 +02:00
parent 72ac54213e
commit ddd8ac837e
5 changed files with 35 additions and 22 deletions

View file

@ -5,8 +5,10 @@ import (
"encoding/json" "encoding/json"
"github.com/KristianJBorgwarth/dendrite.daemon/core/frontmatter" "github.com/KristianJBorgwarth/dendrite.daemon/core/frontmatter"
"github.com/KristianJBorgwarth/dendrite.daemon/core/models"
"github.com/KristianJBorgwarth/dendrite.daemon/core/template" "github.com/KristianJBorgwarth/dendrite.daemon/core/template"
"github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories" "github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories"
"github.com/google/uuid"
) )
type createNoteCommand struct { type createNoteCommand struct {
@ -26,6 +28,7 @@ func NewCreateNoteHandler(uow *repositories.UnitOfWork) *CreateNoteHandler {
func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (any, error) { func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (any, error) {
var cmd createNoteCommand var cmd createNoteCommand
if err := json.Unmarshal(raw, &cmd); err != nil { if err := json.Unmarshal(raw, &cmd); err != nil {
return nil, err return nil, err
} }
@ -51,6 +54,11 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (an
tagRepo := repositories.NewTagRepository(tx) tagRepo := repositories.NewTagRepository(tx)
noteRepo := repositories.NewNoteRepository(tx) noteRepo := repositories.NewNoteRepository(tx)
tagModels := make([]models.Tag, len(tags))
for i, tag := range tags {
tagModels[i] = models.NewTag(uuidv7.New(), tag)
}
if err = tagRepo.Upsert(ctx, tags); err != nil { if err = tagRepo.Upsert(ctx, tags); err != nil {
return nil, err return nil, err
} }
@ -59,6 +67,7 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (an
return nil, err return nil, err
} }
h.uow.FileStore.Stage(cmd.Path, data) h.uow.FileStore.Stage(cmd.Path, data)
if err = h.uow.Commit(); err != nil { if err = h.uow.Commit(); err != nil {

View file

@ -1,7 +1,7 @@
package models package models
type Note struct { type Note struct {
id int id string
path string path string
title string title string
slug string slug string
@ -9,7 +9,7 @@ type Note struct {
updatedAt string updatedAt string
} }
func NewNote(id int, path, title, slug, createdAt, updatedAt string) *Note { func NewNote(id, path, title, slug, createdAt, updatedAt string) *Note {
return &Note{ return &Note{
id: id, id: id,
path: path, path: path,
@ -20,7 +20,7 @@ func NewNote(id int, path, title, slug, createdAt, updatedAt string) *Note {
} }
} }
func (n *Note) ID() int { func (n *Note) ID() string {
return n.id return n.id
} }

View file

@ -1,21 +1,21 @@
package models package models
type NoteTag struct { type NoteTag struct {
noteID int noteID string
tagID int tagID string
} }
func NewNoteTag(noteID, tagID int) *NoteTag { func NewNoteTag(noteID, tagID string) *NoteTag {
return &NoteTag{ return &NoteTag{
noteID: noteID, noteID: noteID,
tagID: tagID, tagID: tagID,
} }
} }
func (nt *NoteTag) NoteID() int { func (nt *NoteTag) NoteID() string {
return nt.noteID return nt.noteID
} }
func (nt *NoteTag) TagID() int { func (nt *NoteTag) TagID() string {
return nt.tagID return nt.tagID
} }

View file

@ -1,18 +1,20 @@
package models package models
import "github.com/google/uuid"
type Tag struct { type Tag struct {
id int id string
name string name string
} }
func NewTag(id int, name string) *Tag { func NewTag(id uuid.UUID, name string) *Tag {
return &Tag{ return &Tag{
id: id, id: id.String(),
name: name, name: name,
} }
} }
func (t *Tag) ID() int { func (t *Tag) ID() string {
return t.id return t.id
} }

View file

@ -4,31 +4,33 @@ import (
"context" "context"
"database/sql" "database/sql"
"strings" "strings"
"github.com/KristianJBorgwarth/dendrite.daemon/core/models"
) )
type TagRepository interface { type ITagRepository interface {
Upsert(ctx context.Context, names []string) error Upsert(ctx context.Context, tags []models.Tag) error
} }
type tagRepository struct { type tagRepository struct {
Transaction *sql.Tx Transaction *sql.Tx
} }
func NewTagRepository(tx *sql.Tx) TagRepository { func NewTagRepository(tx *sql.Tx) ITagRepository {
return &tagRepository{Transaction: tx} return &tagRepository{Transaction: tx}
} }
func (r *tagRepository) Upsert(ctx context.Context, names []string) error { func (r *tagRepository) Upsert(ctx context.Context, tags []models.Tag) error {
if len(names) == 0 { if len(tags) == 0 {
return nil return nil
} }
placeholders := make([]string, 0, len(names)) placeholders := make([]string, 0, len(tags))
args := make([]any, 0, len(names)) args := make([]any, 0, len(tags))
for _, name := range names { for _, tag := range tags {
placeholders = append(placeholders, "(?)") placeholders = append(placeholders, "(?, ?)")
args = append(args, name) args = append(args, tag.ID(), tag.Name())
} }
query := "INSERT OR IGNORE INTO tags(name) VALUES " + strings.Join(placeholders, ",") query := "INSERT OR IGNORE INTO tags(name) VALUES " + strings.Join(placeholders, ",")