feat(note/search): added search by tag notes (#34)
This commit is contained in:
parent
fda0286690
commit
ce21a7fc9b
4 changed files with 60 additions and 1 deletions
|
|
@ -38,6 +38,7 @@ func main() {
|
||||||
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/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("completion/link", completion.NewCompleteLinkHandler(linkRepo))
|
server.RegisterHandler("completion/link", completion.NewCompleteLinkHandler(linkRepo))
|
||||||
server.RegisterHandler("completion/tag", completion.NewCompleteTagHandler(tagRepo))
|
server.RegisterHandler("completion/tag", completion.NewCompleteTagHandler(tagRepo))
|
||||||
|
|
|
||||||
33
core/handlers/note/get_notes_by_tag_handler.go
Normal file
33
core/handlers/note/get_notes_by_tag_handler.go
Normal file
|
|
@ -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
|
||||||
|
}
|
||||||
BIN
dendrite
BIN
dendrite
Binary file not shown.
|
|
@ -15,6 +15,7 @@ type INoteRepository interface {
|
||||||
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
|
||||||
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)
|
||||||
|
GetByTag(ctx context.Context, tag string) ([]*models.Note, error)
|
||||||
GetNoteCount(ctx context.Context) (int, 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 nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return models.NewNote(id, path, title, slug, createdAt, updatedAt), nil
|
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
|
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
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue