From 672987a2fa7a8092c90678474f73969b7736bf75 Mon Sep 17 00:00:00 2001 From: thomasabishop Date: Mon, 14 Jul 2025 17:41:55 +0100 Subject: [PATCH] feat: create Tag resource with first route --- src/controllers/EntriesController.js | 2 +- src/controllers/TagsController.js | 18 ++++++++++++++++++ src/index.js | 5 ++++- src/routes/entries.js | 4 ++-- src/routes/tags.js | 12 ++++++++++++ src/services/EntriesService.js | 2 +- src/services/TagsService.js | 18 ++++++++++++++++++ src/sql/tags.js | 3 +++ 8 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 src/controllers/TagsController.js create mode 100644 src/routes/tags.js create mode 100644 src/services/TagsService.js create mode 100644 src/sql/tags.js diff --git a/src/controllers/EntriesController.js b/src/controllers/EntriesController.js index e7c96a2..fecdd88 100644 --- a/src/controllers/EntriesController.js +++ b/src/controllers/EntriesController.js @@ -1,4 +1,4 @@ -export class EntriesController { +export default class EntriesController { entriesService constructor(entriesService) { diff --git a/src/controllers/TagsController.js b/src/controllers/TagsController.js new file mode 100644 index 0000000..127c74e --- /dev/null +++ b/src/controllers/TagsController.js @@ -0,0 +1,18 @@ +export default class TagsController { + tagsService + + constructor(tagsService) { + this.tagsService = tagsService + } + + getAllTags = (req, res) => { + const tags = this.tagsService.getAllTags() + if (!tags) { + return res.status(404).json({ + message: `Tags list could not be retrieved`, + }) + } + + return res.json(tags) + } +} diff --git a/src/index.js b/src/index.js index 28789c8..ccf59fa 100644 --- a/src/index.js +++ b/src/index.js @@ -1,12 +1,15 @@ import express from "express" import entries from "./routes/entries.js" +import tags from "./routes/tags.js" + const app = express() const port = process.env.PORT || 3000 app.use(express.json()) app.use("/entries", entries) +app.use("/tags", tags) 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/routes/entries.js b/src/routes/entries.js index a629150..b5652af 100644 --- a/src/routes/entries.js +++ b/src/routes/entries.js @@ -1,6 +1,6 @@ import express from "express" -import { EntriesService } from "../services/EntriesService.js" -import { EntriesController } from "../controllers/EntriesController.js" +import EntriesService from "../services/EntriesService.js" +import EntriesController from "../controllers/EntriesController.js" import database from "../db/connection.js" const router = express.Router() diff --git a/src/routes/tags.js b/src/routes/tags.js new file mode 100644 index 0000000..df77e3e --- /dev/null +++ b/src/routes/tags.js @@ -0,0 +1,12 @@ +import express from "express" +import TagsService from "../services/TagsService.js" +import TagsController from "../controllers/TagsController.js" +import database from "../db/connection.js" + +const router = express.Router() +const tagsService = new TagsService(database) +const tagsController = new TagsController(tagsService) + +router.get("/", tagsController.getAllTags) + +export default router diff --git a/src/services/EntriesService.js b/src/services/EntriesService.js index c569bd2..a4c8e50 100644 --- a/src/services/EntriesService.js +++ b/src/services/EntriesService.js @@ -1,7 +1,7 @@ import { sortByDate, sortByTitle } from "../lib/sorters.js" import { GET_ALL_ENTRIES, GET_ENTRY } from "../sql/entries.js" -export class EntriesService { +export default class EntriesService { database constructor(database) { diff --git a/src/services/TagsService.js b/src/services/TagsService.js new file mode 100644 index 0000000..6447bed --- /dev/null +++ b/src/services/TagsService.js @@ -0,0 +1,18 @@ +import { GET_ALL_TAGS } from "../sql/tags.js" + +export default class TagsService { + database + + constructor(database) { + this.database = database + } + + sortTags = (tags) => { + return tags.sort((a, b) => a.name.localeCompare(b.name)) + } + + getAllTags = () => { + const tags = this.sortTags(this.database.prepare(GET_ALL_TAGS).all()).flatMap((tag) => tag.name) + return { count: tags.length, tags: tags } + } +} diff --git a/src/sql/tags.js b/src/sql/tags.js new file mode 100644 index 0000000..a0f8f4c --- /dev/null +++ b/src/sql/tags.js @@ -0,0 +1,3 @@ +const GET_ALL_TAGS = `SELECT name FROM tags` + +export { GET_ALL_TAGS }