From 650c155fea3df9c2686bc8c48b1da846c1f5af5d Mon Sep 17 00:00:00 2001 From: Alistair Henshaw Date: Wed, 26 Jun 2024 14:10:36 +0100 Subject: [PATCH] core --- db/index.js | 32 +++++------ src/functions/logicBooks.js | 109 ++++++++++++++++++++++++++++++++++++ src/functions/logicPets.js | 101 +++++++++++++++++++++++++++++++++ src/routers/books.js | 39 ++++++++++++- src/routers/pets.js | 45 +++++++++++++++ src/server.js | 2 + 6 files changed, 308 insertions(+), 20 deletions(-) create mode 100644 src/functions/logicBooks.js create mode 100644 src/functions/logicPets.js create mode 100644 src/routers/pets.js diff --git a/db/index.js b/db/index.js index af723442..b33d8065 100644 --- a/db/index.js +++ b/db/index.js @@ -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; diff --git a/src/functions/logicBooks.js b/src/functions/logicBooks.js new file mode 100644 index 00000000..e3878863 --- /dev/null +++ b/src/functions/logicBooks.js @@ -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 +} \ No newline at end of file diff --git a/src/functions/logicPets.js b/src/functions/logicPets.js new file mode 100644 index 00000000..544ffe0e --- /dev/null +++ b/src/functions/logicPets.js @@ -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 +} \ No newline at end of file diff --git a/src/routers/books.js b/src/routers/books.js index 1551dd87..97fd6c0d 100644 --- a/src/routers/books.js +++ b/src/routers/books.js @@ -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 diff --git a/src/routers/pets.js b/src/routers/pets.js new file mode 100644 index 00000000..92ec6385 --- /dev/null +++ b/src/routers/pets.js @@ -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 \ No newline at end of file diff --git a/src/server.js b/src/server.js index dac55e5d..233ceb88 100644 --- a/src/server.js +++ b/src/server.js @@ -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