Skip to content

Ryan #86

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 23 commits into
base: main
Choose a base branch
from
Open

Ryan #86

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
50 changes: 48 additions & 2 deletions src/routers/books.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,50 @@
// Import data here...
const { books } = require('../../data/index.js')

const express = require('express')
const router = express.Router()

// Write routes here...
const findBook = (id) => {
return books.find(book => book.id === parseInt(id))
}

router.get('/', (req, res) => {
res.json({ books })
})

router.post('/', (req, res) => {
const body = req.body
const newBook = { id: books.length + 1, ...body }
books.push(newBook)
res.status(201).json({ book: newBook })
})

router.get('/:id', (req, res) => {
const book = findBook(req.params.id)
if (book) {
res.json({ book })
} else {
res.status(404).json({ error: 'Book not found' })
}
})

router.delete('/:id', (req, res) => {
const book = findBook(req.params.id)
if (book) {
books.splice(books.indexOf(book), 1)
res.json({ book })
} else {
res.status(404).json({ error: 'Book not found' })
}
})

router.put('/:id', (req, res) => {
const book = findBook(req.params.id)
if (book) {
Object.assign(book, req.body)
res.json({ book })
} else {
res.status(404).json({ error: 'Book not found' })
}
})

module.exports = router;
50 changes: 50 additions & 0 deletions src/routers/films.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const { films } = require('../../data/index.js')

const express = require('express')
const router = express.Router()

const findFilm = (id) => {
return films.find(film => film.id === parseInt(id))
}

router.get('/', (req, res) => {
res.json({ films })
})

router.post('/', (req, res) => {
const body = req.body
const newFilm = { id: films.length + 1, ...body }
films.push(newFilm)
res.status(201).json({ film: newFilm })
})

router.get('/:id', (req, res) => {
const film = findFilm(req.params.id)
if (film) {
res.json({ film })
} else {
res.status(404).json({ error: 'film not found' })
}
})

router.delete('/:id', (req, res) => {
const film = findFilm(req.params.id)
if (film) {
films.splice(films.indexOf(film), 1)
res.json({ film })
} else {
res.status(404).json({ error: 'film not found' })
}
})

router.put('/:id', (req, res) => {
const film = findFilm(req.params.id)
if (film) {
Object.assign(film, req.body)
res.json({ film })
} else {
res.status(404).json({ error: 'film not found' })
}
})

module.exports = router;
50 changes: 50 additions & 0 deletions src/routers/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const { users } = require('../../data/index.js')

const express = require('express')
const router = express.Router()

const findUser = (id) => {
return users.find(user => user.id === parseInt(id))
}

router.get('/', (req, res) => {
res.json({ users })
})

router.post('/', (req, res) => {
const body = req.body
const newUser = { id: users.length + 1, ...body }
users.push(newUser)
res.status(201).json({ user: newUser })
})

router.get('/:id', (req, res) => {
const user = findUser(req.params.id)
if (user) {
res.json({ user })
} else {
res.status(404).json({ error: 'user not found' })
}
})

router.delete('/:id', (req, res) => {
const user = findUser(req.params.id)
if (user) {
users.splice(users.indexOf(user), 1)
res.json({ user })
} else {
res.status(404).json({ error: 'user not found' })
}
})

router.put('/:id', (req, res) => {
const user = findUser(req.params.id)
if (user) {
Object.assign(user, req.body)
res.json({ user })
} else {
res.status(404).json({ error: 'user not found' })
}
})

module.exports = router;
7 changes: 6 additions & 1 deletion src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ 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

app.use("/users", usersRouter);
app.use('/books', booksRouter);
app.use('/films', filmsRouter);

module.exports = app
module.exports = app;