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
57 changes: 57 additions & 0 deletions src/routers/books.js
Original file line number Diff line number Diff line change
@@ -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
48 changes: 48 additions & 0 deletions src/routers/films.js
Original file line number Diff line number Diff line change
@@ -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
49 changes: 49 additions & 0 deletions src/routers/users.js
Original file line number Diff line number Diff line change
@@ -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
9 changes: 7 additions & 2 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 5 additions & 5 deletions test/api/routes/films.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ 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)
expect(response.body.films).not.toEqual(undefined)
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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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")

Expand Down