/** @module classes */
const { BookFactory } = require('./Book.js');
const SpineImageController = require('../../controllers/SpineImageController.js');
const BaseFactory = require('../Base.js');
const EditionController = require('../../controllers/EditionController.js');
/** Class representing an edition. */
class Edition {
constructor(id, bookId, isbn10, isbn13, book = null) {
this.id = id;
this.bookId = bookId;
this.isbn10 = isbn10;
this.isbn13 = isbn13;
this.book = book;
}
/** Get the book of the edition. */
async getBook(forceRefresh = false) {
if (this.book && !forceRefresh) {
return this.book;
}
this.book = await new BookFactory().setId(this.bookId).create();
return this.book;
}
/** Refreshes data from the database that has been pulled at least once before in this instance. */
async refresh() {
if (this.book) {
await this.getBook(true);
}
}
}
/** Class representing an edition factory. */
class EditionFactory extends BaseFactory {
/** Set the id of the edition. */
setId(id) {
this.id = id;
return this;
}
/** Set the book id of the edition. */
setBookId(bookId) {
this.bookId = bookId;
return this;
}
/** Set the ISBN-10 of the edition. */
setIsbn10(isbn10) {
this.isbn10 = isbn10;
return this;
}
/** Set the ISBN-13 of the edition. */
setIsbn13(isbn13) {
this.isbn13 = isbn13;
return this;
}
/** Set the book of the edition so it doesn't have to be fetched again. */
setBook(book) {
this.book = book;
return this;
}
/** Loads data from a database object */
load(dbObject) {
this.setId(dbObject.id);
this.setBookId(dbObject.book_id);
this.setIsbn10(dbObject.isbn10);
this.setIsbn13(dbObject.isbn13);
return this;
}
/** Creates an edition */
async create() {
if (!this.id) {
throw new Error('Missing required fields for Edition');
} else if (!this.bookId && !this.isbn10 && !this.isbn13) {
const editionResponse = await new EditionController().byId(this.id);
this.load(editionResponse);
}
return new Edition(this.id, this.bookId, this.isbn10, this.isbn13, this.book);;
}
}
module.exports = { EditionFactory, Edition };