Skip to content

Thomas API Express Database #129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 21 additions & 52 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 71 additions & 0 deletions src/routers/base.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const db = require('../../db')

class BaseController {
constructor(tableName) {
this.tableName = tableName
}

async getAll(req, res, next) {
try {
const response = await db.query(
`
SELECT * FROM ${this.tableName}
`
)
const items = response.rows

res.json({ [this.tableName]: items })
} catch (err) {
next(new Error(`Could not get ${this.tableName}: `, err))
}
}

async getById(req, res, next) {
const id = parseInt(req.params.id)

try {
const response = await db.query(
`
SELECT * FROM ${this.tableName} WHERE id = $1
`,
[id]
)
const [item] = response.rows

res.json({ [this.tableName.slice(0, -1)]: item })
} catch (err) {
next(
new Error(
`Could not get item from ${this.tableName} with id ${id}:`,
err
)
)
}
}

async delete(req, res, next) {
const id = parseInt(req.params.id)

try {
const response = await db.query(
`
DELETE FROM ${this.tableName} WHERE id = $1
returning *
`,
[id]
)
const [item] = response.rows

res.status(201).json({ [this.tableName.slice(0, -1)]: item })
} catch (err) {
next(
new Error(
`Could not delete ${this.tableName} with id ${id}:`,
err
)
)
}
}
}

module.exports = BaseController
50 changes: 50 additions & 0 deletions src/routers/books.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const db = require('../../db')
const BaseController = require('./base.controller')

class BooksController extends BaseController {
constructor() {
super('books')
}

async addBook(req, res, next) {
const { title, type, author, topic, publication_date, pages } = req.body

try {
const response = await db.query(
`
INSERT INTO books (title, type, author, topic, publication_date, pages)
VALUES ($1, $2, $3, $4, $5, $6)
returning *
`,
[title, type, author, topic, publication_date, pages]
)
const [book] = response.rows

res.status(201).json({ book: book })
} catch (err) {
next(new Error('Could not add book: ', err))
}
}

async updateBook(req, res, next) {
const { title, type, author, topic, publication_date, pages } = req.body

try {
const response = await db.query(
`
INSERT INTO books (title, type, author, topic, publication_date, pages)
VALUES ($1, $2, $3, $4, $5, $6)
returning *
`,
[title, type, author, topic, publication_date, pages]
)
const [book] = response.rows

res.status(201).json({ book: book })
} catch (err) {
next(new Error('Could not add book: ', err))
}
}
}

module.exports = BooksController
10 changes: 7 additions & 3 deletions src/routers/books.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
const express = require('express')
const router = express.Router()
const db = require("../../db");

router.get('/', async (req, res) => {
const BooksController = require('./books.controller')
const booksController = new BooksController()

})
router.post('/', booksController.addBook.bind(booksController))
router.get('/', booksController.getAll.bind(booksController))
router.get('/:id', booksController.getById.bind(booksController))
router.put('/:id', booksController.updateBook.bind(booksController))
router.delete('/:id', booksController.delete.bind(booksController))

module.exports = router
52 changes: 52 additions & 0 deletions src/routers/pets.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const db = require('../../db')
const BaseController = require('./base.controller')

class PetsController extends BaseController {
constructor() {
super('pets')
}

async addPet(req, res, next) {
const { name, age, type, breed, has_microchip } = req.body

try {
const response = await db.query(
`
INSERT INTO pets (name, age, type, breed, has_microchip)
VALUES ($1, $2, $3, $4, $5)
returning *
`,
[name, age, type, breed, has_microchip]
)
const [pet] = response.rows

res.status(201).json({ pet: pet })
} catch (err) {
next(new Error('Could not add pet: ', err))
}
}

async updatePet(req, res, next) {
const id = parseInt(req.params.id)
const { name, age, type, breed, has_microchip } = req.body

try {
const response = await db.query(
`
UPDATE pets
SET name = $1, age = $2, type = $3, breed = $4, has_microchip = $5
WHERE id = $6
returning *
`,
[name, age, type, breed, has_microchip, id]
)
const [pet] = response.rows

res.status(201).json({ pet })
} catch (err) {
next(new Error(`Could not update pet with id ${id}: `, err))
}
}
}

module.exports = PetsController
13 changes: 13 additions & 0 deletions src/routers/pets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const express = require('express')
const router = express.Router()

const PetsController = require('./pets.controller')
const petsController = new PetsController()

router.post('/', petsController.addPet.bind(petsController))
router.get('/', petsController.getAll.bind(petsController))
router.get('/:id', petsController.getById.bind(petsController))
router.put('/:id', petsController.updatePet.bind(petsController))
router.delete('/:id', petsController.delete.bind(petsController))

module.exports = router
16 changes: 9 additions & 7 deletions src/server.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
const express = require("express");
const morgan = require("morgan");
const cors = require("cors");
const express = require('express')
const morgan = require('morgan')
const cors = require('cors')

const app = express();
const app = express()

app.use(morgan("dev"));
app.use(cors());
app.use(express.json());
app.use(morgan('dev'))
app.use(cors())
app.use(express.json())

//TODO: Implement books and pets APIs using Express Modular Routers
const booksRouter = require('./routers/books.js')
const petsRouter = require('./routers/pets.js')

app.use('/books', booksRouter)
app.use('/pets', petsRouter)

module.exports = app
Loading
Loading