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
34 changes: 17 additions & 17 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.2",
"express": "^4.19.2",
"morgan": "^1.10.0"
},
"devDependencies": {
Expand Down
139 changes: 138 additions & 1 deletion src/routers/books.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,141 @@
const router = require("express").Router();
// Import data here...

let books = require("../../data/index.js").books;

// Write routes here...
let newBookId = 4;

router.get("/", (req, res) => {
res.status(200).json({ books });
});

router.post("/", (req, res) => {
const newBook = req.body;
// const requiredProperties = ["title", "type", "author", "pages"];

// for (const item of requiredProperties) {
// if (newBook[item] === undefined) {
// return res
// .status(400)
// .json({ error: "Missing fields in request body" });
// }
// }

if (!newBook.title || !newBook.author || !newBook.type) {
res.status(400).json({ error: "Missing fields in request body" });
}

const matchedBook = books.find((book) => book.title === newBook.title);

if (matchedBook) {
res.status(409).json({ error: "A book with the provided title already exists" });
} else {
newBookId += 1;
newBook.id = newBookId;
books.push(newBook);
res.status(201).json({ book: newBook });
}
});

router.get("/:id", (req, res) => {
const id = Number(req.params.id);

const foundBook = books.find((book) => book.id === id);

if (!foundBook) {
res
.status(404)
.json({ error: "A book the provided ID does not exist" });
}

res.status(200).json({ book: foundBook });
});

router.delete("/:id", (req, res) => {
const id = Number(req.params.id);

const foundBook = books.find((book) => book.id === id);

if (!foundBook) {
res
.status(404)
.json({ error: "A book with the provided ID does not exist" });
}

books = books.filter((book) => book.id !== foundBook.id);

res.status(200).json({ book: foundBook });
});

router.put("/:id", (req, res) => {
const updatedBook = req.body;
const id = Number(req.params.id);
if (
!updatedBook.title ||
!updatedBook.author ||
!updatedBook.type
) {
res.status(400).json({ error: "Missing fields in request body" });
}

const foundBook = books.find((book) => book.id === id);

if (!foundBook) {
res
.status(404)
.json({ error: "A book with the provided ID does not exist" });
}

const matchedBookTitle = books.find(
(book) => book.title === updatedBook.title
);

if (matchedBookTitle) {
res
.status(409)
.json({ error: "A book with the provided title already exists" });
}

const existingBookIndex = books.findIndex((book) => book.id === id);

updatedBook.id = id;

books.splice(existingBookIndex, 1, updatedBook);

res.status(200).json({ book: updatedBook });
});

router.patch("/:id", (req, res) => {
const updatedBook = req.body;
const id = Number(req.params.id);

const foundBook = books.find((book) => book.id === id);

if (!foundBook) {
res
.status(404)
.json({ error: "A book with the provided ID does not exist" });
}

const matchedBookTitle = books.find(
(book) => book.title === updatedBook.title
);

if (matchedBookTitle) {
return res
.status(409)
.json({ error: "A book with the provided title already exists" });
}

const existingBookIndex = books.findIndex((book) => book.id === id);

books[existingBookIndex] = {
...books[existingBookIndex],
...updatedBook,
};

return res.status(400).json({ book: updatedBook });

});

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

let films = require("../../data/index.js").films;

let newFilmId = 4;

router.get("/", (req, res) => {
if (req.query.director) {
const director = req.query.director;
const filteredFilms = films.filter((film) => film.director === director);

res.status(200).json({ films: filteredFilms });
}
res.status(200).json({ films });
});

router.post("/", (req, res) => {
const newFilm = req.body;

if (!newFilm.title || !newFilm.director) {
return res.status(400).json({ error: "Missing fields in request body" });
}

const matchedFilm = films.find((film) => film.title === newFilm.title);

if (matchedFilm) {
res
.status(409)
.json({ error: "A film with the provided title already exists" });
}

newFilmId += 1;
newFilm.id = newFilmId;

films.push(newFilm);
res.status(201).json({ film: newFilm });

});

router.get("/:id", (req, res) => {
const id = Number(req.params.id);

const foundFilm = films.find((film) => film.id === id);

if (!foundFilm) {
res.status(404).json({ error: "A film with provided ID does not exist" });
}

res.status(200).json({ film: foundFilm });
});

router.delete("/:id", (req, res) => {
const id = Number(req.params.id);

const foundFilm = films.find((film) => film.id === id);

if (!foundFilm) {
res.status(404).json({ error: "A film with provided ID does not exist" });
}

films = films.filter((film) => film.id !== foundFilm.id);

res.status(200).json({ film: foundFilm });
});

router.put("/:id", (req, res) => {
const updatedFilm = req.body;
const id = Number(req.params.id);

const foundFilm = films.find((film) => film.id === id);

if (!foundFilm) {
res.status(404).json({ error: "A film with provided ID does not exist" });
}

const matchedFilm = films.find((film) => film.title === updatedFilm.title);

if (matchedFilm) {
res
.status(409)
.json({ error: "A film with the provided title already exists" });
}

const existingFilmIndex = films.findIndex((film) => film.id === id);

updatedFilm.id = id;

films.splice(existingFilmIndex, 1, updatedFilm);

res.status(200).json({ film: updatedFilm });
});

router.patch("/:id", (req, res) => {
const updatedFilm = req.body;
const id = Number(req.params.id);

if (!updatedFilm.title) {
return res.status(400).json({ error: "Missing fields in request body" });
}

const foundFilm = films.find((film) => film.id === id);

if (!foundFilm) {
res.status(404).json({ error: "A film with provided ID does not exist" });
}

const matchedFilmTitle = films.find(
(film) => film.title === updatedFilm.title
);

if (matchedFilmTitle) {
res
.status(409)
.json({ error: "A film with the provided title already exists" });
}

const existingFilmIndex = films.findIndex((film) => film.id === id);

films[existingFilmIndex] = {
...films[existingFilmIndex],
...updatedFilm,
};

res.status(200).json({ film: updatedFilm });
});

module.exports = router;
Loading