diff --git a/README.md b/README.md index 96d081a..0cff68d 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,47 @@ On the VPS, `eolas-api` runs as a `systemd` service. See the ## API +### Search + +``` +GET /search/ +``` + +```json + +{ + "count": 2, + "data": [ + { + "entry": "Test_values_in_Bash", + "excerpt": "# Test values in Bash\n\n`test` is a built-in command that is used to compare values or determine whether\nsomething is the case.\n\nWe..." + }, + { + "entry": "Testing_Python_code", + "excerpt": "...for a module called `lorem`, it will detzect the unit test\n files `lorem_test.py` and `test_lorem.py`.\n- In order to detect tests..." + }, +``` + +### Diagnostics + +#### Get broken links + +``` +GET /diagnostics/broken-links +``` + +```json +{ + "count": 1, + "data": [ + { + "source_entry_title": "Reducing_fractions", + "broken_link_title": "Equivalent%20fractions" + } + ] +} +``` + ### Entries #### Get all entries diff --git a/src/controllers/DiagnosticsController.js b/src/controllers/DiagnosticsController.js new file mode 100644 index 0000000..e175070 --- /dev/null +++ b/src/controllers/DiagnosticsController.js @@ -0,0 +1,18 @@ +export default class DiagnosticsController { + diagnosticsService + + constructor(diagnosticsService) { + this.diagnosticsService = diagnosticsService + } + + getBrokenLinks = (req, res) => { + const brokenLinks = this.diagnosticsService.getBrokenLinks() + if (!brokenLinks) { + return res.status(404).json({ + message: `Broken link list could not be retrieved`, + }) + } + + return res.json(brokenLinks) + } +} diff --git a/src/index.js b/src/index.js index 1f12c31..3a769aa 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,7 @@ import express from "express" import entries from "./routes/entries.js" import tags from "./routes/tags.js" import search from "./routes/search.js" +import diagnostics from "./routes/diagnostics.js" import cors from "cors" import { validateApiKey } from "./middlewear/auth.js" import morgan from "morgan" @@ -17,12 +18,13 @@ app.use("/", validateApiKey) app.use("/entries", entries) app.use("/tags", tags) app.use("/search", search) +app.use("/diagnostics", diagnostics) app.listen(port, () => { - console.info(`TB-INFO eolas-api running on NodeJS ${process.version}`) - console.info(`TB-INFO eolas-api server running at http://localhost:${port}`) + console.info(`TB-INFO eolas-api running on NodeJS ${process.version}`) + console.info(`TB-INFO eolas-api server running at http://localhost:${port}`) }) app.get("/health", (req, res) => { - res.status(200).json({ status: "ok" }) + res.status(200).json({ status: "ok" }) }) diff --git a/src/routes/diagnostics.js b/src/routes/diagnostics.js new file mode 100644 index 0000000..47d2d6c --- /dev/null +++ b/src/routes/diagnostics.js @@ -0,0 +1,11 @@ +import express from "express" +import DiagnosticsService from "../services/DiagnosticsService.js" +import DiagnosticsController from "../controllers/DiagnosticsController.js" +import database from "../db/connection.js" + +const router = express.Router() +const diagnosticsService = new DiagnosticsService(database) +const diagnosticsContoller = new DiagnosticsController(diagnosticsService) + +router.get("/broken-links", diagnosticsContoller.getBrokenLinks) +export default router diff --git a/src/services/DiagnosticsService.js b/src/services/DiagnosticsService.js new file mode 100644 index 0000000..8ff2a1e --- /dev/null +++ b/src/services/DiagnosticsService.js @@ -0,0 +1,17 @@ +import { GET_BROKEN_LINKS } from "../sql/diagnostics.js" + +export default class DiagnosticsService { + database + + constructor(database) { + this.database = database + } + + getBrokenLinks = () => { + const brokenLinks = this.database.prepare(GET_BROKEN_LINKS).all() + return { + count: brokenLinks.length, + data: brokenLinks, + } + } +} diff --git a/src/sql/diagnostics.js b/src/sql/diagnostics.js new file mode 100644 index 0000000..0743983 --- /dev/null +++ b/src/sql/diagnostics.js @@ -0,0 +1,3 @@ +const GET_BROKEN_LINKS = `SELECT * from broken_links` + +export { GET_BROKEN_LINKS }