2021-11-22 03:00:40 +01:00
|
|
|
const Logger = require('../Logger')
|
2022-11-12 00:13:10 +01:00
|
|
|
const Collection = require('../objects/Collection')
|
2021-11-22 03:00:40 +01:00
|
|
|
|
|
|
|
class CollectionController {
|
|
|
|
constructor() { }
|
|
|
|
|
|
|
|
async create(req, res) {
|
2022-11-12 00:13:10 +01:00
|
|
|
var newCollection = new Collection()
|
2021-11-22 03:00:40 +01:00
|
|
|
req.body.userId = req.user.id
|
|
|
|
var success = newCollection.setData(req.body)
|
|
|
|
if (!success) {
|
|
|
|
return res.status(500).send('Invalid collection data')
|
|
|
|
}
|
2022-03-13 01:50:31 +01:00
|
|
|
var jsonExpanded = newCollection.toJSONExpanded(this.db.libraryItems)
|
2021-11-22 03:00:40 +01:00
|
|
|
await this.db.insertEntity('collection', newCollection)
|
2021-11-27 23:01:53 +01:00
|
|
|
this.emitter('collection_added', jsonExpanded)
|
2021-11-22 03:00:40 +01:00
|
|
|
res.json(jsonExpanded)
|
|
|
|
}
|
|
|
|
|
|
|
|
findAll(req, res) {
|
|
|
|
var collections = this.db.collections.filter(c => c.userId === req.user.id)
|
2022-03-13 01:50:31 +01:00
|
|
|
var expandedCollections = collections.map(c => c.toJSONExpanded(this.db.libraryItems))
|
2021-11-22 03:00:40 +01:00
|
|
|
res.json(expandedCollections)
|
|
|
|
}
|
|
|
|
|
|
|
|
findOne(req, res) {
|
2022-08-31 22:46:10 +02:00
|
|
|
res.json(req.collection.toJSONExpanded(this.db.libraryItems))
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async update(req, res) {
|
2022-08-31 22:46:10 +02:00
|
|
|
const collection = req.collection
|
2021-11-22 03:00:40 +01:00
|
|
|
var wasUpdated = collection.update(req.body)
|
2022-03-13 01:50:31 +01:00
|
|
|
var jsonExpanded = collection.toJSONExpanded(this.db.libraryItems)
|
2021-11-22 03:00:40 +01:00
|
|
|
if (wasUpdated) {
|
|
|
|
await this.db.updateEntity('collection', collection)
|
2021-11-27 23:01:53 +01:00
|
|
|
this.emitter('collection_updated', jsonExpanded)
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
|
|
|
res.json(jsonExpanded)
|
|
|
|
}
|
|
|
|
|
|
|
|
async delete(req, res) {
|
2022-08-31 22:46:10 +02:00
|
|
|
const collection = req.collection
|
2022-03-13 01:50:31 +01:00
|
|
|
var jsonExpanded = collection.toJSONExpanded(this.db.libraryItems)
|
2021-11-22 03:00:40 +01:00
|
|
|
await this.db.removeEntity('collection', collection.id)
|
2021-11-27 23:01:53 +01:00
|
|
|
this.emitter('collection_removed', jsonExpanded)
|
2021-11-22 03:00:40 +01:00
|
|
|
res.sendStatus(200)
|
|
|
|
}
|
|
|
|
|
|
|
|
async addBook(req, res) {
|
2022-08-31 22:46:10 +02:00
|
|
|
const collection = req.collection
|
2022-03-13 01:50:31 +01:00
|
|
|
var libraryItem = this.db.libraryItems.find(li => li.id === req.body.id)
|
|
|
|
if (!libraryItem) {
|
2021-11-22 03:00:40 +01:00
|
|
|
return res.status(500).send('Book not found')
|
|
|
|
}
|
2022-03-13 01:50:31 +01:00
|
|
|
if (libraryItem.libraryId !== collection.libraryId) {
|
2021-11-22 03:00:40 +01:00
|
|
|
return res.status(500).send('Book in different library')
|
|
|
|
}
|
|
|
|
if (collection.books.includes(req.body.id)) {
|
|
|
|
return res.status(500).send('Book already in collection')
|
|
|
|
}
|
|
|
|
collection.addBook(req.body.id)
|
2022-03-13 01:50:31 +01:00
|
|
|
var jsonExpanded = collection.toJSONExpanded(this.db.libraryItems)
|
2021-11-22 03:00:40 +01:00
|
|
|
await this.db.updateEntity('collection', collection)
|
2021-11-27 23:01:53 +01:00
|
|
|
this.emitter('collection_updated', jsonExpanded)
|
2021-11-22 03:00:40 +01:00
|
|
|
res.json(jsonExpanded)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DELETE: api/collections/:id/book/:bookId
|
|
|
|
async removeBook(req, res) {
|
2022-08-31 22:46:10 +02:00
|
|
|
const collection = req.collection
|
2021-11-22 03:00:40 +01:00
|
|
|
if (collection.books.includes(req.params.bookId)) {
|
|
|
|
collection.removeBook(req.params.bookId)
|
2022-03-13 01:50:31 +01:00
|
|
|
var jsonExpanded = collection.toJSONExpanded(this.db.libraryItems)
|
2021-11-22 03:00:40 +01:00
|
|
|
await this.db.updateEntity('collection', collection)
|
2021-11-27 23:01:53 +01:00
|
|
|
this.emitter('collection_updated', jsonExpanded)
|
|
|
|
}
|
2022-03-13 01:50:31 +01:00
|
|
|
res.json(collection.toJSONExpanded(this.db.libraryItems))
|
2021-11-27 23:01:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// POST: api/collections/:id/batch/add
|
|
|
|
async addBatch(req, res) {
|
2022-08-31 22:46:10 +02:00
|
|
|
const collection = req.collection
|
2021-11-27 23:01:53 +01:00
|
|
|
if (!req.body.books || !req.body.books.length) {
|
|
|
|
return res.status(500).send('Invalid request body')
|
|
|
|
}
|
|
|
|
var bookIdsToAdd = req.body.books
|
|
|
|
var hasUpdated = false
|
|
|
|
for (let i = 0; i < bookIdsToAdd.length; i++) {
|
|
|
|
if (!collection.books.includes(bookIdsToAdd[i])) {
|
|
|
|
collection.addBook(bookIdsToAdd[i])
|
|
|
|
hasUpdated = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (hasUpdated) {
|
|
|
|
await this.db.updateEntity('collection', collection)
|
2022-03-13 01:50:31 +01:00
|
|
|
this.emitter('collection_updated', collection.toJSONExpanded(this.db.libraryItems))
|
2021-11-27 23:01:53 +01:00
|
|
|
}
|
2022-03-13 01:50:31 +01:00
|
|
|
res.json(collection.toJSONExpanded(this.db.libraryItems))
|
2021-11-27 23:01:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// POST: api/collections/:id/batch/remove
|
|
|
|
async removeBatch(req, res) {
|
2022-08-31 22:46:10 +02:00
|
|
|
const collection = req.collection
|
2021-11-27 23:01:53 +01:00
|
|
|
if (!req.body.books || !req.body.books.length) {
|
|
|
|
return res.status(500).send('Invalid request body')
|
|
|
|
}
|
|
|
|
var bookIdsToRemove = req.body.books
|
|
|
|
var hasUpdated = false
|
|
|
|
for (let i = 0; i < bookIdsToRemove.length; i++) {
|
|
|
|
if (collection.books.includes(bookIdsToRemove[i])) {
|
|
|
|
collection.removeBook(bookIdsToRemove[i])
|
|
|
|
hasUpdated = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (hasUpdated) {
|
|
|
|
await this.db.updateEntity('collection', collection)
|
2022-03-13 01:50:31 +01:00
|
|
|
this.emitter('collection_updated', collection.toJSONExpanded(this.db.libraryItems))
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
2022-03-13 01:50:31 +01:00
|
|
|
res.json(collection.toJSONExpanded(this.db.libraryItems))
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
2022-08-31 22:46:10 +02:00
|
|
|
|
|
|
|
middleware(req, res, next) {
|
|
|
|
if (req.params.id) {
|
|
|
|
var collection = this.db.collections.find(c => c.id === req.params.id)
|
|
|
|
if (!collection) {
|
|
|
|
return res.status(404).send('Collection not found')
|
|
|
|
}
|
|
|
|
req.collection = collection
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.method == 'DELETE' && !req.user.canDelete) {
|
|
|
|
Logger.warn(`[CollectionController] User attempted to delete without permission`, req.user.username)
|
|
|
|
return res.sendStatus(403)
|
|
|
|
} else if ((req.method == 'PATCH' || req.method == 'POST') && !req.user.canUpdate) {
|
|
|
|
Logger.warn('[CollectionController] User attempted to update without permission', req.user.username)
|
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
|
|
|
|
|
|
|
next()
|
|
|
|
}
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
|
|
|
module.exports = new CollectionController()
|