diff --git a/src/controllers/TagsController.js b/src/controllers/TagsController.js index 127c74e..45c4b02 100644 --- a/src/controllers/TagsController.js +++ b/src/controllers/TagsController.js @@ -1,18 +1,30 @@ export default class TagsController { - tagsService + tagsService - constructor(tagsService) { - this.tagsService = 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`, - }) - } + 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) - } + return res.json(tags) + } + + getTagsForEntry = (req, res) => { + const entryTitle = req.params.entryTitle + const entries = this.tagsService.getTagsForEntry(entryTitle) + if (!entries) { + return res.status(404).json({ + message: `Tag list could not be retrieved for entry ${entryTitle}`, + }) + } + + return res.json(entries) + } } diff --git a/src/routes/tags.js b/src/routes/tags.js index df77e3e..81619df 100644 --- a/src/routes/tags.js +++ b/src/routes/tags.js @@ -8,5 +8,6 @@ const tagsService = new TagsService(database) const tagsController = new TagsController(tagsService) router.get("/", tagsController.getAllTags) +router.get("/:entryTitle", tagsController.getTagsForEntry) export default router diff --git a/src/services/TagsService.js b/src/services/TagsService.js index b2d815f..d117a8f 100644 --- a/src/services/TagsService.js +++ b/src/services/TagsService.js @@ -1,4 +1,4 @@ -import { GET_ALL_TAGS } from "../sql/tags.js" +import { GET_ALL_TAGS, GET_TAGS_FOR_ENTRY } from "../sql/tags.js" export default class TagsService { database @@ -8,11 +8,20 @@ export default class TagsService { } getAllTags = () => { - const tags = this._sortTags(this.database.prepare(GET_ALL_TAGS).all()).flatMap((tag) => tag.name) - return { count: tags.length, tags: tags } + const tags = this.database.prepare(GET_ALL_TAGS).all() + const sorted = this._sortTags(tags, "name") + const list = sorted.flatMap((tag) => tag.name) + return { count: tags.length, tags: list } } - _sortTags = (tags) => { - return tags.sort((a, b) => a.name.localeCompare(b.name)) + getTagsForEntry = (entryTitle) => { + const tags = this.database.prepare(GET_TAGS_FOR_ENTRY).all(entryTitle) + const sorted = this._sortTags(tags, "tag_name") + const list = sorted.flatMap((tag) => tag.tag_name) + return { count: tags.length, tags: list } + } + + _sortTags = (tags, fieldName) => { + return tags.sort((a, b) => a[fieldName].localeCompare(b[fieldName])) } } diff --git a/src/sql/tags.js b/src/sql/tags.js index a0f8f4c..5ad0785 100644 --- a/src/sql/tags.js +++ b/src/sql/tags.js @@ -1,3 +1,5 @@ const GET_ALL_TAGS = `SELECT name FROM tags` -export { GET_ALL_TAGS } +const GET_TAGS_FOR_ENTRY = `SELECT tag_name FROM entries_tags WHERE entry_title = ?` + +export { GET_ALL_TAGS, GET_TAGS_FOR_ENTRY }