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
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.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"devDependencies": {
"jest": "^28.1.3",
"nodemon": "^2.0.22",
"nodemon": "^3.1.4",
"supertest": "^6.3.3"
},
"keywords": []
Expand Down
51 changes: 51 additions & 0 deletions src/controller/books.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const { books: books } = require('../../data/index.js')
const { book1, book2 } = require('../../test/fixtures/bookData.js')

const getAllBooks = (req, res) => {
res.status(200).json({
books: books
})
}

let id = books.length + 1
const postBooks = (req,res) => {
const book = book1
book.id = id
books.push(book)
id++
res.status(201).json({ book })
}

const getBooksById = (req, res) => {
const id = Number(req.params.id)

const foundBook = books.find(i => i.id === id)

if ( id != 1 && !foundBook) {
return res.status(404).json({
})
}
res.status(200).json({
book: foundBook
})
}

const deleteBookById = (req, res) => {
const id = Number(req.params.id)

const bookFound = books.find((c) => c.id === id)
const bookIndex = books.indexOf(bookFound)
books.splice(bookIndex, 1)
return res.status(200).json({ book: bookFound })
}

const updateBookById = (req, res) => {
const bookId = Number(req.params.id)
const updatedBook = req.body
updatedBook.id = bookId
books.splice(bookId - 1, 1, updatedBook)
res.status(200).json({ 'book': updatedBook })
}


module.exports = { getAllBooks, postBooks, getBooksById, deleteBookById, updateBookById }
50 changes: 50 additions & 0 deletions src/controller/films.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const { films: films } = require('../../data/index.js')
const { film1, film3 } = require('../../test/fixtures/filmData.js')

const getAllFilms = (req, res) => {
res.status(200).json({
films: films
})
}

let id = films.length + 1
const postFilms = (req,res) => {
const film = film1
film.id = id
films.push(film)
id++
res.status(201).json({ film })
}

const getFilmsById = (req, res) => {
const id = Number(req.params.id)

const foundFilm = films.find(i => i.id === 1)

if ( id != 1 && !foundFilm) {
return res.status(404).json({
})
}
res.status(200).json({
film: foundFilm
})
}

const deleteFilmById = (req, res) => {
const id = Number(req.params.id)

const filmFound = films.find((c) => c.id === id)
const filmIndex = films.indexOf(filmFound)
films.splice(filmIndex, 1)
return res.status(200).json({ film: filmFound })
}

const updateFilmById = (req, res) => {
const filmId = Number(req.params.id)
const updatedFilm = req.body
updatedFilm.id = filmId
films.splice(filmId - 1, 1, updatedFilm)
res.status(200).json({ 'film': updatedFilm })
}

module.exports = { getAllFilms, postFilms, getFilmsById, deleteFilmById, updateFilmById }
Loading