All files / src/repository bookRepository.js

100% Statements 24/24
100% Branches 8/8
100% Functions 12/12
100% Lines 20/20

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 525x     17x       3x   3x       1x 1x       6x       2x 6x   2x       2x 2x 1x 1x     2x       3x   2x 7x 1x             5x  
const uuid = require("uuid")
class BookRepository {
  constructor(data) {
    this.data = data
  }  
 
  async find(value, field) {
    if(this.data.length === 0) return {}
    
    return await this.data.find((book) => book[field] === value )
  }
 
  async save(bookData) {
    this.data.push(bookData)
    return bookData
  }
 
  async filter(value, field) {
    return await this.data.filter((book) => book[field] === value )
  }
 
  async findMany() {
    const data = await this.data.map(book => {
      return book
    })
    return data;
  }
 
  async update(id, newData) {
    await this.data.find((book, idx) => {
      if (book.id === id) {
        this.data[idx] = { id, ...newData }
        return this.data[idx]
      }
    });
    return { id, ...newData }
  }
 
  delete(id) {
    if(this.data.length === 0) return
 
    this.data.find((book, idx) => {
      if (book?.id === id) {
        this.data.splice(idx, 1)
      }
    });
  }
 
}
 
module.exports = BookRepository