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
32 changes: 13 additions & 19 deletions db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,18 @@
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;
109 changes: 109 additions & 0 deletions src/functions/logicBooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
const dbClient = require("../../db/index.js");

const getAllBooks = async () => {
const db = await dbClient.connect()

try {
const sqlQuery = 'SELECT * FROM books'
const result = await db.query(sqlQuery)

return result.rows
}
catch (error) {
console.log(error)
}
finally {
db.release()
}
}

const createBook = async (info) => {
const db = await dbClient.connect()
const { title, type, author, topic, publication_date, pages } = info

// console.log(info)
try {
const sqlQuery = `INSERT INTO books (title, type, author, topic, publication_date, pages)
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING *`
const postedData = await db.query(sqlQuery, [ title, type, author, topic, publication_date, pages ])

// console.log(postedData)
return postedData.rows[0]
}
catch (error) {
console.log(error)
}
finally {
db.release()
}
}

const getBookById = async (id) => {
const db = await dbClient.connect()
// console.log(id)

try {
const sqlQuery = `SELECT * FROM books WHERE id = $1`
const result = await db.query(sqlQuery, [Number(id)])

return result.rows[0]
}
catch (error) {
console.log(error)
}
finally {
db.release()
}
}

const updateBook = async (id, info) => {
const db = await dbClient.connect()
const { title, type, author, topic, publication_date, pages } = info
// console.log(id)

try {
const sqlQuery = `UPDATE books
Set title = $2, type = $3, author = $4, topic = $5, publication_date = $6, pages = $7
WHERE id = $1
RETURNING *`
// console.log(sqlQuery)
const result = await db.query(sqlQuery, [ Number(id), title, type, author, topic, publication_date, pages ])
// console.log(result)
return result.rows[0]
}
catch (error) {
console.log(error)
}
finally {
db.release()
}
}

const deleteBook = async (id) => {
const db = await dbClient.connect()
// console.log(id)

try {
const sqlQuery = `DELETE FROM books
WHERE id = $1
RETURNING *`
const result = await db.query(sqlQuery, [Number(id)])

return result.rows[0]
}
catch (error) {
console.log(error)
}
finally {
db.release()
}
}

module.exports = {
getAllBooks,
createBook,
getBookById,
updateBook,
deleteBook
}
101 changes: 101 additions & 0 deletions src/functions/logicPets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
const dbClient = require("../../db/index.js");

const getAllPets = async () => {
const db = await dbClient.connect()

try {
const sqlQuery = 'SELECT * FROM pets'
const result = await db.query(sqlQuery)

return result.rows
}
catch (error) {
console.log(error)
}
finally {
db.release()
}
}

const addPet = async (info) => {
const db = await dbClient.connect()
const { name, age, type, breed, has_microchip } = info

try {
const sqlQuery = `INSERT INTO pets (name, age, type, breed, has_microchip)
VALUES ($1, $2, $3, $4, $5)
RETURNING *`
const postedData = await db.query(sqlQuery, [ name, age, type, breed, has_microchip ])
return postedData.rows[0]
}
catch (error) {
console.log(error)
}
finally {
db.release()
}
}

const getPet = async (id) => {
const db = await dbClient.connect()

try {
const sqlQuery = `SELECT * FROM pets
WHERE id = $1`
const result = await db.query(sqlQuery, [Number(id)])

return result.rows[0]
}
catch (error) {
console.log(error)
}
finally {
db.release()
}
}

const updatePet = async (id, info) => {
const db = await dbClient.connect()
const { name, age, type, breed, has_microchip } = info
try {
const sqlQuery = `UPDATE pets
SET name = $2, age = $3, type = $4, breed = $5, has_microchip = $6
WHERE id = $1
RETURNING *`
const result = await db.query(sqlQuery, [ Number(id), name, age, type, breed, has_microchip ])

return result.rows[0]
}
catch (error) {
console.log(error)
}
finally {
db.release()
}
}

const deletePet = async (id) => {
const db = await dbClient.connect()

try {
const sqlQuery = 'DELETE FROM pets WHERE id = $1 RETURNING *'
const result = await db.query(sqlQuery, [Number(id)])

return result.rows[0]
}
catch (error) {
console.log(error)
}
finally {
db.release()
}

}

module.exports = {
getAllPets,
addPet,
getPet,
updatePet,
deletePet
}
39 changes: 38 additions & 1 deletion src/routers/books.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,46 @@
const express = require('express')
const router = express.Router()
const db = require("../../db");
const { getAllBooks, createBook, getBookById, updateBook, deleteBook } = require('../functions/logicBooks.js')

router.get('/', async (req, res) => {
const books = await getAllBooks()
// console.log(books)
res.status(200).json({
books
})
})

router.post('/', async (req, res) => {
// console.log(req.body)
const book = await createBook(req.body)

res.status(201).json({
book
})
})

router.get('/:id', async (req, res) => {
const book = await getBookById(req.params.id)

res.json({
book
})
})

router.put('/:id', async (req, res) => {
const book = await updateBook(req.params.id, req.body)

res.status(201).json({
book
})
})

router.delete('/:id', async (req, res) => {
const book = await deleteBook(req.params.id)

res.status(201).json({
book
})
})

module.exports = router
45 changes: 45 additions & 0 deletions src/routers/pets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const express = require('express')
const router = express.Router()
const { getAllPets, addPet, getPet, updatePet, deletePet } = require('../functions/logicPets.js')

router.get('/', async (req, res) => {
const pets = await getAllPets()

res.json({
pets
})
})

router.post('/', async (req, res) => {
const pet = await addPet(req.body)

res.status(201).json({
pet
})
})

router.get('/:id', async (req, res) => {
const pet = await getPet(req.params.id)

res.json({
pet
})
})

router.put('/:id', async (req, res) => {
const pet = await updatePet(req.params.id, req.body)

res.status(201).json({
pet
})
})

router.delete('/:id', async (req, res) => {
const pet = await deletePet(req.params.id)

res.status(201).json({
pet
})
})

module.exports = router
2 changes: 2 additions & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ 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