diff --git a/cmd/main.go b/cmd/main.go index f5b264b..cea50d4 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -36,6 +36,7 @@ func main() { server.RegisterHandler("note/create", note.NewCreateNoteHandler(uow, tagService, noteRepo)) 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/backlinks", note.NewGetBackLinksHandler(linkRepo, noteRepo)) server.RegisterHandler("note/search_by_tag", note.NewGetNotesByTagHandler(noteRepo)) diff --git a/core/handlers/note/delete_note_handler.go b/core/handlers/note/delete_note_handler.go new file mode 100644 index 0000000..cd40b3e --- /dev/null +++ b/core/handlers/note/delete_note_handler.go @@ -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 +} diff --git a/core/services/note_service.go b/core/services/note_service.go index b2e42d4..513050b 100644 --- a/core/services/note_service.go +++ b/core/services/note_service.go @@ -11,6 +11,8 @@ import ( type INoteService interface { CreateNote(ctx context.Context, dbCtx persistence.IDbContext, path, title, slug string) (*models.Note, 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 GetNoteCount(ctx context.Context) (int, error) } @@ -57,6 +59,22 @@ func (s *noteService) DeleteNoteMetaData(ctx context.Context, dbCtx persistence. 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) { 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) +} diff --git a/dendrite b/dendrite index 6d6fa77..3a23b1f 100755 Binary files a/dendrite and b/dendrite differ diff --git a/persistence/repositories/note_repository.go b/persistence/repositories/note_repository.go index 4fd397a..5db6955 100644 --- a/persistence/repositories/note_repository.go +++ b/persistence/repositories/note_repository.go @@ -13,6 +13,7 @@ type INoteRepository interface { Insert(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 + Delete(ctx context.Context, dbContext persistence.IDbContext, noteID string) error Search(ctx context.Context, query string) ([]*models.Note, error) GetBySlug(ctx context.Context, slug 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 } +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) { query := `SELECT id, title, path, slug, created_at, updated_at FROM note WHERE slug = ?` row := r.readDBContext.QueryRowContext(ctx, query, slug)