ref(tag): change tag model to non-synthetic

This commit is contained in:
Kristian Borgwarth 2026-04-18 01:23:27 +02:00
parent c9bba78565
commit 271f6a265f
5 changed files with 56 additions and 34 deletions

View file

@ -1,15 +1,11 @@
package models
import "github.com/google/uuid"
type Tag struct {
id string
name string
}
func NewTag(id, name string) *Tag {
func NewTag(name string) *Tag {
return &Tag{
id: id,
name: name,
}
}
@ -17,17 +13,12 @@ func NewTag(id, name string) *Tag {
func CreateTags(tags []string) ([]*Tag) {
tagModels := make([]*Tag, len(tags))
for i, tag := range tags {
id, _ := uuid.NewV7()
tagModels[i] = NewTag(id.String(), tag)
tagModels[i] = NewTag(tag)
}
return tagModels
}
func (t *Tag) ID() string {
return t.id
}
func (t *Tag) Name() string {
return t.name
}

View file

@ -55,7 +55,7 @@ func (s *tagService) CreateTags(ctx context.Context, dbCtx persistence.IDbContex
func(s *tagService) CreateNoteTags(ctx context.Context, dbCtx persistence.IDbContext, noteID string, tags []*models.Tag) error {
tagIds := utils.Select(tags, func(t *models.Tag) string {
return t.ID()
return t.Name()
});
return s.tagRepo.InsertNoteTags(ctx, dbCtx, noteID, tagIds)

View file

@ -1,6 +1,5 @@
CREATE TABLE IF NOT EXISTS tag (
id TEXT PRIMARY KEY,
name TEXT UNIQUE
name TEXT PRIMARY KEY
);
CREATE TABLE IF NOT EXISTS note_tag (
@ -8,5 +7,5 @@ CREATE TABLE IF NOT EXISTS note_tag (
tag_id TEXT,
PRIMARY KEY (note_id, tag_id),
FOREIGN KEY(note_id) REFERENCES note(id) ON DELETE CASCADE,
FOREIGN KEY(tag_id) REFERENCES tag(id) ON DELETE CASCADE
FOREIGN KEY(tag_id) REFERENCES tag(name) ON DELETE CASCADE
);

View file

@ -30,25 +30,25 @@ func (r *tagRepository) Insert(ctx context.Context, dbCtx persistence.IDbContext
args := make([]any, 0, len(tags))
for _, tag := range tags {
placeholders = append(placeholders, "(?, ?)")
args = append(args, tag.ID(), tag.Name())
placeholders = append(placeholders, "(?)")
args = append(args, tag.Name())
}
query := "INSERT OR IGNORE INTO tag(id, name) VALUES " + strings.Join(placeholders, ",")
query := "INSERT OR IGNORE INTO tag(name) VALUES " + strings.Join(placeholders, ",")
_, err := dbCtx.ExecContext(ctx, query, args...)
return err
}
func (r *tagRepository) InsertNoteTags(ctx context.Context, dbCtx persistence.IDbContext, noteID string, tagIDs []string) error {
if len(tagIDs) == 0 {
func (r *tagRepository) InsertNoteTags(ctx context.Context, dbCtx persistence.IDbContext, noteID string, tags []string) error {
if len(tags) == 0 {
return nil
}
placeholders := make([]string, 0, len(tagIDs))
args := make([]any, 0, len(tagIDs))
placeholders := make([]string, 0, len(tags))
args := make([]any, 0, len(tags))
for _, tagID := range tagIDs {
for _, tagID := range tags {
placeholders = append(placeholders, "(?, ?)")
args = append(args, noteID, tagID)
}
@ -76,7 +76,7 @@ func (r *tagRepository) GetByNames(ctx context.Context, dbCtx persistence.IDbCon
args = append(args, name)
}
query := "SELECT id, name FROM tag WHERE name IN (" + strings.Join(placeholders, ",") + ")"
query := "SELECT name FROM tag WHERE name IN (" + strings.Join(placeholders, ",") + ")"
rows, err := dbCtx.QueryContext(ctx, query, args...)
if err != nil {
@ -86,12 +86,11 @@ func (r *tagRepository) GetByNames(ctx context.Context, dbCtx persistence.IDbCon
var tags []*models.Tag
for rows.Next() {
var id string
var name string
if err := rows.Scan(&id, &name); err != nil {
if err := rows.Scan(&name); err != nil {
return nil, err
}
tags = append(tags, models.NewTag(id, name))
tags = append(tags, models.NewTag(name))
}
return tags, nil

View file

@ -35,7 +35,22 @@ func (r *IndexRebuilder) RebuildIndex(vaultRoot string) error {
return err
}
if err = r.InsertData(tx, notes, links, tags, noteTags); err != nil {
if err = r.InsertNotes(tx, notes); err != nil {
tx.Rollback()
return err
}
if err = r.InsertLinks(tx, links); err != nil {
tx.Rollback()
return err
}
if err = r.InsertTags(tx, tags); err != nil {
tx.Rollback()
return err
}
if err = r.InsertNoteTags(tx, noteTags); err != nil {
tx.Rollback()
return err
}
@ -45,12 +60,30 @@ func (r *IndexRebuilder) RebuildIndex(vaultRoot string) error {
return nil
}
func (r *IndexRebuilder) InsertData(
tx *sql.Tx,
notes []*models.Note,
links []*models.Link,
tags []*models.Tag,
noteTags []*models.NoteTag) error {
func (r *IndexRebuilder) InsertNotes(tx *sql.Tx, notes []*models.Note) error {
noteStmt, err := tx.Prepare(`INSERT INTO note (id, title, slug, path) VALUES (?, ?, ?, ?)`)
if err != nil {
return err
}
for _, note := range notes {
if _, err = noteStmt.Exec(note.ID(), note.Title(), note.Slug(), note.Path()); err != nil {
return err
}
}
return nil
}
func (r *IndexRebuilder) InsertLinks(tx *sql.Tx, links []*models.Link) error {
panic("not implemented")
}
func (r *IndexRebuilder) InsertTags(tx *sql.Tx, tags []*models.Tag) error {
panic("not implemented")
}
func (r *IndexRebuilder) InsertNoteTags(tx *sql.Tx, noteTags []*models.NoteTag) error {
panic("not implemented")
}