feat: add get_all_entries_for_tag method
This commit is contained in:
parent
7e997bacaa
commit
254c5be8f5
4 changed files with 33 additions and 3 deletions
|
|
@ -28,4 +28,16 @@ export default class EntriesController {
|
||||||
|
|
||||||
return res.json(entries)
|
return res.json(entries)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getEntriesForTag = (req, res) => {
|
||||||
|
const tag = req.params.tag
|
||||||
|
const sort = req.query.sort
|
||||||
|
const entries = this.entriesService.getEntriesForTag(tag, sort)
|
||||||
|
if (!entries) {
|
||||||
|
return res.status(404).json({
|
||||||
|
message: `Could not retrieve entries for tag ${tag}`,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return res.json(entries)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,5 +9,5 @@ const entriesController = new EntriesController(entriesService)
|
||||||
|
|
||||||
router.get("/", entriesController.getAllEntries)
|
router.get("/", entriesController.getAllEntries)
|
||||||
router.get("/:title", entriesController.getEntry)
|
router.get("/:title", entriesController.getEntry)
|
||||||
|
router.get("/tag/:tag", entriesController.getEntriesForTag)
|
||||||
export default router
|
export default router
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { sortByDate, sortByTitle } from "../lib/sorters.js"
|
import { sortByDate, sortByTitle } from "../lib/sorters.js"
|
||||||
import { GET_ALL_ENTRIES, GET_ENTRY } from "../sql/entries.js"
|
import { GET_ALL_ENTRIES, GET_ENTRY, GET_ENTRIES_FOR_TAG } from "../sql/entries.js"
|
||||||
|
|
||||||
export default class EntriesService {
|
export default class EntriesService {
|
||||||
database
|
database
|
||||||
|
|
@ -23,4 +23,20 @@ export default class EntriesService {
|
||||||
entries: sort === "date" ? sortByDate(entries) : sortByTitle(entries),
|
entries: sort === "date" ? sortByDate(entries) : sortByTitle(entries),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getEntriesForTag = (tag, sort) => {
|
||||||
|
const entries = this.database.prepare(GET_ENTRIES_FOR_TAG).all(tag)
|
||||||
|
return {
|
||||||
|
count: entries.length,
|
||||||
|
entries: sort === "date" ? this._sortByDate(entries) : this._sortByTitle(entries, "entry_title"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_sortByTitle = (entries, fieldName) => {
|
||||||
|
return entries.sort((a, b) => a[fieldName].localeCompare(b[fieldName]))
|
||||||
|
}
|
||||||
|
|
||||||
|
_sortByDate = (entries, fieldName = last_modified) => {
|
||||||
|
return entries.sort((a, b) => new Date(b[fieldName]) - new Date(a[fieldName]))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,4 +2,6 @@ const GET_ENTRY = `SELECT * FROM entries WHERE title = ?`
|
||||||
|
|
||||||
const GET_ALL_ENTRIES = `SELECT title, last_modified FROM entries`
|
const GET_ALL_ENTRIES = `SELECT title, last_modified FROM entries`
|
||||||
|
|
||||||
export { GET_ENTRY, GET_ALL_ENTRIES }
|
const GET_ENTRIES_FOR_TAG = `SELECT entry_title from entries_tags WHERE tag_name = ?`
|
||||||
|
|
||||||
|
export { GET_ENTRY, GET_ALL_ENTRIES, GET_ENTRIES_FOR_TAG }
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue