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

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

93 changes: 93 additions & 0 deletions src/contollers/books.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
const { books } = require("../../data/index.js");
const {
MissingFieldError,
DataError,
NotFoundError,
} = require("../errors/index.js");

let bookID = books.length + 1;

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

const get = (req, res) => {
const id = Number(req.params.id);
const found = books.find((book) => book.id === id);

if (!found) {
res.status(404).send({ error: "A book the provided ID does not exist" });
throw new NotFoundError("A book the provided ID does not exist");
}

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

const create = (req, res) => {
const newBook = req.body;
if (!newBook.title || !newBook.author || !newBook.type) {
res.status(400).send({ error: `Missing fields in request body` });
throw new MissingFieldError(`Missing fields in request body`);
}

if (books.find((book) => book.title === newBook.title)) {
res
.status(409)
.send({ error: `A book with the provided title already exists` });
throw new MissingFieldError(
`A book with the provided title already exists`
);
}
newBook.id = bookID;
books.push(newBook);
bookID++;
res.status(201).json({ book: newBook });
};

const update = (req, res) => {
const id = Number(req.params.id);
const updates = req.body;

if (!updates.title || !updates.author || !updates.type) {
res.status(400).send({ error: `Missing fields in request body` });
throw new MissingFieldError(`Missing fields in the request body`);
}

const found = books.find((book) => book.id === id);
if (!found) {
res.status(404).send({ error: `A book the provided ID does not exist` });
throw new NotFoundError(`A book with the provided ID does not exist`);
}

if (books.find((book) => book.title === updates.title)) {
res.status(409).send({ error: `A book with the provided title already exists` });
throw new MissingFieldError(
`A book with the provided title already exists`
);
}

const index = books.indexOf(found);
const updated = { ...found, ...updates };
res.status(200).json({ book: updated });
};

const remove = (req, res) => {
const id = Number(req.params.id);
const found = books.find((book) => book.id === id);
if (!found) {
res.status(404).send({ error: `A book the provided ID does not exist` });
throw new NotFoundError(`Book with the provided ID does not exist`);
}

const index = books.indexOf(found);
books.splice(index, 1);
res.status(200).json({ book: found });
};

module.exports = {
all,
get,
create,
update,
remove,
};
117 changes: 117 additions & 0 deletions src/contollers/films.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
const { films } = require("../../data/index.js");

let filmID = films.length + 1;

const all = (req, res) => {
const { director } = req.query;
if (!director) {
res.status(200).json({ films: films });
}
const found = films.filter((film) => film.director === director);
res.status(200).json({ films: found });
};

const get = (req, res) => {
const id = Number(req.params.id);
const found = films.find((film) => film.id === id);
if (!found) {
res.status(404).send({ error: "A film with provided ID does not exist" });
throw new NotFoundError("A film with provided ID does not exist");
}
res.status(200).json({ film: found });
};

const create = (req, res) => {
const newfilm = req.body;
if (!newfilm.title || !newfilm.director) {
res.status(400).send({ error: `Missing fields in request body` });
throw new MissingFieldError(`Missing fields in request body`);
}
if (films.find((film) => film.title === newfilm.title)) {
res
.status(409)
.send({ error: `A film with the provided title already exists` });
throw new MissingFieldError(
`A film with the provided title already exists`
);
}
newfilm.id = filmID;
films.push(newfilm);
filmID++;
res.status(201).json({ film: newfilm });
};

const update = (req, res) => {
const id = Number(req.params.id);
const updates = req.body;
const found = films.find((film) => film.id === id);
if (!found) {
res.status(404).send({ error: `A film with provided ID does not exist` });
throw new NotFoundError(`A film with provided ID does not exist`);
}

if (!updates.title || !updates.director) {
res.status(400).send({ error: `Missing fields in the request body` });
throw new MissingFieldError(`Missing fields in the request body`);
}

if (films.find((film) => film.title === updates.title)) {
res
.status(409)
.send({ error: `A film with the provided title already exists` });
throw new MissingFieldError(`A film with the provided title already exists`);
}
const index = films.indexOf(found);
const updated = { ...found, ...updates };
res.status(200).json({ film: updated });
};

const remove = (req, res) => {
const id = Number(req.params.id);
const found = films.find((film) => film.id === id);
if (!found) {
res.status(404).send({ error: `A film with provided ID does not exist` });
throw new NotFoundError(`A film with provided ID does not exist`);
}
const index = films.indexOf(found);
films.splice(index, 1);
res.status(200).json({ film: found });
};

const patch = (req, res) => {
const id = Number(req.params.id);
const updates = req.body;

if (!updates.title && !updates.director) {
res.status(400).send({ error: `Missing fields in the request body` });
throw new MissingFieldError(`Missing fields in the request body`);
}

if (films.find((film) => film.title === updates.title)) {
res
.status(409)
.send({ error: `A film with the provided title already exists` });
throw new MissingFieldError(
`A film with the provided title already exists`
);
}

const found = films.find((film) => film.id === id);
if (!found) {
res.status(404).send({ error: `A film with provided ID does not exist` });
throw new NotFoundError(`A film with provided ID does not exist`);
}

const index = films.indexOf(found);
const updated = { ...found, ...updates };
res.status(200).json({ film: updated });
};

module.exports = {
all,
get,
create,
update,
remove,
patch
};
80 changes: 80 additions & 0 deletions src/contollers/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
const { users } = require("../../data/index.js");

let userID = users.length + 1;

const all = (req, res) => {
res.status(200).json({ users: users });
};

const get = (req, res) => {
const id = Number(req.params.id);
const found = users.find((user) => user.id === id);
if (!found) {
res.status(404).send({ error: "A user with the provided ID does not exist" });
throw new NotFoundError("A user with provided ID does not exist");
}
res.status(200).json({ user: found });
};

const create = (req, res) => {
const newUser = req.body;
if (!newUser.email) {
res.status(400).send({ error: `Missing fields in request body` });
throw new MissingFieldError(`Missing fields in request body`);
}
if (users.find((user) => user.email === newUser.email)) {
res
.status(409)
.send({ error: `A user with the provided email already exists` });
throw new MissingFieldError(`A user the with provided title already exists`);
}
newUser.id = userID;
users.push(newUser);
userID++;
res.status(201).json({ user: newUser });
};

const update = (req, res) => {
const id = Number(req.params.id);
const updates = req.body;
const found = users.find((user) => user.id === id);
if (!found) {
res.status(404).send({ error: `A user with the provided ID does not exist` });
throw new NotFoundError(`A user with the provided ID does not exist`);
}

if (!updates.email) {
res.status(400).send({ error: `Missing fields in the request body` });
throw new MissingFieldError(`Missing fields in the request body`);
}

if (users.find((user) => user.email === updates.email)) {
res
.status(409)
.send({ error: `A user with the provided email already exists` });
throw new MissingFieldError(`A user with the provided email already exists`);
}
const index = users.indexOf(found);
const updated = { ...found, ...updates };
res.status(200).json({ user: updated });
};

const remove = (req, res) => {
const id = Number(req.params.id);
const found = users.find((user) => user.id === id);
if (!found) {
res.status(404).send({ error: `A user with the provided ID does not exist` });
throw new NotFoundError(`A user with the provided ID does not exist`);
}
const index = users.indexOf(found);
users.splice(index, 1);
res.status(200).json({ user: found });
};

module.exports = {
all,
get,
create,
update,
remove,
};
Loading