fix(repos): pass id to values
This commit is contained in:
parent
4da1ae0322
commit
bf2ff4066b
5 changed files with 42 additions and 17 deletions
|
|
@ -48,12 +48,13 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (an
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
defer h.uow.Rollback()
|
defer h.uow.Rollback()
|
||||||
|
|
||||||
tagRepo := repositories.NewTagRepository(tx)
|
tagRepo := repositories.NewTagRepository(tx)
|
||||||
noteRepo := repositories.NewNoteRepository(tx)
|
noteRepo := repositories.NewNoteRepository(tx)
|
||||||
|
|
||||||
tagModels, err := models.NewTags(tags)
|
tagModels, err := models.CreateTags(tags)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -62,7 +63,9 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (an
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = noteRepo.Upsert(ctx, cmd.Title, cmd.Path, slug); err != nil {
|
note := models.CreateNote(cmd.Path, cmd.Title, slug)
|
||||||
|
|
||||||
|
if err = noteRepo.Upsert(ctx, note); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,11 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
type Note struct {
|
type Note struct {
|
||||||
id string
|
id string
|
||||||
path string
|
path string
|
||||||
|
|
@ -20,6 +26,21 @@ func NewNote(id, path, title, slug, createdAt, updatedAt string) *Note {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CreateNote(path, title, slug string) *Note {
|
||||||
|
id, err := uuid.NewV7()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return &Note{
|
||||||
|
id: id.String(),
|
||||||
|
path: path,
|
||||||
|
title: title,
|
||||||
|
slug: slug,
|
||||||
|
createdAt: time.Now().Format("2006-01-02 15:04:05"),
|
||||||
|
updatedAt: time.Now().Format("2006-01-02 15:04:05"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (n *Note) ID() string {
|
func (n *Note) ID() string {
|
||||||
return n.id
|
return n.id
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,22 +7,21 @@ type Tag struct {
|
||||||
name string
|
name string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTag(id string, name string) *Tag {
|
func NewTag(name string) *Tag {
|
||||||
|
id, err := uuid.NewV7()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
return &Tag{
|
return &Tag{
|
||||||
id: id,
|
id: id.String(),
|
||||||
name: name,
|
name: name,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTags(tags []string) ([]*Tag, error) {
|
func CreateTags(tags []string) ([]*Tag, error) {
|
||||||
|
|
||||||
tagModels := make([]*Tag, len(tags))
|
tagModels := make([]*Tag, len(tags))
|
||||||
for i, tag := range tags {
|
for i, tag := range tags {
|
||||||
id, err := uuid.NewV7()
|
tagModels[i] = NewTag(tag)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
tagModels[i] = NewTag(id.String(), tag)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return tagModels, nil
|
return tagModels, nil
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,12 @@ package repositories
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
|
||||||
|
"github.com/KristianJBorgwarth/dendrite.daemon/core/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
type NoteRepository interface {
|
type NoteRepository interface {
|
||||||
Upsert(ctx context.Context, title string, path string, slug string) error
|
Upsert(ctx context.Context, note *models.Note) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type noteRepository struct {
|
type noteRepository struct {
|
||||||
|
|
@ -17,14 +19,14 @@ func NewNoteRepository(tx *sql.Tx) NoteRepository {
|
||||||
return ¬eRepository{Transaction: tx}
|
return ¬eRepository{Transaction: tx}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *noteRepository) Upsert(ctx context.Context, title string, path string, slug string) error {
|
func (r *noteRepository) Upsert(ctx context.Context, note *models.Note) error {
|
||||||
query := `
|
query := `
|
||||||
INSERT INTO notes (title, path, slug, created_at, updated_at)
|
INSERT INTO notes (id, title, path, slug, created_at, updated_at)
|
||||||
VALUES (?, ?, ?, datetime('now'), datetime('now'))
|
VALUES (?, ?, ?, ?, datetime('now'), datetime('now'))
|
||||||
ON CONFLICT (slug) DO UPDATE
|
ON CONFLICT (slug) DO UPDATE
|
||||||
SET title = EXCLUDED.title,
|
SET title = EXCLUDED.title,
|
||||||
path = EXCLUDED.path;
|
path = EXCLUDED.path;
|
||||||
`
|
`
|
||||||
_, err := r.Transaction.ExecContext(ctx, query, title, path, slug)
|
_, err := r.Transaction.ExecContext(ctx, query, note.ID(), note.Title(), note.Path(), note.Slug())
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,7 @@ func (r *tagRepository) Upsert(ctx context.Context, tags []*models.Tag) error {
|
||||||
args = append(args, tag.ID(), tag.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(id, name) VALUES " + strings.Join(placeholders, ",")
|
||||||
|
|
||||||
_, err := r.Transaction.ExecContext(ctx, query, args...)
|
_, err := r.Transaction.ExecContext(ctx, query, args...)
|
||||||
return err
|
return err
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue