diff --git a/src/routers/books.js b/src/routers/books.js index 18b9a7c..ede79ae 100644 --- a/src/routers/books.js +++ b/src/routers/books.js @@ -1,4 +1,61 @@ // Import data here... +const express = require('express') +const router = express.Router() +const { books } = require('../../data') + // Write routes here... +//all books +router.get('/', (request, respond) => { + respond.status(200).json({ books }) +}) + +// the ID''S +router.get('/:id', (request, respond) => { + const book = books.find(b => b.id === parseInt(request.params.id)) + if (book) { + respond.status(200).json({ book }) + } else { + respond.status(404).json({ error: 'Book not found'}) + } +}) + +router.post('/', (request, respond) => { + const newBook = {id: books.length + 1, ...request.body} + books.push(newBook) + respond.status(201).json({ book: newBook }) +}) + +router.put('/:id', (request, respond) => { + const bookId = parseInt(request.params.id) + const updatedBook = request.body + + const bookIndex = books.findIndex(book => book.id === bookId) + + if (bookIndex !== -1) { + // Update the book found at bookIndex + books[bookIndex] = { id: bookId, ...updatedBook } + respond.status(200).json({ book: books[bookIndex] }) + } else { + respond.status(404).json({ error: "Book not found" }) + } +}); + +router.delete("/:id", (request, respond) => { + const bookIndex = books.findIndex(b => b.id === parseInt(request.params.id)) + if (bookIndex !== -1) { + const deletedBook = books.splice(bookIndex, 1)[0] + respond.status(200).json({ book: deletedBook }) + } else { + respond.status(404).json({ error: 'Book not found'}) + } +}) + + + + + + + +module.exports = router \ No newline at end of file diff --git a/src/routers/films.js b/src/routers/films.js index e69de29..635cf22 100644 --- a/src/routers/films.js +++ b/src/routers/films.js @@ -0,0 +1,48 @@ +const express = require('express') +const router = express.Router() +const { films } = require('../../data') + + +router.get('/', (request, respond) => { + respond.status(200).json({ films }) +}) + +router.get('/:id', (request, respond) => { + const film = films.find(f => f.id === parseInt (request.params.id)) + if (film) { + respond.status(200).json({ film }) + } else { + respond.status(404).json({ error: "film not found" }) + } +}) + +router.post('/', (request, respond) => { + const newFilm = {id: films.length +1, ...request.body} + films.push(newFilm) + respond.status(201).json({film: newFilm} ) +}) + +router.put('/:id', (request, respond) => { + const filmId = parseInt(request.params.id) + const { title, director } = request.body + const filmIndex = films.findIndex(f => f.id === filmId) + + if (filmIndex !== -1) { + // + films[filmIndex] = {id: filmId, title, director} + respond.status(200).json({ film: films[filmIndex] }) + } else { + respond.status(404).json ({error: 'Film not found' }) + } +}) + +router.delete('/:id', (request, respond) => { + const filmIndex = films.findIndex(b => b.id === parseInt(request.params.id)) + if (filmIndex !== -1) { + const deletedFilm = films.splice(filmIndex, 1)[0] + respond.status(200).json({ film: deletedFilm }) + } else { + respond.status(404).json({ error: 'Film not found'}) + } +}) +module.exports = router \ No newline at end of file diff --git a/src/routers/users.js b/src/routers/users.js index e69de29..34d1300 100644 --- a/src/routers/users.js +++ b/src/routers/users.js @@ -0,0 +1,49 @@ +const express = require('express') +const router = express.Router() +const { users } = require('../../data') + + +router.get('/', (request, respond) => { + respond.status(200).json({ users }) +}) + +router.get('/:id', (request, respond) => { + const user = users.find(u => u.id === parseInt (request.params.id)) + if (user) { + respond.status(200).json({ user }) + } else { + respond.status(404).json({ error: 'user not found' }) + } +}) + +router.post('/', (request, respond) => { + const newUser = {id: users.length +1, ...request.body} + users.push(newUser) + respond.status(201).json({ user: newUser }) +}) + +router.put('/:id', (request, respond) => { + const userId = parseInt(request.params.id) + const { email } = request.body + const userIndex = users.findIndex(u => u.id === userId) + + if (userIndex !== -1) { + + users[userIndex] = {id: userId, email} + respond.status(200).json({ user: users[userIndex] }) + } else { + respond.status(404).json({ error: 'User not found' }) + } +}) + +router.delete('/:id', (request, respond) => { + const userIndex = users.findIndex(u => u.id === parseInt(request.params.id)) + if (userIndex !== -1) { + const deletedUser = users.splice(userIndex, 1)[0] + respond.status(200).json({ user: deletedUser }) + } else { + respond.status(404).json({ error: 'User not found' }) + } +}) + +module.exports = router \ No newline at end of file diff --git a/src/server.js b/src/server.js index 715321f..9acf136 100644 --- a/src/server.js +++ b/src/server.js @@ -7,12 +7,17 @@ const morgan = require("morgan"); // SETUP MIDDLEWARE app.use(cors()); app.use(express.json()); -app.use(morgan("dev")); +app.use(morgan('dev')); // REQUIRE ROUTERS const usersRouter = require("./routers/users"); +const booksRouter = require("./routers/books") +const filmsRouter = require("./routers/films") -// ADD ROUTERS TO APP +// ADD ROUTERS TO APP +app.use("/users", usersRouter) +app.use("/films", filmsRouter) +app.use("/books", booksRouter) module.exports = app diff --git a/test/api/routes/films.spec.js b/test/api/routes/films.spec.js index 24590d0..6da385d 100644 --- a/test/api/routes/films.spec.js +++ b/test/api/routes/films.spec.js @@ -7,7 +7,7 @@ describe("Films Endpoint", () => { app = require("../../../src/server.js") }) describe("GET /films", () => { - it("will list all films", async () => { + it("will list all films", async () => { //done const response = await supertest(app).get("/films") expect(response.status).toEqual(200) @@ -15,7 +15,7 @@ describe("Films Endpoint", () => { expect(response.body.films.length).toEqual(4) }) - it("will list a film", async () => { + it("will list a film", async () => { //done const response = await supertest(app).get("/films/1") expect(response.status).toEqual(200) @@ -28,7 +28,7 @@ describe("Films Endpoint", () => { describe("POST /films", () => { - it("will create a new film", async () => { + it("will create a new film", async () => { //done const response = await supertest(app).post("/films").send(film1) expect(response.status).toEqual(201) @@ -42,7 +42,7 @@ describe("Films Endpoint", () => { }) describe("PUT /films", () => { - it("will update a film", async () => { + it("will update a film", async () => { //done const response = await supertest(app).put("/films/1").send(film2) expect(response.status).toEqual(200) @@ -53,7 +53,7 @@ describe("Films Endpoint", () => { }) }) - describe("DELETE /films", () => { + describe("DELETE /films", () => { //done it("will return the deleted the film", async () => { const response = await supertest(app).delete("/films/1")