feat: create Tag resource with first route
This commit is contained in:
parent
858d20314f
commit
672987a2fa
8 changed files with 59 additions and 5 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
export class EntriesController {
|
export default class EntriesController {
|
||||||
entriesService
|
entriesService
|
||||||
|
|
||||||
constructor(entriesService) {
|
constructor(entriesService) {
|
||||||
|
|
|
||||||
18
src/controllers/TagsController.js
Normal file
18
src/controllers/TagsController.js
Normal file
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,12 +1,15 @@
|
||||||
import express from "express"
|
import express from "express"
|
||||||
import entries from "./routes/entries.js"
|
import entries from "./routes/entries.js"
|
||||||
|
import tags from "./routes/tags.js"
|
||||||
|
|
||||||
const app = express()
|
const app = express()
|
||||||
|
|
||||||
const port = process.env.PORT || 3000
|
const port = process.env.PORT || 3000
|
||||||
|
|
||||||
app.use(express.json())
|
app.use(express.json())
|
||||||
app.use("/entries", entries)
|
app.use("/entries", entries)
|
||||||
|
app.use("/tags", tags)
|
||||||
|
|
||||||
app.listen(port, () => {
|
app.listen(port, () => {
|
||||||
console.info(`INFO Server running at http://localhost:${port}`)
|
console.info(`INFO Server running at http://localhost:${port}`)
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import express from "express"
|
import express from "express"
|
||||||
import { EntriesService } from "../services/EntriesService.js"
|
import EntriesService from "../services/EntriesService.js"
|
||||||
import { EntriesController } from "../controllers/EntriesController.js"
|
import EntriesController from "../controllers/EntriesController.js"
|
||||||
import database from "../db/connection.js"
|
import database from "../db/connection.js"
|
||||||
|
|
||||||
const router = express.Router()
|
const router = express.Router()
|
||||||
|
|
|
||||||
12
src/routes/tags.js
Normal file
12
src/routes/tags.js
Normal file
|
|
@ -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
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
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 } from "../sql/entries.js"
|
||||||
|
|
||||||
export class EntriesService {
|
export default class EntriesService {
|
||||||
database
|
database
|
||||||
|
|
||||||
constructor(database) {
|
constructor(database) {
|
||||||
|
|
|
||||||
18
src/services/TagsService.js
Normal file
18
src/services/TagsService.js
Normal file
|
|
@ -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 }
|
||||||
|
}
|
||||||
|
}
|
||||||
3
src/sql/tags.js
Normal file
3
src/sql/tags.js
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
const GET_ALL_TAGS = `SELECT name FROM tags`
|
||||||
|
|
||||||
|
export { GET_ALL_TAGS }
|
||||||
Loading…
Add table
Reference in a new issue