feat: add backlinks and outlinks routes
This commit is contained in:
parent
a14574f808
commit
f85867dd3a
4 changed files with 55 additions and 2 deletions
|
|
@ -29,6 +29,31 @@ export default class EntriesController {
|
||||||
return res.json(entries)
|
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) => {
|
getEntriesForTag = (req, res) => {
|
||||||
const tag = req.params.tag
|
const tag = req.params.tag
|
||||||
const sort = req.query.sort
|
const sort = req.query.sort
|
||||||
|
|
|
||||||
|
|
@ -9,5 +9,7 @@ const entriesController = new EntriesController(entriesService)
|
||||||
|
|
||||||
router.get("/", entriesController.getAllEntries)
|
router.get("/", entriesController.getAllEntries)
|
||||||
router.get("/:title", entriesController.getEntry)
|
router.get("/:title", entriesController.getEntry)
|
||||||
|
router.get("/backlinks/:title", entriesController.getBacklinksForEntry)
|
||||||
|
router.get("/outlinks/:title", entriesController.getOutlinksForEntry)
|
||||||
router.get("/tag/:tag", entriesController.getEntriesForTag)
|
router.get("/tag/:tag", entriesController.getEntriesForTag)
|
||||||
export default router
|
export default router
|
||||||
|
|
|
||||||
|
|
@ -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 {
|
export default class EntriesService {
|
||||||
database
|
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) => {
|
_sortByTitle = (entries, fieldName) => {
|
||||||
return entries.sort((a, b) => a[fieldName].localeCompare(b[fieldName]))
|
return entries.sort((a, b) => a[fieldName].localeCompare(b[fieldName]))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,11 @@ const GET_ENTRY = `SELECT * FROM entries WHERE title = ?`
|
||||||
|
|
||||||
const GET_ALL_ENTRIES = `SELECT title, last_modified FROM entries`
|
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 = ?`
|
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 }
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue