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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_modules
node_modules/*
build
build/*
.env
9 changes: 9 additions & 0 deletions data/deletedData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const deletedUsers = []
const deletedFilms = []
const deletedBooks = []

module.exports = {
deletedUsers,
deletedFilms,
deletedBooks
}
159 changes: 159 additions & 0 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 @@ -10,12 +10,14 @@
},
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.18.2",
"morgan": "^1.10.0"
},
"devDependencies": {
"jest": "^28.1.3",
"nodemon": "^2.0.22",
"pg": "^8.12.0",
"supertest": "^6.3.3"
},
"keywords": []
Expand Down
115 changes: 115 additions & 0 deletions src/controllers/books/books.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
const { getAllBooks, getBookByID, filterByTitle } = require("../../domain/books/books")
const newID = require("../../functions/createID")
const { deletedBooks } = require('../../../data/deletedData.js')

let newBook = {
id: 0,
title: 'string',
type: 'string',
author: 'string'
}

const getBooks = (req, res) => {
res.status(200).json({
books: getAllBooks()
})
}

const addBook = (req, res) => {
newBook.id = newID(getAllBooks())
newBook.title = req.body.title
newBook.type = req.body.type
newBook.author = req.body.author

if (
req.body.title === "" ||
req.body.type === "" ||
req.body.author === ""
) {
throw new FieldsMissing("Missing fields")
}

const checkTitle = filterByTitle(newBook.title)

if(checkTitle) {
throw new AlreadyExistsError("Book already exists")
}

getAllBooks().push(newBook)
res.status(201).json({
book: newBook
})
}

const getByID = (req, res) => {
const id = Number(req.params.id)
const found = getBookByID(id)

if (typeof id !== "number") {
throw new InvalidDataError("ID must be a number")
}

if (!found) {
throw new NotFoundError("Book not found")
}

res.status(200).json({
book: found
})
}

const removeBook = (req, res) => {
const id = Number(req.params.id)
const found = getBookByID(id)

if (typeof id !== "number") {
throw new InvalidDataError("ID must be a number")
}

if (!found) {
throw new NotFoundError("Book not found")
}

deletedBooks.push(found)
const index = getAllBooks().indexOf(found)
getAllBooks().splice(index, 1)
res.status(200).json({
book: found
})
}

const updateBook = (req, res) => {
const id = Number(req.params.id)
const found = getBookByID(id)

if (typeof id !== "number") {
throw new InvalidDataError("ID must be a number")
}


if (!found) {
throw new NotFoundError("Book not found")
}

found.title = req.body.title
found.type = req.body.type
found.author = req.body.author

const checkTitle = filterByTitle(found.title)

if(!checkTitle) {
throw new AlreadyExistsError("Book already exists")
}

res.status(200).json({
book: found
})
}

module.exports = {
getBooks,
addBook,
getByID,
removeBook,
updateBook
}
Loading