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"
"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/persistence/repositories"
"github.com/google/uuid"
)
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) {
var cmd createNoteCommand
if err := json.Unmarshal(raw, &cmd); err != nil {
return nil, err
}
@ -51,6 +54,11 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (an
tagRepo := repositories.NewTagRepository(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 {
return nil, err
}
@ -59,6 +67,7 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (an
return nil, err
}
h.uow.FileStore.Stage(cmd.Path, data)
if err = h.uow.Commit(); err != nil {

View file

@ -1,7 +1,7 @@
package models
type Note struct {
id int
id string
path string
title string
slug string
@ -9,7 +9,7 @@ type Note struct {
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{
id: id,
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
}

View file

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

View file

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

View file

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