2023-07-04 18:14:44 -05:00
|
|
|
const uuidv4 = require("uuid").v4
|
2023-08-18 17:08:34 -05:00
|
|
|
const { getTitleIgnorePrefix, getTitlePrefixAtEnd } = require('../../utils/index')
|
2022-03-09 19:23:17 -06:00
|
|
|
|
2024-02-25 18:32:04 +00:00
|
|
|
/**
|
|
|
|
|
* @openapi
|
|
|
|
|
* components:
|
|
|
|
|
* schemas:
|
|
|
|
|
* series:
|
|
|
|
|
* type: object
|
|
|
|
|
* properties:
|
|
|
|
|
* id:
|
|
|
|
|
* description: The ID of the series.
|
|
|
|
|
* type: string
|
|
|
|
|
* example: ser_cabkj4jeu8be3rap4g
|
|
|
|
|
* name:
|
|
|
|
|
* description: The name of the series.
|
|
|
|
|
* type: string
|
|
|
|
|
* example: Sword of Truth
|
|
|
|
|
* description:
|
|
|
|
|
* description: A description for the series. Will be null if there is none.
|
|
|
|
|
* type: [string, 'null']
|
|
|
|
|
* addedAt:
|
|
|
|
|
* description: The time (in ms since POSIX epoch) when the series was added.
|
|
|
|
|
* type: integer
|
|
|
|
|
* example: 1650621073750
|
|
|
|
|
* updatedAt:
|
|
|
|
|
* description: The time (in ms since POSIX epoch) when the series was last updated.
|
|
|
|
|
* type: integer
|
|
|
|
|
* example: 1650621073750
|
|
|
|
|
* seriesNumBooks:
|
|
|
|
|
* type: object
|
|
|
|
|
* properties:
|
|
|
|
|
* id:
|
|
|
|
|
* description: The ID of the series.
|
|
|
|
|
* type: string
|
|
|
|
|
* example: ser_cabkj4jeu8be3rap4g
|
|
|
|
|
* name:
|
|
|
|
|
* description: The name of the series.
|
|
|
|
|
* type: string
|
|
|
|
|
* example: Sword of Truth
|
|
|
|
|
* nameIgnorePrefix:
|
|
|
|
|
* description: The name of the series with any prefix moved to the end.
|
|
|
|
|
* type: string
|
|
|
|
|
* example: Sword of Truth
|
|
|
|
|
* libraryItemIds:
|
|
|
|
|
* description: The IDs of the library items in the series.
|
|
|
|
|
* type: array
|
|
|
|
|
* items:
|
|
|
|
|
* type: string
|
|
|
|
|
* example: li_8gch9ve09orgn4fdz8
|
|
|
|
|
* numBooks:
|
|
|
|
|
* description: The number of books in the series.
|
|
|
|
|
* type: integer
|
|
|
|
|
* example: 1
|
|
|
|
|
* seriesBooks:
|
|
|
|
|
* type: object
|
|
|
|
|
* properties:
|
|
|
|
|
* id:
|
|
|
|
|
* description: The ID of the series.
|
|
|
|
|
* type: string
|
|
|
|
|
* example: ser_cabkj4jeu8be3rap4g
|
|
|
|
|
* name:
|
|
|
|
|
* description: The name of the series.
|
|
|
|
|
* type: string
|
|
|
|
|
* example: Sword of Truth
|
|
|
|
|
* nameIgnorePrefix:
|
|
|
|
|
* description: The name of the series with any prefix moved to the end.
|
|
|
|
|
* type: string
|
|
|
|
|
* example: Sword of Truth
|
|
|
|
|
* nameIgnorePrefixSort:
|
|
|
|
|
* description: The name of the series with any prefix removed.
|
|
|
|
|
* type: string
|
|
|
|
|
* example: Sword of Truth
|
|
|
|
|
* type:
|
|
|
|
|
* description: Will always be series.
|
|
|
|
|
* type: string
|
|
|
|
|
* example: series
|
|
|
|
|
* books:
|
|
|
|
|
* description: The library items that contain the books in the series. A sequence attribute that denotes the position in the series the book is in, is tacked on.
|
|
|
|
|
* type: array
|
|
|
|
|
* items:
|
|
|
|
|
* $ref: '#/components/schemas/libraryItem'
|
|
|
|
|
* addedAt:
|
|
|
|
|
* description: The time (in ms since POSIX epoch) when the series was added.
|
|
|
|
|
* type: integer
|
|
|
|
|
* example: 1650621073750
|
|
|
|
|
* totalDuration:
|
|
|
|
|
* description: The combined duration (in seconds) of all books in the series.
|
|
|
|
|
* type: number
|
|
|
|
|
* example: 12000.946
|
|
|
|
|
* seriesSequence:
|
|
|
|
|
* type: object
|
|
|
|
|
* properties:
|
|
|
|
|
* id:
|
|
|
|
|
* description: The ID of the series.
|
|
|
|
|
* type: string
|
|
|
|
|
* example: ser_cabkj4jeu8be3rap4g
|
|
|
|
|
* name:
|
|
|
|
|
* description: The name of the series.
|
|
|
|
|
* type: string
|
|
|
|
|
* example: Sword of Truth
|
|
|
|
|
* sequence:
|
|
|
|
|
* description: The position in the series the book is.
|
|
|
|
|
* type: string
|
|
|
|
|
* example: '1'
|
|
|
|
|
*/
|
2022-03-08 19:31:44 -06:00
|
|
|
class Series {
|
|
|
|
|
constructor(series) {
|
|
|
|
|
this.id = null
|
|
|
|
|
this.name = null
|
2022-03-13 06:42:43 -05:00
|
|
|
this.description = null
|
2022-03-08 19:31:44 -06:00
|
|
|
this.addedAt = null
|
|
|
|
|
this.updatedAt = null
|
2023-07-08 09:57:32 -05:00
|
|
|
this.libraryId = null
|
2022-03-08 19:31:44 -06:00
|
|
|
|
|
|
|
|
if (series) {
|
|
|
|
|
this.construct(series)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
construct(series) {
|
|
|
|
|
this.id = series.id
|
|
|
|
|
this.name = series.name
|
2022-03-13 06:42:43 -05:00
|
|
|
this.description = series.description || null
|
2022-03-08 19:31:44 -06:00
|
|
|
this.addedAt = series.addedAt
|
|
|
|
|
this.updatedAt = series.updatedAt
|
2023-07-08 09:57:32 -05:00
|
|
|
this.libraryId = series.libraryId
|
2022-03-08 19:31:44 -06:00
|
|
|
}
|
|
|
|
|
|
2023-07-28 18:03:31 -05:00
|
|
|
get nameIgnorePrefix() {
|
|
|
|
|
if (!this.name) return ''
|
|
|
|
|
return getTitleIgnorePrefix(this.name)
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-08 19:31:44 -06:00
|
|
|
toJSON() {
|
|
|
|
|
return {
|
|
|
|
|
id: this.id,
|
|
|
|
|
name: this.name,
|
2023-08-18 17:08:34 -05:00
|
|
|
nameIgnorePrefix: getTitlePrefixAtEnd(this.name),
|
2022-03-13 06:42:43 -05:00
|
|
|
description: this.description,
|
2022-03-08 19:31:44 -06:00
|
|
|
addedAt: this.addedAt,
|
2023-07-08 09:57:32 -05:00
|
|
|
updatedAt: this.updatedAt,
|
|
|
|
|
libraryId: this.libraryId
|
2022-03-08 19:31:44 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-09 19:23:17 -06:00
|
|
|
toJSONMinimal(sequence) {
|
2022-03-08 19:31:44 -06:00
|
|
|
return {
|
|
|
|
|
id: this.id,
|
|
|
|
|
name: this.name,
|
2022-03-09 19:23:17 -06:00
|
|
|
sequence
|
2022-03-08 19:31:44 -06:00
|
|
|
}
|
|
|
|
|
}
|
2022-03-09 19:23:17 -06:00
|
|
|
|
2023-07-08 09:57:32 -05:00
|
|
|
setData(data, libraryId) {
|
2023-07-04 18:14:44 -05:00
|
|
|
this.id = uuidv4()
|
2022-03-09 19:23:17 -06:00
|
|
|
this.name = data.name
|
2022-03-13 06:42:43 -05:00
|
|
|
this.description = data.description || null
|
2022-03-09 19:23:17 -06:00
|
|
|
this.addedAt = Date.now()
|
|
|
|
|
this.updatedAt = Date.now()
|
2023-07-08 09:57:32 -05:00
|
|
|
this.libraryId = libraryId
|
2022-03-09 19:23:17 -06:00
|
|
|
}
|
2022-03-12 17:45:32 -06:00
|
|
|
|
2022-09-27 17:48:45 -05:00
|
|
|
update(series) {
|
|
|
|
|
if (!series) return false
|
2022-09-28 17:12:27 -05:00
|
|
|
const keysToUpdate = ['name', 'description']
|
2022-12-26 16:08:53 -06:00
|
|
|
let hasUpdated = false
|
2022-09-27 17:48:45 -05:00
|
|
|
for (const key of keysToUpdate) {
|
|
|
|
|
if (series[key] !== undefined && series[key] !== this[key]) {
|
|
|
|
|
this[key] = series[key]
|
|
|
|
|
hasUpdated = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return hasUpdated
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-12 17:45:32 -06:00
|
|
|
checkNameEquals(name) {
|
2022-05-23 03:56:51 +01:00
|
|
|
if (!name || !this.name) return false
|
2022-03-12 17:45:32 -06:00
|
|
|
return this.name.toLowerCase() == name.toLowerCase().trim()
|
|
|
|
|
}
|
2022-03-08 19:31:44 -06:00
|
|
|
}
|
|
|
|
|
module.exports = Series
|