{"size": 1233, "ext": "js", "max_stars_repo_path": "controllers/googleController.js", "max_stars_repo_name": "uyennguyen30696/react-google-books", "max_stars_repo_stars_event_min_datetime": "2021-08-09T13:01:33.000Z", "max_stars_repo_stars_event_max_datetime": "2021-08-09T13:01:33.000Z", "max_issues_repo_path": "controllers/googleController.js", "max_issues_repo_name": "uyennguyen30696/react-google-books", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "controllers/googleController.js", "max_forks_repo_name": "uyennguyen30696/react-google-books", "max_forks_repo_forks_event_min_datetime": "2021-08-09T13:01:41.000Z", "max_forks_repo_forks_event_max_datetime": "2021-08-09T13:01:41.000Z"}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 37.3636363636, "max_line_length": 110, "alphanum_fraction": 0.4476885645}
const axios = require("axios");
const db = require("../models");

module.exports = {
    findAll: function (req, res) {
        const { query: params } = req;
        axios.get(`https://www.googleapis.com/books/v1/volumes?q=${params.q}&maxResults=20`)
            .then(results =>
                results.data.items.filter(
                    result =>
                        result.id &&
                        result.volumeInfo.title &&
                        result.volumeInfo.infoLink &&
                        result.volumeInfo.authors &&
                        result.volumeInfo.description &&
                        result.volumeInfo.imageLinks.thumbnail
                )
            )
            // Look in the database, if a book is already saved, do not display it in the results from the api
            .then(apiBooks => 
                db.Book.find().then(dbBooks => 
                    apiBooks.filter(apiBook => 
                        dbBooks.every(dbBook => 
                            dbBook.googleId !== apiBook.id
                        )
                    )
                )
            )
            .then(books => res.json(books))
            .catch(err => res.status(422).json(err));
    }
}
