From f85867dd3aecfd12b5e1f550be2e09f73e70544d Mon Sep 17 00:00:00 2001 From: thomasabishop Date: Sat, 9 Aug 2025 12:53:36 +0100 Subject: [PATCH] feat: add backlinks and outlinks routes --- src/controllers/EntriesController.js | 25 +++++++++++++++++++++++++ src/routes/entries.js | 2 ++ src/services/EntriesService.js | 23 ++++++++++++++++++++++- src/sql/entries.js | 7 ++++++- 4 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/controllers/EntriesController.js b/src/controllers/EntriesController.js index 3c85684..8288830 100644 --- a/src/controllers/EntriesController.js +++ b/src/controllers/EntriesController.js @@ -29,6 +29,31 @@ export default class EntriesController { return res.json(entries) } + getBacklinksForEntry = (req, res) => { + const title = req.params.title + const entries = this.entriesService.getBacklinksForEntry(title) + if (!entries) { + return res.status(404).json({ + message: `Backlinks could not be retrieved for entry ${title}`, + }) + } + + return res.json(entries) + } + + getOutlinksForEntry = (req, res) => { + const title = req.params.title + const entries = this.entriesService.getOutlinksForEntry(title) + if (!entries) { + return res.status(404).json({ + message: `Outlinks could not be retrieved for entry ${title}`, + }) + } + + return res.json(entries) + } + + getEntriesForTag = (req, res) => { const tag = req.params.tag const sort = req.query.sort diff --git a/src/routes/entries.js b/src/routes/entries.js index bf486b6..ac4d952 100644 --- a/src/routes/entries.js +++ b/src/routes/entries.js @@ -9,5 +9,7 @@ const entriesController = new EntriesController(entriesService) router.get("/", entriesController.getAllEntries) router.get("/:title", entriesController.getEntry) +router.get("/backlinks/:title", entriesController.getBacklinksForEntry) +router.get("/outlinks/:title", entriesController.getOutlinksForEntry) router.get("/tag/:tag", entriesController.getEntriesForTag) export default router diff --git a/src/services/EntriesService.js b/src/services/EntriesService.js index 2e02335..3102fbe 100644 --- a/src/services/EntriesService.js +++ b/src/services/EntriesService.js @@ -1,4 +1,4 @@ -import { GET_ALL_ENTRIES, GET_ENTRY, GET_ENTRIES_FOR_TAG } from "../sql/entries.js" +import { GET_ALL_ENTRIES, GET_ENTRY, GET_ENTRIES_FOR_TAG, GET_BACKLINKS_FOR_ENTRY, GET_OUTLINKS_FOR_ENTRY } from "../sql/entries.js" export default class EntriesService { database @@ -36,6 +36,27 @@ export default class EntriesService { } } + getBacklinksForEntry = (title) => { + const backlinks = this.database.prepare(GET_BACKLINKS_FOR_ENTRY).all(title) + const sorted = this._sortByTitle(backlinks, "source_entry_title") + const list = sorted.flatMap((i) => i.source_entry_title) + return { + count: backlinks.length, + data: list + } + } + + getOutlinksForEntry = (title) => { + const outlinks = this.database.prepare(GET_OUTLINKS_FOR_ENTRY).all(title) + const sorted = this._sortByTitle(outlinks, "target_entry_title") + const list = sorted.flatMap((i) => i.target_entry_title) + return { + count: outlinks.length, + data: list + } + + } + _sortByTitle = (entries, fieldName) => { return entries.sort((a, b) => a[fieldName].localeCompare(b[fieldName])) } diff --git a/src/sql/entries.js b/src/sql/entries.js index 9272547..649d766 100644 --- a/src/sql/entries.js +++ b/src/sql/entries.js @@ -2,6 +2,11 @@ const GET_ENTRY = `SELECT * FROM entries WHERE title = ?` const GET_ALL_ENTRIES = `SELECT title, last_modified FROM entries` +const GET_OUTLINKS_FOR_ENTRY = `SELECT target_entry_title FROM backlinks WHERE source_entry_title = ?` + +const GET_BACKLINKS_FOR_ENTRY = `SELECT source_entry_title FROM backlinks WHERE target_entry_title = ?` + const GET_ENTRIES_FOR_TAG = `SELECT entry_title from entries_tags WHERE tag_name = ?` -export { GET_ENTRY, GET_ALL_ENTRIES, GET_ENTRIES_FOR_TAG } + +export { GET_ENTRY, GET_ALL_ENTRIES, GET_BACKLINKS_FOR_ENTRY, GET_OUTLINKS_FOR_ENTRY, GET_ENTRIES_FOR_TAG }