2022-04-25 01:41:47 +02:00
|
|
|
const Logger = require('../../Logger')
|
2022-03-10 02:23:17 +01:00
|
|
|
const { getId } = require('../../utils/index')
|
2022-09-18 23:58:20 +02:00
|
|
|
const { checkNamesAreEqual } = require('../../utils/parsers/parseNameString')
|
2022-03-10 02:23:17 +01:00
|
|
|
|
2022-03-09 02:31:44 +01:00
|
|
|
class Author {
|
|
|
|
constructor(author) {
|
|
|
|
this.id = null
|
|
|
|
this.asin = null
|
|
|
|
this.name = null
|
2022-03-13 12:42:43 +01:00
|
|
|
this.description = null
|
2022-03-09 02:31:44 +01:00
|
|
|
this.imagePath = null
|
2022-03-10 02:23:17 +01:00
|
|
|
this.relImagePath = null
|
2022-03-09 02:31:44 +01:00
|
|
|
this.addedAt = null
|
|
|
|
this.updatedAt = null
|
|
|
|
|
|
|
|
if (author) {
|
|
|
|
this.construct(author)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
construct(author) {
|
|
|
|
this.id = author.id
|
|
|
|
this.asin = author.asin
|
2022-04-25 01:41:47 +02:00
|
|
|
this.name = author.name || ''
|
2022-03-13 12:42:43 +01:00
|
|
|
this.description = author.description || null
|
2022-03-09 02:31:44 +01:00
|
|
|
this.imagePath = author.imagePath
|
2022-03-10 02:23:17 +01:00
|
|
|
this.relImagePath = author.relImagePath
|
2022-03-09 02:31:44 +01:00
|
|
|
this.addedAt = author.addedAt
|
|
|
|
this.updatedAt = author.updatedAt
|
|
|
|
}
|
|
|
|
|
|
|
|
toJSON() {
|
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
asin: this.asin,
|
|
|
|
name: this.name,
|
2022-03-13 12:42:43 +01:00
|
|
|
description: this.description,
|
2022-03-09 02:31:44 +01:00
|
|
|
imagePath: this.imagePath,
|
2022-03-10 02:23:17 +01:00
|
|
|
relImagePath: this.relImagePath,
|
2022-03-09 02:31:44 +01:00
|
|
|
addedAt: this.addedAt,
|
2022-05-14 01:11:54 +02:00
|
|
|
updatedAt: this.updatedAt
|
2022-03-09 02:31:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-13 16:35:35 +01:00
|
|
|
toJSONExpanded(numBooks = 0) {
|
|
|
|
var json = this.toJSON()
|
|
|
|
json.numBooks = numBooks
|
|
|
|
return json
|
|
|
|
}
|
|
|
|
|
2022-03-09 02:31:44 +01:00
|
|
|
toJSONMinimal() {
|
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
name: this.name
|
|
|
|
}
|
|
|
|
}
|
2022-03-10 02:23:17 +01:00
|
|
|
|
|
|
|
setData(data) {
|
|
|
|
this.id = getId('aut')
|
|
|
|
this.name = data.name
|
2022-03-13 12:42:43 +01:00
|
|
|
this.description = data.description || null
|
2022-03-10 02:23:17 +01:00
|
|
|
this.asin = data.asin || null
|
|
|
|
this.imagePath = data.imagePath || null
|
|
|
|
this.relImagePath = data.relImagePath || null
|
|
|
|
this.addedAt = Date.now()
|
|
|
|
this.updatedAt = Date.now()
|
|
|
|
}
|
2022-03-13 00:45:32 +01:00
|
|
|
|
2022-03-15 00:53:49 +01:00
|
|
|
update(payload) {
|
|
|
|
var json = this.toJSON()
|
|
|
|
delete json.id
|
|
|
|
delete json.addedAt
|
|
|
|
delete json.updatedAt
|
|
|
|
var hasUpdates = false
|
|
|
|
for (const key in json) {
|
|
|
|
if (payload[key] !== undefined && json[key] != payload[key]) {
|
|
|
|
this[key] = payload[key]
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hasUpdates
|
|
|
|
}
|
|
|
|
|
2022-03-13 00:45:32 +01:00
|
|
|
checkNameEquals(name) {
|
|
|
|
if (!name) return false
|
2022-04-25 01:41:47 +02:00
|
|
|
if (this.name === null) {
|
|
|
|
Logger.error(`[Author] Author name is null (${this.id})`)
|
|
|
|
return false
|
|
|
|
}
|
2022-09-18 23:58:20 +02:00
|
|
|
return checkNamesAreEqual(this.name, name)
|
2022-03-13 00:45:32 +01:00
|
|
|
}
|
2022-03-09 02:31:44 +01:00
|
|
|
}
|
|
|
|
module.exports = Author
|