Source: controllers/EditionController.js

/** @module controllers */

const BaseDatabaseController = require("./BaseDatabaseController");

/** Class representing a edition controller. */
class EditionController extends BaseDatabaseController {
    /** Create a edition controller. */
    constructor() {
        super();
    }

    /**
     * Get an edition by id.
     * 
     * @param {number} id
     * @returns {Promise<object>}
     */
    async byId(id) {
        const editionResponse = await this._query('SELECT * FROM editions WHERE id = ?', id);
        if (editionResponse.length !== 1) {
            return null;
        }

        return editionResponse[0];
    }

    /**
     * Get an edition by ISBN.
     * 
     * @param {object} isbn
     * @returns {Promise<object>}
     */
    async byISBN(isbn) {
        const editionResponse = await this._query('SELECT * FROM editions WHERE isbn13 = ?', isbn.getISBN());
        if (editionResponse.length !== 1) {
            return null;
        }

        return editionResponse[0];
    }

    /**
     * Get editions by book.
     * 
     * @param {number} bookId
     * @returns {Promise<object[]>}
     */
    async byBook(bookId) {
        const editionsResponse = await this._query('SELECT * FROM editions WHERE book_id = ?', bookId);

        return editionsResponse;
    }

    /**
     * Insert a new edition.
     * 
     * @param {number} bookId
     * @param {string} isbn10
     * @param {string} isbn13
     * @returns {Promise<object>}
     */
    async insert(bookId, isbn10, isbn13, openlibraryId) {
        const response = await this._query('INSERT INTO editions (book_id, isbn10, isbn13, openlibrary_id) VALUES (?, ?, ?, ?)', bookId, isbn10, isbn13, openlibraryId);
        return await this.byId(response.insertId);
    }
}

module.exports = EditionController;