2021-08-21 20:02:24 +02:00
|
|
|
const Path = require('path')
|
2021-08-18 00:01:11 +02:00
|
|
|
class Book {
|
|
|
|
constructor(book = null) {
|
|
|
|
this.olid = null
|
|
|
|
this.title = null
|
|
|
|
this.author = null
|
2021-08-19 18:31:03 +02:00
|
|
|
this.series = null
|
2021-08-18 00:01:11 +02:00
|
|
|
this.publishYear = null
|
|
|
|
this.publisher = null
|
|
|
|
this.description = null
|
|
|
|
this.cover = null
|
2021-08-20 00:29:36 +02:00
|
|
|
this.genres = []
|
2021-08-18 00:01:11 +02:00
|
|
|
|
|
|
|
if (book) {
|
|
|
|
this.construct(book)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
construct(book) {
|
|
|
|
this.olid = book.olid
|
|
|
|
this.title = book.title
|
|
|
|
this.author = book.author
|
2021-08-19 18:31:03 +02:00
|
|
|
this.series = book.series
|
2021-08-20 02:14:24 +02:00
|
|
|
this.publishYear = book.publishYear
|
2021-08-18 00:01:11 +02:00
|
|
|
this.publisher = book.publisher
|
|
|
|
this.description = book.description
|
|
|
|
this.cover = book.cover
|
2021-08-20 00:29:36 +02:00
|
|
|
this.genres = book.genres
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
toJSON() {
|
|
|
|
return {
|
|
|
|
olid: this.olid,
|
|
|
|
title: this.title,
|
|
|
|
author: this.author,
|
2021-08-19 18:31:03 +02:00
|
|
|
series: this.series,
|
2021-08-20 02:14:24 +02:00
|
|
|
publishYear: this.publishYear,
|
2021-08-18 00:01:11 +02:00
|
|
|
publisher: this.publisher,
|
|
|
|
description: this.description,
|
|
|
|
cover: this.cover,
|
2021-08-20 00:29:36 +02:00
|
|
|
genres: this.genres
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setData(data) {
|
|
|
|
this.olid = data.olid || null
|
|
|
|
this.title = data.title || null
|
|
|
|
this.author = data.author || null
|
2021-08-19 18:31:03 +02:00
|
|
|
this.series = data.series || null
|
2021-08-20 02:14:24 +02:00
|
|
|
this.publishYear = data.publishYear || null
|
2021-08-18 00:01:11 +02:00
|
|
|
this.description = data.description || null
|
|
|
|
this.cover = data.cover || null
|
2021-08-20 00:29:36 +02:00
|
|
|
this.genres = data.genres || []
|
2021-08-21 20:02:24 +02:00
|
|
|
|
|
|
|
// Use first image file as cover
|
|
|
|
if (data.otherFiles && data.otherFiles.length) {
|
|
|
|
var imageFile = data.otherFiles.find(f => f.filetype === 'image')
|
|
|
|
if (imageFile) {
|
|
|
|
this.cover = Path.join('/local', imageFile.path)
|
|
|
|
}
|
|
|
|
}
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
update(payload) {
|
|
|
|
var hasUpdates = false
|
|
|
|
for (const key in payload) {
|
|
|
|
if (payload[key] === undefined) continue;
|
|
|
|
|
2021-08-20 00:29:36 +02:00
|
|
|
if (key === 'genres') {
|
|
|
|
if (payload['genres'] === null && this.genres !== null) {
|
|
|
|
this.genres = []
|
2021-08-18 00:01:11 +02:00
|
|
|
hasUpdates = true
|
2021-08-20 00:29:36 +02:00
|
|
|
} else if (payload['genres'].join(',') !== this.genres.join(',')) {
|
|
|
|
this.genres = payload['genres']
|
2021-08-18 00:01:11 +02:00
|
|
|
hasUpdates = true
|
|
|
|
}
|
|
|
|
} else if (this[key] !== undefined && payload[key] !== this[key]) {
|
|
|
|
this[key] = payload[key]
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = Book
|