All files / controllers BookController.js

50.87% Statements 29/57
25% Branches 11/44
66.66% Functions 4/6
50.87% Lines 29/57

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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163  2x 2x   2x   2x 2x 2x             2x 2x 2x   2x 1x       1x 1x   1x             2x 2x   2x 1x     1x 1x             2x                   2x   2x 1x           1x 1x 1x 1x                                                                                                                                                                                   2x  
// Book controller
const Book = require("../models/Book");
const Borrowing = require("../models/Borrowing");
 
const BookController = {
  async getAll(req, res) {
    try {
      const books = await Book.getAll();
      res.json(books);
    } catch (error) {
      res.status(500).json({ error: error.message });
    }
  },
 
  async getById(req, res) {
    try {
      const { id } = req.params;
      const book = await Book.findById(id);
 
      if (!book) {
        return res.status(404).json({ error: "Book not found" });
      }
 
      // Check if book is currently borrowed
      const isBorrowed = await Borrowing.checkIfBookBorrowed(id);
      book.isBorrowed = isBorrowed;
 
      res.json(book);
    } catch (error) {
      res.status(500).json({ error: error.message });
    }
  },
 
  async search(req, res) {
    try {
      const { q } = req.query;
 
      if (!q) {
        return res.status(400).json({ error: "Search query is required" });
      }
 
      const books = await Book.search(q);
      res.json(books);
    } catch (error) {
      res.status(500).json({ error: error.message });
    }
  },
 
  async create(req, res) {
    try {
      const {
        isbn,
        title,
        author,
        publisher,
        publicationYear,
        category,
        totalCopies,
        shelfLocation,
      } = req.body;
 
      if (!title || !author || !totalCopies) {
        return res
          .status(400)
          .json({ error: "Title, author, and total copies are required" });
      }
 
      // Check for duplicate ISBN
      Eif (isbn) {
        const existingBook = await Book.findByIsbn(isbn);
        Eif (existingBook) {
          return res.status(400).json({ error: "ISBN already exists" });
        }
      }
 
      const result = await Book.create(
        isbn || null,
        title,
        author,
        publisher || null,
        publicationYear || null,
        category || null,
        totalCopies,
        shelfLocation || null,
      );
 
      res.status(201).json({
        success: true,
        book_id: result.lastID,
      });
    } catch (error) {
      res.status(500).json({ error: error.message });
    }
  },
 
  async update(req, res) {
    try {
      const { id } = req.params;
      const {
        isbn,
        title,
        author,
        publisher,
        publicationYear,
        category,
        totalCopies,
        shelfLocation,
      } = req.body;
 
      const book = await Book.findById(id);
      if (!book) {
        return res.status(404).json({ error: "Book not found" });
      }
 
      if (!title || !author || !totalCopies) {
        return res
          .status(400)
          .json({ error: "Title, author, and total copies are required" });
      }
 
      await Book.update(
        id,
        isbn || null,
        title,
        author,
        publisher || null,
        publicationYear || null,
        category || null,
        totalCopies,
        shelfLocation || null,
      );
 
      res.json({ success: true });
    } catch (error) {
      res.status(500).json({ error: error.message });
    }
  },
 
  async delete(req, res) {
    try {
      const { id } = req.params;
 
      const book = await Book.findById(id);
      if (!book) {
        return res.status(404).json({ error: "Book not found" });
      }
 
      // Check if book is borrowed
      const isBorrowed = await Borrowing.checkIfBookBorrowed(id);
      if (isBorrowed) {
        return res.status(400).json({ error: "Cannot delete a borrowed book" });
      }
 
      await Book.delete(id);
      res.json({ success: true });
    } catch (error) {
      res.status(500).json({ error: error.message });
    }
  },
};
 
module.exports = BookController;