Skip to content

Periklis #94

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 7 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
2 changes: 1 addition & 1 deletion data/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const books = [
const books = [
{
id: 1,
title: "1984",
Expand Down
115 changes: 65 additions & 50 deletions package-lock.json

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

12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "express-router-with-in-memory-data-store",
"version": "1.0.0",
"description": "",
"description": "In this exercise, you are going to extend the REST APIs you have been building to handle DELETE and PUT requests. You will also learn how to make your route handling more modular in express using express.Router.",
"main": "index.js",
"scripts": {
"start": "npx nodemon src/index.js",
Expand All @@ -15,8 +15,14 @@
},
"devDependencies": {
"jest": "^28.1.3",
"nodemon": "^2.0.22",
"nodemon": "^3.1.3",
"supertest": "^6.3.3"
},
"keywords": []
"keywords": [],
"directories": {
"doc": "docs",
"test": "test"
},
"author": "",
"license": "ISC"
}
94 changes: 94 additions & 0 deletions src/controllers/booksController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
const booksData = require("../../data/index.js")
const {
MissingFieldsError,
ExistingDataError,
DataNotFoundError,
} = require("../errors/errors.js")

const books = booksData.books
let newBookId = books.length + 1

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

const createNewBook = (req, res) => {
const newBook = req.body

if (!newBook.title || !newBook.author || !newBook.type) {
throw new MissingFieldsError("Missing fields in request body")
}
if (books.find((b) => b.title === newBook.title)) {
throw new ExistingDataError(
"A book with the provided title already exists"
)
}
newBook.id = newBookId
newBookId += 1
books.push(newBook)
res.status(201).json({ book: newBook })
}

const getBookById = (req, res) => {
const bookId = Number(req.params.id)
const foundBook = books.find((b) => b.id === bookId)
if (!foundBook) {
throw new DataNotFoundError(
"A book the provided ID does not exist"
)
}
res.status(200).json({ book: foundBook })
}

const deleteBookById = (req, res) => {
const bookId = Number(req.params.id)
const bookToDelete = books.find((b) => b.id === bookId)
if (!bookToDelete) {
throw new DataNotFoundError(
"A book the provided ID does not exist"
)
}
const indexToDelete = books.indexOf(bookToDelete)
books.splice(indexToDelete, 1)
res.status(200).json({ book: bookToDelete })
}

const updateBookById = (req, res) => {
const bookId = Number(req.params.id)
const updateBook = req.body
const foundBook = books.find((b) => b.id === bookId)

if (
!updateBook.title ||
!updateBook.type ||
!updateBook.author
) {
throw new MissingFieldsError('Missing fields in request body')
}

if (!foundBook) {
throw new DataNotFoundError(
"A book the provided ID does not exist"
)
}


const existingTitle = books.find((b) => b.title === updateBook.title)
if (existingTitle) {
throw new ExistingDataError(
"A book with the provided title already exists"
)
}

updateBook.id = bookId
books.splice(bookId - 1, 1, updateBook)
res.status(200).json({ book: updateBook })
}

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