diff --git a/cmd/main.go b/cmd/main.go index b602fc6..d210d4e 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -38,6 +38,7 @@ func main() { server.RegisterHandler("note/save", note.NewSaveNoteHandler(uow, noteRepo, tagService, noteService, linkService)) server.RegisterHandler("note/goto", note.NewGotoNoteHandler(noteRepo)) server.RegisterHandler("note/backlinks", note.NewGetBackLinksHandler(linkRepo, noteRepo)) + server.RegisterHandler("note/search_by_tag", note.NewGetNotesByTagHandler(noteRepo)) server.RegisterHandler("completion/link", completion.NewCompleteLinkHandler(linkRepo)) server.RegisterHandler("completion/tag", completion.NewCompleteTagHandler(tagRepo)) diff --git a/core/handlers/note/get_notes_by_tag_handler.go b/core/handlers/note/get_notes_by_tag_handler.go new file mode 100644 index 0000000..8c97caf --- /dev/null +++ b/core/handlers/note/get_notes_by_tag_handler.go @@ -0,0 +1,33 @@ +package note + +import ( + "context" + "encoding/json" + + "github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories" +) +type getNotesByTagCommand struct { + Tag string `json:"tag"` +} + +type GetNotesByTagHandler struct { + noteRepo repositories.INoteRepository +} + +func NewGetNotesByTagHandler(noteRepo repositories.INoteRepository) *GetNotesByTagHandler { + return &GetNotesByTagHandler{noteRepo: noteRepo} +} + +func (h *GetNotesByTagHandler) Handle(ctx context.Context, raw json.RawMessage) (any, error) { + var cmd getNotesByTagCommand + if err := json.Unmarshal(raw, &cmd); err != nil { + return nil, err + } + + notes, err := h.noteRepo.GetByTag(ctx, cmd.Tag) + if err != nil { + return nil, err + } + + return notes, nil +} diff --git a/dendrite b/dendrite index 72347f1..4b61683 100755 Binary files a/dendrite and b/dendrite differ diff --git a/persistence/repositories/note_repository.go b/persistence/repositories/note_repository.go index ae061ed..1d3b08a 100644 --- a/persistence/repositories/note_repository.go +++ b/persistence/repositories/note_repository.go @@ -15,6 +15,7 @@ type INoteRepository interface { Update(ctx context.Context, dbContext persistence.IDbContext, noteID, path, title, slug string) error GetBySlug(ctx context.Context, slug string) (*models.Note, error) GetByPath(ctx context.Context, path string) (*models.Note, error) + GetByTag(ctx context.Context, tag string) ([]*models.Note, error) GetNoteCount(ctx context.Context) (int, error) } @@ -97,7 +98,7 @@ func (r *noteRepository) GetByPath(ctx context.Context, path string) (*models.No } return nil, err } - + return models.NewNote(id, path, title, slug, createdAt, updatedAt), nil } @@ -113,3 +114,27 @@ func (r *noteRepository) GetNoteCount(ctx context.Context) (int, error) { return count, nil } + +func (r *noteRepository) GetByTag(ctx context.Context, tag string) ([]*models.Note, error) { + rows, err := r.readDBContext.QueryContext(ctx, ` + SELECT n.id, n.title, n.path, n.slug, n.created_at, n.updated_at + FROM note n + JOIN note_tag nt ON n.id = nt.note_id + JOIN tag t ON t.name = nt.tag_id + WHERE t.name LIKE ?`, "%"+tag+"%") + if err != nil { + return nil, err + } + defer rows.Close() + + notes := make([]*models.Note, 0) + for rows.Next() { + var id, title, path, slug, createdAt, updatedAt string + if err := rows.Scan(&id, &title, &path, &slug, &createdAt, &updatedAt); err != nil { + return nil, err + } + notes = append(notes, models.NewNote(id, path, title, slug, createdAt, updatedAt)) + } + + return notes, nil +}