All files / src/controller bookController.js

100% Statements 23/23
100% Branches 0/0
100% Functions 4/4
100% Lines 23/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 413x   3x   3x 1x 1x     3x 2x 2x 2x 1x   1x       3x 2x 2x       2x 1x   1x       3x 2x 2x 2x 1x   1x    
const bookRepository = new (require("../repository/bookRepository"))([]);
 
const BookService = require('../services/bookService');
 
exports.getBooks = async(req,res) => {
  const books = await bookRepository.findMany()
  res.json(books)
}
 
exports.createBook = async(req,res) => {
  try {
    const book = new BookService(bookRepository, req.body)
    const newBook = await book.save()
    res.json(newBook)
  } catch (error) {
    res.status(400).json({ message: error.message })
  }
}
 
exports.updateBook = async(req,res) => {
  try {
    const book = new BookService(bookRepository, {
      id: req.params.id,
      ...req.body
    })
    const updatedBook = await book.update()
    return res.json(updatedBook)
  } catch (error) {
    return res.status(400).json({ message: error.message })
  }
}
 
exports.deleteBook = async(req,res) => {
  try {
    const book = new BookService(bookRepository, { id: req.params.id })  
    await book.delete()
    res.json({ message: `Book with id ${req.params.id} deleted!!`})
  } catch (error) {
    res.status(400).json({ message: error.message })
  }
}