feat: setup DB connection and Entries resource
This commit is contained in:
parent
d36b3ef422
commit
858d20314f
7 changed files with 78 additions and 15 deletions
|
|
@ -5,7 +5,27 @@ export class EntriesController {
|
|||
this.entriesService = entriesService
|
||||
}
|
||||
|
||||
getEntry = async (req, res) => {
|
||||
const response = await this.entriesService.getEntry()
|
||||
getEntry = (req, res) => {
|
||||
const title = req.params.title
|
||||
const entry = this.entriesService.getEntry(title)
|
||||
if (!entry) {
|
||||
return res.status(404).json({
|
||||
message: `No entry found with title ${title}`,
|
||||
})
|
||||
}
|
||||
|
||||
return res.json(entry)
|
||||
}
|
||||
|
||||
getAllEntries = (req, res) => {
|
||||
const { sort, limit } = req.query
|
||||
const entries = this.entriesService.getAllEntries(sort, limit)
|
||||
if (!entries) {
|
||||
return res.status(404).json({
|
||||
message: `Entries list could not be retrieved`,
|
||||
})
|
||||
}
|
||||
|
||||
return res.json(entries)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
7
src/db/connection.js
Normal file
7
src/db/connection.js
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
import { DatabaseSync } from "node:sqlite"
|
||||
|
||||
const DATABASE_PATH = "/home/thomas/repos/eolas-api/data/eolas.db"
|
||||
|
||||
const database = new DatabaseSync(DATABASE_PATH)
|
||||
|
||||
export default database
|
||||
|
|
@ -1,7 +1,8 @@
|
|||
import express from "express"
|
||||
import entries from "./routes/entries"
|
||||
import entries from "./routes/entries.js"
|
||||
const app = express()
|
||||
const port = 3000
|
||||
|
||||
const port = process.env.PORT || 3000
|
||||
|
||||
app.use(express.json())
|
||||
app.use("/entries", entries)
|
||||
|
|
|
|||
9
src/lib/sorters.js
Normal file
9
src/lib/sorters.js
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
const sortByDate = (entries) => {
|
||||
return entries.sort((a, b) => new Date(b.last_modified) - new Date(a.last_modified))
|
||||
}
|
||||
|
||||
const sortByTitle = (entries) => {
|
||||
return entries.sort((a, b) => a.title.localeCompare(b.title))
|
||||
}
|
||||
|
||||
export { sortByDate, sortByTitle }
|
||||
|
|
@ -1,12 +1,13 @@
|
|||
import express from "express"
|
||||
import { EntriesService } from "../services/EntriesService"
|
||||
import { EntriesController } from "../controllers/EntriesController"
|
||||
import { EntriesService } from "../services/EntriesService"
|
||||
import { EntriesService } from "../services/EntriesService.js"
|
||||
import { EntriesController } from "../controllers/EntriesController.js"
|
||||
import database from "../db/connection.js"
|
||||
|
||||
const router = express.Router()
|
||||
const entriesService = new EntriesService()
|
||||
const entriesService = new EntriesService(database)
|
||||
const entriesController = new EntriesController(entriesService)
|
||||
|
||||
router.get("/:entry", entriesController.getEntry)
|
||||
router.get("/", entriesController.getAllEntries)
|
||||
router.get("/:title", entriesController.getEntry)
|
||||
|
||||
export default router
|
||||
|
|
|
|||
|
|
@ -1,7 +1,27 @@
|
|||
export class EntriesService {
|
||||
// TODO add DatabaseConnectorService
|
||||
import { sortByDate, sortByTitle } from "../lib/sorters.js"
|
||||
import { GET_ALL_ENTRIES, GET_ENTRY } from "../sql/entries.js"
|
||||
|
||||
getEntry = async () => {
|
||||
return "API is working"
|
||||
export class EntriesService {
|
||||
database
|
||||
|
||||
constructor(database) {
|
||||
this.database = database
|
||||
}
|
||||
|
||||
getEntry = (title) => {
|
||||
return this.database.prepare(GET_ENTRY).get(title)
|
||||
}
|
||||
|
||||
getAllEntries = (sort, limit) => {
|
||||
const entries = this.database
|
||||
.prepare(GET_ALL_ENTRIES)
|
||||
.all()
|
||||
.slice(0, Number(limit) || -1)
|
||||
|
||||
if (sort === "date") {
|
||||
return sortByDate(entries)
|
||||
}
|
||||
|
||||
return sortByTitle(entries)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
5
src/sql/entries.js
Normal file
5
src/sql/entries.js
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
const GET_ENTRY = `SELECT * FROM entries WHERE title = ?`
|
||||
|
||||
const GET_ALL_ENTRIES = `SELECT title, last_modified FROM entries`
|
||||
|
||||
export { GET_ENTRY, GET_ALL_ENTRIES }
|
||||
Loading…
Add table
Reference in a new issue