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
2 changes: 1 addition & 1 deletion db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ const client = {
}
}

module.exports = client;
module.exports = client;
84 changes: 53 additions & 31 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"dependencies": {
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"date-fns": "^3.6.0",
"date-fns-tz": "^3.1.3",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"faker": "^5.5.3",
Expand Down
2 changes: 1 addition & 1 deletion sql/create-books.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ CREATE TABLE IF NOT EXISTS books (
topic VARCHAR(255) NOT NULL,
publication_date DATE NOT NULL,
pages INTEGER NOT NULL
);
);
2 changes: 1 addition & 1 deletion sql/create-pets.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ CREATE TABLE IF NOT EXISTS pets (
type VARCHAR(255) NOT NULL,
breed VARCHAR(255) NOT NULL,
has_microchip BOOLEAN NOT NULL
);
);
2 changes: 1 addition & 1 deletion sql/insert-books.sql
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,4 @@ INSERT INTO books (title, type, author, topic, publication_date, pages) VALUES (
INSERT INTO books (title, type, author, topic, publication_date, pages) VALUES ('placeat eius consectetur architecto', 'Non-Fiction', 'Nick Labadie', 'philosophy', '2018-02-20T07:56:03.841+00:00', 400);
INSERT INTO books (title, type, author, topic, publication_date, pages) VALUES ('quia cumque maiores illum', 'Non-Fiction', 'Nick Labadie', 'philosophy', '2021-04-24T05:10:20.766+01:00', 400);
INSERT INTO books (title, type, author, topic, publication_date, pages) VALUES ('ratione laudantium voluptas nihil voluptatem', 'Non-Fiction', 'Nick Labadie', 'philosophy', '2014-02-26T03:05:12.242+00:00', 400);
INSERT INTO books (title, type, author, topic, publication_date, pages) VALUES ('dolore qui accusamus sed', 'Non-Fiction', 'Arianna Dietrich', 'biography', '2004-06-23T09:29:19.212+01:00', 400);
INSERT INTO books (title, type, author, topic, publication_date, pages) VALUES ('dolore qui accusamus sed', 'Non-Fiction', 'Arianna Dietrich', 'biography', '2004-06-23T09:29:19.212+01:00', 400);
2 changes: 1 addition & 1 deletion sql/insert-pets.sql
Original file line number Diff line number Diff line change
Expand Up @@ -337,4 +337,4 @@ INSERT INTO pets (name, age, type, breed, has_microchip) VALUES('Corbin', 19, 'r
INSERT INTO pets (name, age, type, breed, has_microchip) VALUES('Jazmyne', 8, 'rabbit', 'Florida White', true);
INSERT INTO pets (name, age, type, breed, has_microchip) VALUES('Ralph', 15, 'rabbit', 'Florida White', false);
INSERT INTO pets (name, age, type, breed, has_microchip) VALUES('Yasmin', 16, 'rabbit', 'Florida White', false);
INSERT INTO pets (name, age, type, breed, has_microchip) VALUES('Joan', 4, 'rabbit', 'Florida White', true);
INSERT INTO pets (name, age, type, breed, has_microchip) VALUES('Joan', 4, 'rabbit', 'Florida White', true);
65 changes: 64 additions & 1 deletion src/routers/books.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,72 @@
const express = require('express')
const router = express.Router()
const db = require("../../db");
const { parseISO, format } = require('date-fns');
const { utcToZonedTime } = require('date-fns-tz');

router.get('/', async (req, res) => {

const queryOptions = []
const queryParams = []
const type = req.query.type
const topic = req.query.topic
if(type !== undefined){
queryOptions.push(`LOWER(type) = $1`)
queryParams.push(type)
}
if(topic !== undefined){
queryOptions.push(`LOWER(topic) = $${queryOptions.length + 1}`)
queryParams.push(topic)
}
console.log("query:", `select * FROM books ${queryOptions.length > 0 ? "WHERE " : ""}${queryOptions.join(" AND ")}`, queryParams)
const response = await db.query(`select * FROM books ${queryOptions.length > 0 ? "WHERE " : ""}${queryOptions.join(" AND ")}`, queryParams)
res.json({books: response.rows.map(parseBook)})
})
router.get('/:id', async (req, res) => {
const response = await db.query("select * FROM books WHERE id = $1 ", [req.params.id])
res.json({book: parseBook(response.rows[0])})
})
router.put('/:id', async (req, res) => {
console.log("BumbaClap", req.body)
const update = await db.query(
`UPDATE books
SET title = $1,
type = $2,
author = $3,
topic = $4,
publication_date = $5,
pages = $6
WHERE id = $7;
`, [req.body.title, req.body.type, req.body.author, req.body.topic, req.body.publication_date, req.body.pages, req.params.id ])
const response = await db.query("select * FROM books WHERE id = $1 ", [req.params.id])
res.status(201).json({book: parseBook(response.rows[0])})
})
router.delete('/:id', async (req, res) => {
const response = await db.query("select * FROM books WHERE id = $1 ", [req.params.id])
await db.query("DELETE FROM books WHERE id = $1", [req.params.id])
res.status(201).json({book: parseBook(response.rows[0])})
})
router.post('/', async (req, res) => {
console.log("BumbaClap", req.body)
const update = await db.query(
`INSERT INTO books (title, type, author, topic, publication_date, pages)
VALUES ($1, $2, $3, $4, $5, $6) RETURNING *;
`, [req.body.title, req.body.type, req.body.author, req.body.topic, req.body.publication_date, req.body.pages])
console.log("test", update.rows[0])
const response = await db.query("select * FROM books WHERE id = $1 ", [update.rows[0].id])
res.status(201).json({book: parseBook(response.rows[0])})
})

function parseBook(bookData) {
// // Parse the input date string to a Date object
// const date = parseISO(bookData.publication_date);

// // Convert the date to the local time zone
// const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
// const zonedDate = utcToZonedTime(date, timeZone);

// Format the date to 'yyyy-MM-dd HH:mm:ss'
const outputDateStr = format(new Date(bookData.publication_date), 'yyyy-MM-dd HH:mm:ss');
return {...bookData, publication_date:outputDateStr}
}

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

router.get('/', async (req, res) => {
const type = req.query.type
const response = await db.query(`select * FROM pets ${type !==undefined ? "WHERE type = $1" : ""}`, type !== undefined ? [type] : [])
res.json({pets: response.rows.map(parsePet)})
})
router.get('/:id', async (req, res) => {
const response = await db.query("select * FROM pets WHERE id = $1 ", [req.params.id])
res.json({pet: parsePet(response.rows[0])})
})
router.put('/:id', async (req, res) => {
// console.log("BumbaClap", req.body)
const update = await db.query(
`UPDATE pets
SET name = $1,
age = $2,
type = $3,
breed = $4,
has_microchip = $5
WHERE id = $6;
`, [req.body.name, req.body.age, req.body.type, req.body.breed, req.body.has_microchip, req.params.id ])
const response = await db.query("select * FROM pets WHERE id = $1 ", [req.params.id])
res.status(201).json({pet: parsePet(response.rows[0])})
})
router.delete('/:id', async (req, res) => {

const deleteResponse = await db.query("DELETE FROM pets WHERE id = $1 RETURNING *;", [req.params.id])
// console.log("data", deleteResponse)
res.status(201).json({pet: parsePet(deleteResponse.rows[0])})
})
router.post('/', async (req, res) => {
// console.log("BumbaClap", req.body)
const update = await db.query(
`INSERT INTO pets (name, age, type, breed, has_microchip)
VALUES ($1, $2, $3, $4, $5) RETURNING *;
`, [req.body.name, req.body.age, req.body.type, req.body.breed, req.body.has_microchip])
// console.log("test", update.rows[0])
const response = await db.query("select * FROM pets WHERE id = $1 ", [update.rows[0].id])
res.status(201).json({pet: parsePet(response.rows[0])})
})

function parsePet(data) {
return {...data, has_microchip: !!data.has_microchip}
}

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
3 changes: 2 additions & 1 deletion test/api/routes/pets.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe("Pets Endpoint", () => {

expect(response.status).toEqual(200)
expect(response.body.pets).not.toEqual(undefined)
console.log("response", response.body)
expect(response.body.pets.length).toEqual(2)
})

Expand Down Expand Up @@ -82,4 +83,4 @@ describe("Pets Endpoint", () => {
}
})
})
})
})
6 changes: 3 additions & 3 deletions test/fixtures/bookData.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ module.exports = {
type: "test1",
author: "test1",
topic: "test1",
publication_date: "2020-11-16T00:00:00.000Z",
publication_date: "2020-11-16 00:00:00",
pages: 1
},
book2: {
title: "test2",
type: "test2",
author: "test2",
topic: "test2",
publication_date: "2020-11-17T00:00:00.000Z",
publication_date: "2020-11-17 00:00:00",
pages: 2
},
book3: {
title: "test3",
type: "test3",
author: "test3",
topic: "test3",
publication_date: "2020-11-18T00:00:00.000Z",
publication_date: "2020-11-18 00:00:00",
pages: 3
}
}
2 changes: 1 addition & 1 deletion test/helpers/createBook.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ const createBook = async (values) => {
return result.rows[0]
}

module.exports = createBook
module.exports = createBook
4 changes: 2 additions & 2 deletions test/helpers/createPet.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ const client = require("../../db");

const createPet = async (values) => {
const sqlString = `INSERT INTO "pets" (name, age, type, breed, has_microchip) VALUES ($1, $2, $3, $4, $5) RETURNING *;`

console.log("insert pets", values)
const result = await client.query(sqlString, values)

return result.rows[0]
}

module.exports = createPet
module.exports = createPet
Loading