feat(update_note): add update on save
This commit is contained in:
parent
122b0a271b
commit
5b437b6476
4 changed files with 29 additions and 1 deletions
|
|
@ -46,6 +46,8 @@ func (h *SaveNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (any,
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
slog.Debug("Parsed file", "path", cmd.Path, "slug", file.Slug, "title", file.Title, "tags", file.FrontMatter.Tags, "links", file.ExtractedLinks)
|
||||||
|
|
||||||
tx, err := h.uow.Begin()
|
tx, err := h.uow.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
@ -69,6 +71,10 @@ func (h *SaveNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (any,
|
||||||
slog.Debug("Failed to delete existing note metadata", "noteID", note.ID(), "error", err)
|
slog.Debug("Failed to delete existing note metadata", "noteID", note.ID(), "error", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if err = h.noteService.UpdateNote(ctx, tx, note.ID(), file.Path, file.Title, file.Slug); err != nil {
|
||||||
|
slog.Debug("Failed to update existing note", "noteID", note.ID(), "error", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tagModels, err := h.tagService.CreateTags(ctx, tx, file.FrontMatter.Tags)
|
tagModels, err := h.tagService.CreateTags(ctx, tx, file.FrontMatter.Tags)
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ import (
|
||||||
type INoteService interface {
|
type INoteService interface {
|
||||||
CreateNote(ctx context.Context, dbCtx persistence.IDbContext, path, title, slug string) (*models.Note, error)
|
CreateNote(ctx context.Context, dbCtx persistence.IDbContext, path, title, slug string) (*models.Note, error)
|
||||||
DeleteNoteMetaData(ctx context.Context, dbCtx persistence.IDbContext, noteID string) error
|
DeleteNoteMetaData(ctx context.Context, dbCtx persistence.IDbContext, noteID string) error
|
||||||
|
UpdateNote(ctx context.Context, dbCtx persistence.IDbContext, noteID, path, title, slug string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type noteService struct {
|
type noteService struct {
|
||||||
|
|
@ -28,7 +29,7 @@ func NewNoteService(
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *noteService) CreateNote(ctx context.Context, dbCtx persistence.IDbContext, path, title, slug string) (*models.Note, error) {
|
func (s *noteService) CreateNote(ctx context.Context, dbCtx persistence.IDbContext, path, title, slug string) (*models.Note, error) {
|
||||||
note:= models.CreateNote(path, title, slug)
|
note := models.CreateNote(path, title, slug)
|
||||||
if err := s.noteRepo.Insert(ctx, dbCtx, note); err != nil {
|
if err := s.noteRepo.Insert(ctx, dbCtx, note); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -36,6 +37,13 @@ func (s *noteService) CreateNote(ctx context.Context, dbCtx persistence.IDbConte
|
||||||
return note, nil
|
return note, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *noteService) UpdateNote(ctx context.Context, dbCtx persistence.IDbContext, noteID, path, title, slug string) error {
|
||||||
|
if err := s.noteRepo.Update(ctx, dbCtx, noteID, path, title, slug); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *noteService) DeleteNoteMetaData(ctx context.Context, dbCtx persistence.IDbContext, noteID string) error {
|
func (s *noteService) DeleteNoteMetaData(ctx context.Context, dbCtx persistence.IDbContext, noteID string) error {
|
||||||
if err := s.linkRepo.Delete(ctx, dbCtx, noteID); err != nil {
|
if err := s.linkRepo.Delete(ctx, dbCtx, noteID); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
||||||
BIN
dendrite
BIN
dendrite
Binary file not shown.
|
|
@ -11,6 +11,7 @@ import (
|
||||||
|
|
||||||
type INoteRepository interface {
|
type INoteRepository interface {
|
||||||
Insert(ctx context.Context, dbContext persistence.IDbContext, note *models.Note) error
|
Insert(ctx context.Context, dbContext persistence.IDbContext, note *models.Note) error
|
||||||
|
Update(ctx context.Context, dbContext persistence.IDbContext, noteID, path, title, slug string) error
|
||||||
GetBySlug(ctx context.Context, dbContext persistence.IDbContext, slug string) (*models.Note, error)
|
GetBySlug(ctx context.Context, dbContext persistence.IDbContext, slug string) (*models.Note, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -32,6 +33,19 @@ func (r *noteRepository) Insert(ctx context.Context, dbContext persistence.IDbCo
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *noteRepository) Update(ctx context.Context, dbContext persistence.IDbContext, noteID, path, title, slug string) error {
|
||||||
|
query := `
|
||||||
|
UPDATE note
|
||||||
|
SET path = ?, title = ?, slug = ?, updated_at = datetime('now')
|
||||||
|
WHERE id = ?;
|
||||||
|
`
|
||||||
|
_, err := dbContext.ExecContext(ctx, query, path, title, slug, noteID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (r *noteRepository) GetBySlug(ctx context.Context, dbContext persistence.IDbContext, slug string) (*models.Note, error) {
|
func (r *noteRepository) GetBySlug(ctx context.Context, dbContext persistence.IDbContext, slug string) (*models.Note, error) {
|
||||||
query := `SELECT id, title, path, slug, created_at, updated_at FROM note WHERE slug = ?`
|
query := `SELECT id, title, path, slug, created_at, updated_at FROM note WHERE slug = ?`
|
||||||
row := dbContext.QueryRowContext(ctx, query, slug)
|
row := dbContext.QueryRowContext(ctx, query, slug)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue