Skip to content

Olaoluwa Oyebode_Pull Request #105

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 1 commit 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
114 changes: 64 additions & 50 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "express-router-with-in-memory-data-store",
"version": "1.0.0",
"description": "",
"main": "index.js",
"main": "src/index.js",
"scripts": {
"start": "npx nodemon src/index.js",
"test": "npx jest -i test/api/routes --forceExit --runInBand",
Expand All @@ -15,7 +15,7 @@
},
"devDependencies": {
"jest": "^28.1.3",
"nodemon": "^2.0.22",
"nodemon": "^3.1.4",
"supertest": "^6.3.3"
},
"keywords": []
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* REQUIRE APP */
const app = require('./server.js')
const app = require("./server.js");
const port = 3030;

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

const books = [
{ id: 1, title: "1984", type: "fiction", author: "George Orwell", pages: 5 },
{
id: 2,
title: "Life of Pi",
type: "fiction",
author: "Yann Martel",
pages: 4,
},
{
id: 3,
title: "How to Win Friends and Influence People",
type: "non-fiction",
author: "Dale Carnegie",
pages: 3,
},
{
id: 4,
title: "The Lean Startup",
type: "non-fiction",
author: "Eric Reis",
pages: 2,
},
];

// Write routes here...
// Endpoint to get all books
router.get("/", (req, res) => {
res.json({ books });
});

// Endpoint to get a book by ID
router.get("/:id", (req, res) => {
const book = books.find((b) => b.id === parseInt(req.params.id));
if (!book) {
return res
.status(404)
.send({ error: "A book the provided ID does not exist" });
}
res.json({ book });
});

// Endpoint to create a new book
router.post("/", (req, res) => {
const { title, type, author, pages } = req.body;
if (!title || !type || !author || pages === undefined) {
return res.status(400).send({ error: "Missing fields in request body" });
}
const existingBook = books.find((b) => b.title === title);
if (existingBook) {
return res
.status(409)
.send({ error: "A book with the provided title already exists" });
}
const newBook = {
id: books.length + 1,
title,
type,
author,
pages,
};
books.push(newBook);
res.status(201).json({ book: newBook });
});

// Endpoint to update a book by ID
router.put("/:id", (req, res) => {
const { title, type, author, pages } = req.body;
if (!title || !type || !author || pages === undefined) {
return res.status(400).send({ error: "Missing fields in request body" });
}
const book = books.find((b) => b.id === parseInt(req.params.id));
if (!book) {
return res
.status(404)
.send({ error: "A book with the provided ID does not exist" });
}
const existingBook = books.find((b) => b.title === title && b.id !== book.id);
if (existingBook) {
return res
.status(409)
.send({ error: "A book with the provided title already exists" });
}
Object.assign(book, req.body);
res.status(200).json({ book });
});

// Endpoint to partially update a book by ID
router.patch("/:id", (req, res) => {
const book = books.find((b) => b.id === parseInt(req.params.id));
if (!book) {
return res
.status(404)
.send({ error: "A book with the provided ID does not exist" });
}
if (req.body.title) {
const existingBook = books.find(
(b) => b.title === req.body.title && b.id !== book.id
);
if (existingBook) {
return res
.status(409)
.send({ error: "A book with the provided title already exists" });
}
}
Object.assign(book, req.body);
res.status(200).json({ book });
});

// Endpoint to delete a book by ID
router.delete("/:id", (req, res) => {
const bookIndex = books.findIndex((b) => b.id === parseInt(req.params.id));
if (bookIndex === -1) {
return res
.status(404)
.send({ error: "A book the provided ID does not exist" });
}
const deletedBook = books.splice(bookIndex, 1);
res.status(200).json({ book: deletedBook[0] });
});

module.exports = router;
Loading
Loading