Skip to content

Myrthe Dullaart #118

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

This file was deleted.

60 changes: 16 additions & 44 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"express": "^4.19.2",
"express-async-errors": "^3.1.1",
"faker": "^5.5.3",
"morgan": "1.10.0",
"pg": "8.6.0",
Expand Down
114 changes: 114 additions & 0 deletions src/dal/booksRepo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
const dbConnection = require('../utils/dbConnection.js')
const MissingFieldsError = require('../errors/missingFieldsError.js')
const NotFoundError = require('../errors/notFoundError.js')
const NotUniqueError = require('../errors/notUniqueError.js')

const getAllBooks = async (type, topic, author, pages, perpages) => {
let page = 1
let perpage = 20
let offset = 0

if (pages) {
page = pages
offset = (page - 1) * perpage
}

if (perpages > 9 && perpages < 51) {
perpage = perpages
offset = (page - 1) * perpage
}

if (type) {
const sqlQuery = `select * from books where type = $1 limit $2 offset $3`
const result = await dbConnection.query(sqlQuery, [type, perpage, offset])

return result.rows
}

if (topic) {
const sqlQuery = `select * from books where topic = $1 limit $2 offset $3`
const result = await dbConnection.query(sqlQuery, [topic, perpage, offset])

return result.rows
}

if (author) {
const sqlQuery = `select * from books where author = $1 limit $2 offset $3`
const result = await dbConnection.query(sqlQuery, [author, perpage, offset])

return result.rows
}

const sqlQuery = `select * from books limit $1 offset $2`
const result = await dbConnection.query(sqlQuery, [perpage, offset])

return result.rows
}

const createBook = async (book) => {
if (!verifyFields(book)) {
throw new MissingFieldsError('Missing fields in 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, [book.title, book.type, book.author, book.topic, book.publication_date, book.pages])

return result.rows[0]
}

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

if(result.rows.length === 0) {
throw new NotFoundError('A book with the provided ID does not exist')
}

return result.rows[0]
}

const updateBook = async (id, bookInfo) => {
if (!verifyFields(bookInfo)) {
throw new MissingFieldsError('Missing fields in request body')
}

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

if(result.rows.length === 0) {
throw new NotFoundError('A book with the provided ID does not exist')
}

return result.rows[0]
}

const deleteBookById = async (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('A book with the provided ID does not exist')
}

return result.rows[0]
}

function verifyFields(object) {
const neededProperties = ['title', 'type', 'author', 'topic', 'publication_date', 'pages']

for (const item of neededProperties) {
if (object[item] === undefined) {
return false
}
}

return true
}

module.exports = {
getAllBooks,
createBook,
getBookById,
updateBook,
deleteBookById
}
111 changes: 111 additions & 0 deletions src/dal/petsRepo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
const MissingFieldsError = require('../errors/missingFieldsError.js')
const NotFoundError = require('../errors/notFoundError.js')
const dbConnection = require('../utils/dbConnection.js')

const getAllPets = async (type, pages, perpages) => {
let page = 1
let perpage = 20
let offset = 0

if (pages) {
page = pages
offset = (page - 1) * perpage
}

if (perpages > 9 && perpages < 51) {
perpage = perpages
offset = (page - 1) * perpage
}

if (type) {
const sqlQuery = `select * from pets where type = $1 limit $2 offset $3`
const result = await dbConnection.query(sqlQuery, [type, perpage, offset])

return result.rows
}

const sqlQuery = 'select * from pets limit $1 offset $2'
const result = await dbConnection.query(sqlQuery, [perpage, offset])

return result.rows
}

const createPet = async (pet) => {
if (!verifyFields(pet)) {
throw new MissingFieldsError('Missing fields in request body')
}

const sqlQuery = `insert into pets (name, age, type, breed, has_microchip) values ($1, $2, $3, $4, $5) returning *`
const result = await dbConnection.query(sqlQuery, [pet.name, pet.age, pet.type, pet.breed, pet.has_microchip])

return result.rows[0]
}

const getPetById = async (id) => {
const sqlQuery = `select * from pets where id = $1`
const result = await dbConnection.query(sqlQuery, [id])

if(result.rows.length === 0) {
throw new NotFoundError('A pet with the provided ID does not exist')
}

return result.rows[0]
}

const updatePet = async (id, petInfo) => {
if (!verifyFields(petInfo)) {
throw new MissingFieldsError('Missing fields in request body')
}

const sqlQuery = `update pets set name = $1, age = $2, type = $3, breed = $4, has_microchip = $5 where id = $6 returning *`
const result = await dbConnection.query(sqlQuery, [petInfo.name, petInfo.age, petInfo.type, petInfo.breed, petInfo.has_microchip, id])

if(result.rows.length === 0) {
throw new NotFoundError('A pet with the provided ID does not exist')
}

return result.rows[0]
}

const deletePetById = async (id) => {
const sqlQuery = `delete from pets where id = $1 returning *`
const result = await dbConnection.query(sqlQuery, [id])

if(result.rows.length === 0) {
throw new NotFoundError('A pet with the provided ID does not exist')
}

return result.rows[0]
}

const getAllBreeds = async (type) => {
if (!type) {
throw new MissingFieldsError('Animal type is required')
}

const sqlQuery = `select distinct breed from pets where type = $1`
const result = await dbConnection.query(sqlQuery, [type])

return result.rows
}

function verifyFields(object) {
const neededProperties = ['name', 'age', 'type', 'breed', 'has_microchip']

for (const item of neededProperties) {
if (object[item] === undefined) {
return false
}
}

return true
}

module.exports = {
getAllPets,
createPet,
getPetById,
updatePet,
deletePetById,
getAllBreeds
}
5 changes: 5 additions & 0 deletions src/errors/missingFieldsError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class MissingFieldsError extends Error {

}

module.exports = MissingFieldsError
5 changes: 5 additions & 0 deletions src/errors/notFoundError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class NotFoundError extends Error {

}

module.exports = NotFoundError
5 changes: 5 additions & 0 deletions src/errors/notUniqueError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class NotUniqueError extends Error {

}

module.exports = NotUniqueError
Loading