Skip to content

Oluwagbemi Abiodun #124

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 6 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
63 changes: 13 additions & 50 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"express": "^4.19.2",
"faker": "^5.5.3",
"morgan": "1.10.0",
"pg": "8.6.0",
"pg-promise": "^11.5.4"
},
"devDependencies": {
"jest": "^28.1.3",
"nodemon": "^3.0.2",
"nodemon": "^3.1.4",
"supertest": "^6.3.3"
},
"keywords": []
Expand Down
91 changes: 87 additions & 4 deletions src/routers/books.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,92 @@
const express = require('express')
const router = express.Router()
const db = require("../../db");
require('dotenv').config();
const express = require('express');
const { Pool } = require('pg');

const { PGHOST, PGDATABASE, PGUSER, PGPASSWORD } = process.env;

const pool = new Pool({
host: PGHOST,
database: PGDATABASE,
user: PGUSER,
password: PGPASSWORD,
port: 5432,
ssl: {
require: true,
},
});

const router = express.Router();

router.get('/', async (req, res) => {
const db = await pool.connect();
const sqlQuery = 'SELECT * FROM books';
const result = await db.query(sqlQuery);

res.json({
books: result.rows
});
db.release();
});

router.post('/', async (req, res) => {
const { title, type, author, topic, publication_date, pages } = req.body;
const db = await pool.connect();
const sqlQuery = 'INSERT INTO books (title, type, author, topic, publication_date, pages) VALUES ($1, $2, $3, $4, $5, $6) RETURNING *';
const result = await db.query(sqlQuery, [title, type, author, topic, publication_date, pages]);
res.status(201).json({ book: result.rows[0] });
db.release();
});

router.get('/:id', async (req, res) => {
const { id } = req.params;
const db = await pool.connect();
const sqlQuery = 'SELECT * FROM books WHERE id = $1';
const result = await db.query(sqlQuery, [id]);

if (result.rows.length > 0) {
res.json({ book: result.rows[0] });
} else {
res.status(404).json({ error: 'Book not found' });
}

db.release();
})

module.exports = router
router.put('/:id', async (req, res) => {
const { id } = req.params;
const { title, type, author, topic, publication_date, pages } = req.body;
const db = await pool.connect();
const sqlQuery = `
UPDATE books
SET title = $1, type = $2, author = $3, topic = $4, publication_date = $5, pages = $6
WHERE id = $7
RETURNING *
`;
const result = await db.query(sqlQuery, [title, type, author, topic, publication_date, pages, id]);

if (result.rows.length > 0) {
res.json({ book: result.rows[0] });
} else {
res.status(404).json({ error: 'Book not found' });
}

db.release();
});

router.delete('/:id', async (req, res) => {
const { id } = req.params;
const db = await pool.connect();
const sqlQuery = 'DELETE FROM books WHERE id = $1 RETURNING *';
const result = await db.query(sqlQuery, [id]);

if (result.rows.length > 0) {
res.json({ message: 'Book deleted successfully' });
} else {
res.status(404).json({ error: 'Book not found' });
}

db.release();
});


module.exports = router;
100 changes: 100 additions & 0 deletions src/routers/pets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
require('dotenv').config();
const express = require('express');
const { Pool } = require('pg');

const { PGHOST, PGDATABASE, PGUSER, PGPASSWORD } = process.env;

const pool = new Pool({
host: PGHOST,
database: PGDATABASE,
user: PGUSER,
password: PGPASSWORD,
port: 5432,
ssl: {
require: true,
},
});

const router = express.Router();

//Get all pets
router.get('/', async (req, res) => {
const db = await pool.connect();
const sqlQuery = 'SELECT * FROM pets';
const result = await db.query(sqlQuery);

res.json({
pets: result.rows
});
db.release();
});

//gets pet by ID
router.get('/:id', async (req, res) => {
const { id } = req.params
const db = await pool.connect()
const sqlQuery = 'SELECT * FROM pets WHERE id = $1'
const result = await db.query(sqlQuery, [id])

if (result.rows.length > 0) {
res.json({pet: result.rows[0] })
} else {
res.status(404).json({ error: 'pet not found'})
}
db.release()
})

//Add to pet
router.post('/', async (req, res) => {
const {name, age, type, breed, has_microchip} = req.body
const db = await pool.connect()
const sqlQuery = 'INSERT INTO pets (name, age, type, breed, has_microchip) VALUES ($1, $2, $3, $4, $5) RETURNING *'
const result = await db.query(sqlQuery, [name, age, type, breed, has_microchip])
res.status(201).json({pet: result.rows[0]})

db.release()
})

// Wdit pets
router.put('/:id', async (req, res) => {
const { id } = req.params
const {name, age, type, breed, has_microchip} = req.body
const db = await pool.connect()
const sqlQuery = `
UPDATE pets
SET name = $1,
age = $2,
type = $3,
breed = $4,
has_microchip = $5
WHERE id = $6
RETURNING *
`
const result = await db.query(sqlQuery, [name, age, type, breed, has_microchip, id])

if (result.rows.length > 0) {
res.json({pet: result.rows[0]})
} else {
res.status(400).json({ error: 'Pet not found' })
}
})


//Deletes pet

router.delete('/:id', async (req, res) => {
const { id } = req.params;
const db = await pool.connect();
const sqlQuery = 'DELETE FROM pets WHERE id = $1 RETURNING *';
const result = await db.query(sqlQuery, [id]);

if (result.rows.length > 0) {
res.json({ message: 'Pet deleted', pet: result.rows[0] });
} else {
res.status(404).json({ error: 'Pet not found' });
}
db.release();
});


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