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
25 changes: 0 additions & 25 deletions db/index.js

This file was deleted.

9 changes: 9 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"express-async-errors": "^3.1.1",
"faker": "^5.5.3",
"morgan": "1.10.0",
"pg": "8.6.0",
Expand Down
59 changes: 59 additions & 0 deletions src/controllers/booksControllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const {
getAllBooks,
postNewBook,
getBookById,
updateBookById,
deleteBookById,
} = require("../dal/booksRepo.js")
const getPaginationParams = require("../utils/pagination.js")

const getBooks = async (req, res) => {
const { page, per_page } = getPaginationParams(req)
const books = await getAllBooks(req)

res.json({
books,
per_page,
page
})
}

const postBook = async (req, res) => {
const book = await postNewBook(req)

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

const getBook = async (req, res) => {
const book = await getBookById(req)

res.json({
book,
})
}

const updateBook = async (req, res) => {
const book = await updateBookById(req)

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

const deleteBook = async (req, res) => {
const book = await deleteBookById(req)

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

module.exports = {
getBooks,
postBook,
getBook,
updateBook,
deleteBook,
}
13 changes: 13 additions & 0 deletions src/controllers/breedsControllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const dbConnection = require("../utils/dbConnection.js")

const getAllBreeds = async (req, res) => {
const type = req.query.type
const sqlQuery = "select distinct breed from pets where type = $1"
const result = await dbConnection.query(sqlQuery, [type])

res.json({
breeds: result.rows
})
}

module.exports = getAllBreeds
59 changes: 59 additions & 0 deletions src/controllers/petsControllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const {
getAllPets,
postNewPet,
getPetById,
updatePetById,
deletePetById,
} = require("../dal/petsRepo.js")
const getPaginationParams = require("../utils/pagination.js")

const getPets = async (req, res) => {
const { page, per_page } = getPaginationParams(req)
const pets = await getAllPets(req)

res.json({
pets,
per_page,
page
})
}

const postPet = async (req, res) => {
const pet = await postNewPet(req)

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

const getPet = async (req, res) => {
const pet = await getPetById(req)

res.json({
pet,
})
}

const updatePet = async (req, res) => {
const pet = await updatePetById(req)

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

const deletePet = async (req, res) => {
const pet = await deletePetById(req)

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

module.exports = {
getPets,
postPet,
getPet,
updatePet,
deletePet,
}
115 changes: 115 additions & 0 deletions src/dal/booksRepo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
const ConflictError = require("../errors/ConflictError.js")
const MissingFieldError = require("../errors/MissingFieldError.js")
const NotFoundError = require("../errors/NotFoundError.js")
const dbConnection = require("../utils/dbConnection.js")
const getPaginationParams = require("../utils/pagination.js")

const getAllBooks = async (req) => {
const { page, per_page } = getPaginationParams(req)
const author = req.query.author

let sqlQuery = "select * from books"
let result = await dbConnection.query(sqlQuery)

const calculateOffset = () => (page - 1) * per_page

if (author) {
sqlQuery = "select * from books where author = $1 limit $2 offset $3"
result = await dbConnection.query(sqlQuery, [
author,
per_page,
calculateOffset(),
])
} else {
sqlQuery = "select * from books limit $1 offset $2"
result = await dbConnection.query(sqlQuery, [per_page, calculateOffset()])
}

return result.rows
}

const postNewBook = async (req) => {
const { title, type, author, topic, publication_date, pages } = req.body

if (
[title, type, author, topic, publication_date, pages].some(
(prop) => prop === undefined
)
) {
throw new MissingFieldError("Missing fields in the request body")
}

const sqlQuery =
"insert into books (title, type, author, topic, publication_date, pages) values ($1, $2, $3, $4, $5, $6) returning *"
const result = await dbConnection.query(sqlQuery, [
title,
type,
author,
topic,
publication_date,
pages,
])

return result.rows[0]
}

const getBookById = async (req) => {
const id = Number(req.params.id)
const sqlQuery = "select * from books where id = $1"
const result = await dbConnection.query(sqlQuery, [id])

if (result.rows.length === 0) {
throw new NotFoundError(`no book with id: ${id}`)
}

return result.rows[0]
}

const updateBookById = async (req) => {
const { title, type, author, topic, publication_date, pages } = req.body
const id = Number(req.params.id)
const conflictQuery = "select * from books where title = $1 and id != $2"
const conflictResult = await dbConnection.query(conflictQuery, [title, id])

if (conflictResult.rows.length > 0) {
throw new ConflictError(`A book with the title: ${title} already exists`)
}

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 dbConnection.query(sqlQuery, [
title,
type,
author,
topic,
publication_date,
pages,
id,
])

if (result.rows.length === 0) {
throw new NotFoundError(`no book with id: ${id}`)
}

return result.rows[0]
}

const deleteBookById = async (req) => {
const id = Number(req.params.id)
const sqlQuery = "delete from books where id = $1 returning *"
const result = await dbConnection.query(sqlQuery, [id])

if (result.rows.length === 0) {
throw new NotFoundError(`no book with id: ${id}`)
}

return result.rows[0]
}

module.exports = {
getAllBooks,
postNewBook,
getBookById,
updateBookById,
deleteBookById,
}
Loading