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
32 changes: 16 additions & 16 deletions data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,71 +4,71 @@ const books = [
title: "1984",
type: "fiction",
author: "George Orwell",
pages: 5
pages: 5,
},
{
id: 2,
title: "Life of Pi",
type: "fiction",
author: "Yann Martel",
pages: 4
pages: 4,
},
{
id: 3,
title: "How to Win Friends and Influence People",
type: "non-fiction",
author: "Dale Carnegie",
pages: 3
pages: 3,
},
{
id: 4,
title: "The Lean Startup",
type: "non-fiction",
author: "Eric Reis",
pages: 2
}
pages: 2,
},
];

const films = [
{
id: 1,
title: "Bonnie and Clyde",
director: "Arthur Penn"
director: "Arthur Penn",
},
{
id: 2,
title: "Reservoir Dogs",
director: "Quentin Tarantino"
director: "Quentin Tarantino",
},
{
id: 3,
title: "Inception",
director: "Christopher Nolan"
director: "Christopher Nolan",
},
{
id: 4,
title: "Django Unchained",
director: "Quentin Tarantino"
}
director: "Quentin Tarantino",
},
];

const users = [
{
id: 1,
email: "edward@mail.com"
email: "edward@mail.com",
},
{
id: 2,
email: "nathan@mail.com"
email: "nathan@mail.com",
},
{
id: 3,
email: "mike@mail.com"
}
email: "mike@mail.com",
},
];

module.exports = {
books: books,
films: films,
users: users
}
users: users,
};
35 changes: 18 additions & 17 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 @@ -10,13 +10,19 @@
},
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.2",
"express": "^4.19.2",
"morgan": "^1.10.0"
},
"devDependencies": {
"jest": "^28.1.3",
"nodemon": "^2.0.22",
"supertest": "^6.3.3"
},
"keywords": []
"keywords": [],
"directories": {
"doc": "docs",
"test": "test"
},
"author": "",
"license": "ISC"
}
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* REQUIRE APP */
const app = require('./server.js')
const port = 3030;
const app = require("./server.js");
const port = 3031;

/* START SERVER */
app.listen(port, () => {
Expand Down
73 changes: 71 additions & 2 deletions src/routers/books.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,73 @@
// Import data here...
const express = require("express");
const router = express.Router();

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

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

router.post("/", function (req, res) {
const { title, type, author } = req.body;
if (!title || !type || !author) {
return res.status(400).json({ error: "error" });
}
const currentHighId = books.reduce((max, obj) => {
return obj.id > max ? obj.id : max;
}, 0);
const newBook = {
id: currentHighId + 1,
title,
type,
author,
};
console.log(req.body);
books.push(newBook);
res.status(201).json({ book: newBook });
});

router.get("/:id", function (req, res) {
const toFind = parseInt(req.params.id, 10);
const index = books.findIndex((obj) => obj.id === toFind);
const foundBook = books[index];
if (foundBook) {
res.status(200).json({ book: foundBook });
} else {
res.sendStatus(404);
}
});

router.delete("/:id", function (req, res) {
const toRemove = parseInt(req.params.id, 10);
const bookIndex = books.findIndex((obj) => obj.id === toRemove);
if (bookIndex === -1) {
return res.status(404).json({ error: "Book not found" });
}

const deletedBook = books[bookIndex];
books = books.filter((obj) => obj.id !== toRemove);

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

router.put("/:id", function (req, res) {
const id = parseInt(req.params.id, 10);
const bookIndex = books.findIndex((book) => book.id === id);
if (bookIndex === -1) {
return res.status(404).json({ error: "Book not found" });
}

const { title, type, author, pages } = req.body;
const updatedBook = {
...books[bookIndex],
title,
type,
author,
pages,
};
books[bookIndex] = updatedBook;

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

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

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

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

router.post("/", function (req, res) {
const { title, director } = req.body;
if (!title || !director) {
return res.status(400);
}
const currentHighId = films.reduce((max, obj) => {
return obj.id > max ? obj.id : max;
}, 0);
req.body.id = currentHighId + 1;
films.push(req.body);
res.status(201).json({ film: req.body });
});

router.get("/:id", function (req, res) {
const toFind = parseInt(req.params.id, 10);
const index = films.findIndex((obj) => obj.id === toFind);
const foundFilm = films[index];
if (foundFilm) {
res.send({ film: foundFilm });
} else {
res.sendStatus(404);
}
});

router.delete("/:id", function (req, res) {
const toRemove = parseInt(req.params.id, 10);
const filmIndex = films.findIndex((obj) => obj.id === toRemove);
if (filmIndex === -1) {
return res.status(404).json({ error: "Film not found" });
}

const deletedFilm = films[filmIndex];
films = films.filter((obj) => obj.id !== toRemove);

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

router.put("/:id", function (req, res) {
const id = parseInt(req.params.id, 10);
const filmIndex = films.findIndex((film) => film.id === id);
if (filmIndex === -1) {
return res.status(404).json({ error: "Film not found" });
}

const { title, director } = req.body;
const updatedFilm = {
...films[filmIndex],
title,
director,
};
films[filmIndex] = updatedFilm;

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

module.exports = router;
Loading