const bookRepository = require('../repositories/bookRepository');

class BookService {
    async getAllBooks() {
        return await bookRepository.findAll();
    }

    async searchBooks(query) {
    console.log("1. Service qatlamiga so'rov keldi, qidiruv:", query);
    const results = await bookRepository.search(query);
    console.log("2. Repository'dan ma'lumotlar olindi, soni:", results.length);
    return results;
    }

    async addBook(bookData) {
     
        if (bookData.price < 0) {
            throw new Error("Kitob narxi manfiy bo'lishi mumkin emas!");
        }
        return await bookRepository.create(bookData);
    }
}

module.exports = new BookService();