diff --git a/server/controllers/shelf.js b/server/controllers/shelf.js index df28b30..0d816cf 100644 --- a/server/controllers/shelf.js +++ b/server/controllers/shelf.js @@ -286,6 +286,25 @@ class ShelfController { return success; } + + async deleteShelfItem(shelfItem) { + // Only fully remove if no statuses are associated + const statuses = await shelfItem.getStatuses(); + const options = {}; + if (statuses.length < 1) { + options.force = true; + } + + const success = await shelfItem.destroy(options); + + if (!success) { + return { + error: shelfItem, + }; + } + + return success; + } } module.exports = ShelfController; \ No newline at end of file diff --git a/server/routes/shelf.js b/server/routes/shelf.js index 5284f79..53f9ddb 100644 --- a/server/routes/shelf.js +++ b/server/routes/shelf.js @@ -258,11 +258,11 @@ async function routes(fastify, options) { const shelfController = new ShelfController(fastify.models, request.language); - const shelfItem = await shelfController.moveShelfItem(shelfItem, toShelf); + const moveSuccess = await shelfController.moveShelfItem(shelfItem, toShelf); - if (typeof shelfItem.error !== 'undefined') { + if (typeof moveSuccess.error !== 'undefined') { return reply.code(400).send({ - error: shelfItem.error, + error: moveSuccess.error, message: 'api.shelf.moveItem.could_not_move', }); } @@ -272,6 +272,49 @@ async function routes(fastify, options) { message: 'api.shelf.moveItem.success', }); }); + + fastify.post('/api/shelf/deleteItem', async (request, reply) => { + if (!request.isLoggedInUser) { + return reply.code(401).send({ + error: true, + message: 'api.not_logged_in', + }); + } + + if (typeof request.body.itemId === 'undefined') { + return reply.code(400).send({ + error: true, + message: 'api.shelf.deleteItem.missing_item_id', + }); + } + + const shelfItem = await fastify.models.ShelfItem.findByPk(request.body.itemId, { + include: [ fastify.models.Shelf ], + }); + + if (!ShelfController.userOwnsShelf(request.user, shelfItem.Shelf)) { + return reply.code(403).send({ + error: true, + message: 'api.shelf.not_owner', + }); + } + + const shelfController = new ShelfController(fastify.models, request.language); + + const deleteSuccess = await shelfController.deleteShelfItem(shelfItem); + + if (typeof deleteSuccess.error !== 'undefined') { + return reply.code(400).send({ + error: deleteSuccess.error, + message: 'api.shelf.deleteItem.could_not_delete', + }); + } + + return reply.send({ + error: false, + message: 'api.shelf.deleteItem.success', + }); + }); } module.exports = routes; \ No newline at end of file diff --git a/server/sequelize/models/ShelfItem.js b/server/sequelize/models/ShelfItem.js index ba9e13a..f6c721c 100644 --- a/server/sequelize/models/ShelfItem.js +++ b/server/sequelize/models/ShelfItem.js @@ -51,4 +51,5 @@ module.exports = sequelize => sequelize.define('ShelfItem', { fields: ['shelfId'], }, ], + paranoid: true, // Keep shelfItems in database just in case there are Statuses associated }); \ No newline at end of file