Add deleteShelfItem endpoint

This commit is contained in:
Robbie Antenesse 2020-02-24 09:46:18 -07:00
parent 34403b8ec7
commit e0a8c21051
3 changed files with 66 additions and 3 deletions

View File

@ -286,6 +286,25 @@ class ShelfController {
return success; 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; module.exports = ShelfController;

View File

@ -258,11 +258,11 @@ async function routes(fastify, options) {
const shelfController = new ShelfController(fastify.models, request.language); 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({ return reply.code(400).send({
error: shelfItem.error, error: moveSuccess.error,
message: 'api.shelf.moveItem.could_not_move', message: 'api.shelf.moveItem.could_not_move',
}); });
} }
@ -272,6 +272,49 @@ async function routes(fastify, options) {
message: 'api.shelf.moveItem.success', 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; module.exports = routes;

View File

@ -51,4 +51,5 @@ module.exports = sequelize => sequelize.define('ShelfItem', {
fields: ['shelfId'], fields: ['shelfId'],
}, },
], ],
paranoid: true, // Keep shelfItems in database just in case there are Statuses associated
}); });