feat(note/delete): added note delete handler (#38)
This commit is contained in:
parent
8a6543611e
commit
c0924342fb
5 changed files with 83 additions and 0 deletions
|
|
@ -36,6 +36,7 @@ func main() {
|
||||||
|
|
||||||
server.RegisterHandler("note/create", note.NewCreateNoteHandler(uow, tagService, noteRepo))
|
server.RegisterHandler("note/create", note.NewCreateNoteHandler(uow, tagService, noteRepo))
|
||||||
server.RegisterHandler("note/save", note.NewSaveNoteHandler(uow, noteRepo, tagService, noteService, linkService))
|
server.RegisterHandler("note/save", note.NewSaveNoteHandler(uow, noteRepo, tagService, noteService, linkService))
|
||||||
|
server.RegisterHandler("note/delete", note.NewDeleteNoteHandler(uow, tagService, noteService))
|
||||||
server.RegisterHandler("note/goto", note.NewGotoNoteHandler(noteRepo))
|
server.RegisterHandler("note/goto", note.NewGotoNoteHandler(noteRepo))
|
||||||
server.RegisterHandler("note/backlinks", note.NewGetBackLinksHandler(linkRepo, noteRepo))
|
server.RegisterHandler("note/backlinks", note.NewGetBackLinksHandler(linkRepo, noteRepo))
|
||||||
server.RegisterHandler("note/search_by_tag", note.NewGetNotesByTagHandler(noteRepo))
|
server.RegisterHandler("note/search_by_tag", note.NewGetNotesByTagHandler(noteRepo))
|
||||||
|
|
|
||||||
57
core/handlers/note/delete_note_handler.go
Normal file
57
core/handlers/note/delete_note_handler.go
Normal file
|
|
@ -0,0 +1,57 @@
|
||||||
|
package note
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
|
"github.com/KristianJBorgwarth/dendrite.daemon/core/services"
|
||||||
|
"github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories"
|
||||||
|
)
|
||||||
|
|
||||||
|
type deleteNoteCommand struct {
|
||||||
|
Path string `json:"path"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type DeleteNoteHandler struct {
|
||||||
|
uow *repositories.UnitOfWork
|
||||||
|
tagService services.ITagService
|
||||||
|
noteService services.INoteService
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewDeleteNoteHandler(
|
||||||
|
uow *repositories.UnitOfWork,
|
||||||
|
tagService services.ITagService,
|
||||||
|
noteService services.INoteService,
|
||||||
|
) *DeleteNoteHandler {
|
||||||
|
return &DeleteNoteHandler{uow, tagService, noteService}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *DeleteNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (any, error) {
|
||||||
|
var cmd deleteNoteCommand
|
||||||
|
|
||||||
|
if err := json.Unmarshal(raw, &cmd); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
tx, err := h.uow.Begin()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer h.uow.Rollback()
|
||||||
|
|
||||||
|
note, err := h.noteService.GetByPath(ctx, tx, cmd.Path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = h.noteService.DeleteNote(ctx, tx, note.ID())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if h.uow.Commit() != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
@ -11,6 +11,8 @@ 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
|
||||||
|
DeleteNote(ctx context.Context, dbCtx persistence.IDbContext, path string) error
|
||||||
|
GetByPath(ctx context.Context, dbCtx persistence.IDbContext, path string) (*models.Note, error)
|
||||||
UpdateNote(ctx context.Context, dbCtx persistence.IDbContext, noteID, path, title, slug string) error
|
UpdateNote(ctx context.Context, dbCtx persistence.IDbContext, noteID, path, title, slug string) error
|
||||||
GetNoteCount(ctx context.Context) (int, error)
|
GetNoteCount(ctx context.Context) (int, error)
|
||||||
}
|
}
|
||||||
|
|
@ -57,6 +59,22 @@ func (s *noteService) DeleteNoteMetaData(ctx context.Context, dbCtx persistence.
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *noteService) DeleteNote(ctx context.Context, dbCtx persistence.IDbContext, noteID string) error {
|
||||||
|
if err := s.DeleteNoteMetaData(ctx, dbCtx, noteID); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.noteRepo.Delete(ctx, dbCtx, noteID); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *noteService) GetNoteCount(ctx context.Context) (int, error) {
|
func (s *noteService) GetNoteCount(ctx context.Context) (int, error) {
|
||||||
return s.noteRepo.GetNoteCount(ctx)
|
return s.noteRepo.GetNoteCount(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *noteService) GetByPath(ctx context.Context, dbCtx persistence.IDbContext, path string) (*models.Note, error) {
|
||||||
|
return s.noteRepo.GetByPath(ctx,path)
|
||||||
|
}
|
||||||
|
|
|
||||||
BIN
dendrite
BIN
dendrite
Binary file not shown.
|
|
@ -13,6 +13,7 @@ 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
|
||||||
InsertRange(ctx context.Context, dbContext persistence.IDbContext, note []*models.Note) error
|
InsertRange(ctx context.Context, dbContext persistence.IDbContext, note []*models.Note) error
|
||||||
Update(ctx context.Context, dbContext persistence.IDbContext, noteID, path, title, slug string) error
|
Update(ctx context.Context, dbContext persistence.IDbContext, noteID, path, title, slug string) error
|
||||||
|
Delete(ctx context.Context, dbContext persistence.IDbContext, noteID string) error
|
||||||
Search(ctx context.Context, query string) ([]*models.Note, error)
|
Search(ctx context.Context, query string) ([]*models.Note, error)
|
||||||
GetBySlug(ctx context.Context, slug string) (*models.Note, error)
|
GetBySlug(ctx context.Context, slug string) (*models.Note, error)
|
||||||
GetByPath(ctx context.Context, path string) (*models.Note, error)
|
GetByPath(ctx context.Context, path string) (*models.Note, error)
|
||||||
|
|
@ -70,6 +71,12 @@ func (r *noteRepository) Update(ctx context.Context, dbContext persistence.IDbCo
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *noteRepository) Delete(ctx context.Context, dbContext persistence.IDbContext, noteID string) error {
|
||||||
|
query := `DELETE FROM note WHERE id= ?`
|
||||||
|
_, err := dbContext.ExecContext(ctx, query, noteID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
func (r *noteRepository) GetBySlug(ctx context.Context, slug string) (*models.Note, error) {
|
func (r *noteRepository) GetBySlug(ctx context.Context, 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 := r.readDBContext.QueryRowContext(ctx, query, slug)
|
row := r.readDBContext.QueryRowContext(ctx, query, slug)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue