feat: add backlinks and outlinks routes

This commit is contained in:
Thomas Bishop 2025-08-09 12:53:36 +01:00
parent a14574f808
commit f85867dd3a
4 changed files with 55 additions and 2 deletions

View file

@ -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

View file

@ -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

View file

@ -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]))
}

View file

@ -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 }