All files / models Book.js

50% Statements 8/16
100% Branches 0/0
36.36% Functions 4/11
53.33% Lines 8/15

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  2x   2x   2x           1x       2x       1x 1x                                                                                                                                                                       2x  
// Book model
const { getOne, getAll, run } = require("./query");
 
const Book = {
  async findById(bookId) {
    return getOne("SELECT book_id as id, * FROM books WHERE book_id = ?", [
      bookId,
    ]);
  },
 
  async findByIsbn(isbn) {
    return getOne("SELECT * FROM books WHERE isbn = ?", [isbn]);
  },
 
  async getAll() {
    return getAll("SELECT book_id as id, * FROM books ORDER BY title");
  },
 
  async search(query) {
    const searchTerm = `%${query}%`;
    return getAll(
      "SELECT book_id as id, * FROM books WHERE title LIKE ? OR author LIKE ? OR isbn LIKE ? ORDER BY title",
      [searchTerm, searchTerm, searchTerm],
    );
  },
 
  async create(
    isbn,
    title,
    author,
    publisher,
    publicationYear,
    category,
    totalCopies,
    shelfLocation,
  ) {
    return run(
      "INSERT INTO books (isbn, title, author, publisher, publication_year, category, total_copies, available_copies, shelf_location) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
      [
        isbn,
        title,
        author,
        publisher,
        publicationYear,
        category,
        totalCopies,
        totalCopies,
        shelfLocation,
      ],
    );
  },
 
  async update(
    bookId,
    isbn,
    title,
    author,
    publisher,
    publicationYear,
    category,
    totalCopies,
    shelfLocation,
  ) {
    return run(
      "UPDATE books SET isbn = ?, title = ?, author = ?, publisher = ?, publication_year = ?, category = ?, total_copies = ?, shelf_location = ? WHERE book_id = ?",
      [
        isbn,
        title,
        author,
        publisher,
        publicationYear,
        category,
        totalCopies,
        shelfLocation,
        bookId,
      ],
    );
  },
 
  async updateAvailableCopies(bookId, availableCopies) {
    return run("UPDATE books SET available_copies = ? WHERE book_id = ?", [
      availableCopies,
      bookId,
    ]);
  },
 
  async delete(bookId) {
    return run("DELETE FROM books WHERE book_id = ?", [bookId]);
  },
 
  async getAvailableBooks() {
    return getAll(
      "SELECT book_id as id, * FROM books WHERE available_copies > 0 ORDER BY title",
    );
  },
 
  async getCategories() {
    const result = await getAll(
      "SELECT DISTINCT category FROM books WHERE category IS NOT NULL ORDER BY category",
    );
    return result.map((r) => r.category);
  },
};
 
module.exports = Book;