diff --git a/src/controllers/EntriesController.js b/src/controllers/EntriesController.js index 1e3be4f..e7c96a2 100644 --- a/src/controllers/EntriesController.js +++ b/src/controllers/EntriesController.js @@ -5,7 +5,27 @@ export class EntriesController { this.entriesService = entriesService } - getEntry = async (req, res) => { - const response = await this.entriesService.getEntry() + getEntry = (req, res) => { + const title = req.params.title + const entry = this.entriesService.getEntry(title) + if (!entry) { + return res.status(404).json({ + message: `No entry found with title ${title}`, + }) + } + + return res.json(entry) + } + + getAllEntries = (req, res) => { + const { sort, limit } = req.query + const entries = this.entriesService.getAllEntries(sort, limit) + if (!entries) { + return res.status(404).json({ + message: `Entries list could not be retrieved`, + }) + } + + return res.json(entries) } } diff --git a/src/db/connection.js b/src/db/connection.js new file mode 100644 index 0000000..81dbcd1 --- /dev/null +++ b/src/db/connection.js @@ -0,0 +1,7 @@ +import { DatabaseSync } from "node:sqlite" + +const DATABASE_PATH = "/home/thomas/repos/eolas-api/data/eolas.db" + +const database = new DatabaseSync(DATABASE_PATH) + +export default database diff --git a/src/index.js b/src/index.js index 68b64b8..28789c8 100644 --- a/src/index.js +++ b/src/index.js @@ -1,11 +1,12 @@ import express from "express" -import entries from "./routes/entries" +import entries from "./routes/entries.js" const app = express() -const port = 3000 + +const port = process.env.PORT || 3000 app.use(express.json()) app.use("/entries", entries) app.listen(port, () => { - console.info(`INFO Server running at http://localhost:${port}`) + console.info(`INFO Server running at http://localhost:${port}`) }) diff --git a/src/lib/sorters.js b/src/lib/sorters.js new file mode 100644 index 0000000..9647637 --- /dev/null +++ b/src/lib/sorters.js @@ -0,0 +1,9 @@ +const sortByDate = (entries) => { + return entries.sort((a, b) => new Date(b.last_modified) - new Date(a.last_modified)) +} + +const sortByTitle = (entries) => { + return entries.sort((a, b) => a.title.localeCompare(b.title)) +} + +export { sortByDate, sortByTitle } diff --git a/src/routes/entries.js b/src/routes/entries.js index 513278d..a629150 100644 --- a/src/routes/entries.js +++ b/src/routes/entries.js @@ -1,12 +1,13 @@ import express from "express" -import { EntriesService } from "../services/EntriesService" -import { EntriesController } from "../controllers/EntriesController" -import { EntriesService } from "../services/EntriesService" +import { EntriesService } from "../services/EntriesService.js" +import { EntriesController } from "../controllers/EntriesController.js" +import database from "../db/connection.js" const router = express.Router() -const entriesService = new EntriesService() +const entriesService = new EntriesService(database) const entriesController = new EntriesController(entriesService) -router.get("/:entry", entriesController.getEntry) +router.get("/", entriesController.getAllEntries) +router.get("/:title", entriesController.getEntry) export default router diff --git a/src/services/EntriesService.js b/src/services/EntriesService.js index 5a47c6d..c569bd2 100644 --- a/src/services/EntriesService.js +++ b/src/services/EntriesService.js @@ -1,7 +1,27 @@ -export class EntriesService { - // TODO add DatabaseConnectorService +import { sortByDate, sortByTitle } from "../lib/sorters.js" +import { GET_ALL_ENTRIES, GET_ENTRY } from "../sql/entries.js" - getEntry = async () => { - return "API is working" - } +export class EntriesService { + database + + constructor(database) { + this.database = database + } + + getEntry = (title) => { + return this.database.prepare(GET_ENTRY).get(title) + } + + getAllEntries = (sort, limit) => { + const entries = this.database + .prepare(GET_ALL_ENTRIES) + .all() + .slice(0, Number(limit) || -1) + + if (sort === "date") { + return sortByDate(entries) + } + + return sortByTitle(entries) + } } diff --git a/src/sql/entries.js b/src/sql/entries.js new file mode 100644 index 0000000..a5f1709 --- /dev/null +++ b/src/sql/entries.js @@ -0,0 +1,5 @@ +const GET_ENTRY = `SELECT * FROM entries WHERE title = ?` + +const GET_ALL_ENTRIES = `SELECT title, last_modified FROM entries` + +export { GET_ENTRY, GET_ALL_ENTRIES }