2022-03-12 02:46:32 +01:00
|
|
|
const Logger = require('../Logger')
|
|
|
|
|
|
|
|
class SeriesController {
|
|
|
|
constructor() { }
|
|
|
|
|
2022-03-13 01:50:31 +01:00
|
|
|
async findOne(req, res) {
|
2022-04-25 00:46:21 +02:00
|
|
|
var include = (req.query.include || '').split(',')
|
|
|
|
|
|
|
|
var seriesJson = req.series.toJSON()
|
|
|
|
|
|
|
|
// Add progress map with isFinished flag
|
|
|
|
if (include.includes('progress')) {
|
|
|
|
var libraryItemsInSeries = this.db.libraryItems.filter(li => li.mediaType === 'book' && li.media.metadata.hasSeries(seriesJson.id))
|
|
|
|
var libraryItemsFinished = libraryItemsInSeries.filter(li => {
|
|
|
|
var mediaProgress = req.user.getMediaProgress(li.id)
|
|
|
|
return mediaProgress && mediaProgress.isFinished
|
|
|
|
})
|
|
|
|
seriesJson.progress = {
|
|
|
|
libraryItemIds: libraryItemsInSeries.map(li => li.id),
|
|
|
|
libraryItemIdsFinished: libraryItemsFinished.map(li => li.id),
|
|
|
|
isFinished: libraryItemsFinished.length === libraryItemsInSeries.length
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.json(seriesJson)
|
2022-03-13 01:50:31 +01:00
|
|
|
}
|
|
|
|
|
2022-03-12 02:46:32 +01:00
|
|
|
async search(req, res) {
|
|
|
|
var q = (req.query.q || '').toLowerCase()
|
|
|
|
if (!q) return res.json([])
|
|
|
|
var limit = (req.query.limit && !isNaN(req.query.limit)) ? Number(req.query.limit) : 25
|
|
|
|
var series = this.db.series.filter(se => se.name.toLowerCase().includes(q))
|
|
|
|
series = series.slice(0, limit)
|
|
|
|
res.json(series)
|
|
|
|
}
|
2022-03-13 01:50:31 +01:00
|
|
|
|
|
|
|
middleware(req, res, next) {
|
|
|
|
var series = this.db.series.find(se => se.id === req.params.id)
|
|
|
|
if (!series) return res.sendStatus(404)
|
|
|
|
|
|
|
|
if (req.method == 'DELETE' && !req.user.canDelete) {
|
|
|
|
Logger.warn(`[SeriesController] User attempted to delete without permission`, req.user)
|
|
|
|
return res.sendStatus(403)
|
|
|
|
} else if ((req.method == 'PATCH' || req.method == 'POST') && !req.user.canUpdate) {
|
|
|
|
Logger.warn('[SeriesController] User attempted to update without permission', req.user)
|
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.series = series
|
|
|
|
next()
|
|
|
|
}
|
2022-03-12 02:46:32 +01:00
|
|
|
}
|
|
|
|
module.exports = new SeriesController()
|