feat: add get_all_tags_for_entry method
This commit is contained in:
parent
13f6bbc748
commit
7e997bacaa
4 changed files with 43 additions and 19 deletions
|
|
@ -1,18 +1,30 @@
|
||||||
export default class TagsController {
|
export default class TagsController {
|
||||||
tagsService
|
tagsService
|
||||||
|
|
||||||
constructor(tagsService) {
|
constructor(tagsService) {
|
||||||
this.tagsService = tagsService
|
this.tagsService = tagsService
|
||||||
}
|
}
|
||||||
|
|
||||||
getAllTags = (req, res) => {
|
getAllTags = (req, res) => {
|
||||||
const tags = this.tagsService.getAllTags()
|
const tags = this.tagsService.getAllTags()
|
||||||
if (!tags) {
|
if (!tags) {
|
||||||
return res.status(404).json({
|
return res.status(404).json({
|
||||||
message: `Tags list could not be retrieved`,
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,5 +8,6 @@ const tagsService = new TagsService(database)
|
||||||
const tagsController = new TagsController(tagsService)
|
const tagsController = new TagsController(tagsService)
|
||||||
|
|
||||||
router.get("/", tagsController.getAllTags)
|
router.get("/", tagsController.getAllTags)
|
||||||
|
router.get("/:entryTitle", tagsController.getTagsForEntry)
|
||||||
|
|
||||||
export default router
|
export default router
|
||||||
|
|
|
||||||
|
|
@ -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 {
|
export default class TagsService {
|
||||||
database
|
database
|
||||||
|
|
@ -8,11 +8,20 @@ export default class TagsService {
|
||||||
}
|
}
|
||||||
|
|
||||||
getAllTags = () => {
|
getAllTags = () => {
|
||||||
const tags = this._sortTags(this.database.prepare(GET_ALL_TAGS).all()).flatMap((tag) => tag.name)
|
const tags = this.database.prepare(GET_ALL_TAGS).all()
|
||||||
return { count: tags.length, tags: tags }
|
const sorted = this._sortTags(tags, "name")
|
||||||
|
const list = sorted.flatMap((tag) => tag.name)
|
||||||
|
return { count: tags.length, tags: list }
|
||||||
}
|
}
|
||||||
|
|
||||||
_sortTags = (tags) => {
|
getTagsForEntry = (entryTitle) => {
|
||||||
return tags.sort((a, b) => a.name.localeCompare(b.name))
|
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]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
const GET_ALL_TAGS = `SELECT name FROM tags`
|
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 }
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue