Skip to content
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
33 changes: 14 additions & 19 deletions db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,19 @@
require('dotenv').config()

// Require Client obj from the postgres node module
const { Client } = require("pg");
const { Pool } = require("pg")
const { PGHOST, PGDATABASE, PGUSER, PGPASSWORD } = process.env

const client = {
query: async (str, values) => {
// Get the connection string from process.env -
// the dotenv library sets this variable based
// on the contents of our env file
// Create a new connection to the database using the Client
// object provided by the postgres node module
const dbClient = new Client(process.env.PGURL)
// connect a connection
await dbClient.connect()
// execute the query
const result = await dbClient.query(str, values)
// close the connection
await dbClient.end()
return result
}
}
const dbClient = new Pool({
host: PGHOST,
database: PGDATABASE,
username: PGUSER,
password: PGPASSWORD,
port: 5432,
ssl: {
require: true,
},
})

module.exports = client;

module.exports = dbClient;
67 changes: 15 additions & 52 deletions package-lock.json

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

88 changes: 87 additions & 1 deletion src/routers/books.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,95 @@
const express = require('express')
const router = express.Router()
const db = require("../../db");
const db = require("../../db")
const dbClient = require('../../db')
const { book1, book2 } = require("../../test/fixtures/bookData.js")


router.post('/', async (req, res) => {
const sqlQuery = `
INSERT INTO books (title, type, author, topic, publication_date, pages)
VALUES ($1, $2, $3, $4, $5, $6) RETURNING *;
`

const values = [
book1.title,
book1.type,
book1.author,
book1.topic,
book1.publication_date,
book1.pages
]

const result = await dbClient.query(sqlQuery, values)

res.status(201).json({
book: result.rows[0]
})
})

router.get('/', async (req, res) => {
const sqlQuery = 'select * from books'

const result = await dbClient.query(sqlQuery)

res.status(200).json({
books: result.rows
})
})

module.exports = router

router.get('/:id', async (req, res) => {
const { id } = req.params
const sqlQuery = `select * from books where id = $1`

const result = await dbClient.query(sqlQuery, [id])

res.status(200).json({
book: result.rows[0]
})
})

router.put('/:id', async (req, res) => {
const { id } = req.params
const { title, type, author, topic, publication_date, pages } = req.body
const sqlQuery = `
update books
set title = $1, type = $2, author = $3, topic = $4, publication_date = $5, pages = $6
where id = $7
RETURNING *
`

const values = [
book2.title,
book2.type,
book2.author,
book2.topic,
book2.publication_date,
book2.pages,
id
]

const result = await dbClient.query(sqlQuery, values)

res.status(201).json({
book: result.rows[0]
})
})

router.delete('/:id', async (req, res) => {
const { id } = req.params
const sqlQuery = `
delete from books
where id = $1
RETURNING *
`

const result = await dbClient.query(sqlQuery, [id])

res.status(201).json({
book: result.rows[0]
})
})

module.exports = router
Loading