Compare commits
3 Commits
7d20609cdf
...
34403b8ec7
Author | SHA1 | Date |
---|---|---|
Robbie Antenesse | 34403b8ec7 | |
Robbie Antenesse | 554fd3cf01 | |
Robbie Antenesse | f16aa470d8 |
|
@ -1,9 +1,17 @@
|
|||
const fetch = require('node-fetch');
|
||||
|
||||
class ShelfController {
|
||||
constructor (sequelizeModels) {
|
||||
this.model = sequelizeModels.Shelf;
|
||||
this.itemModel = sequelizeModels.ShelfItem;
|
||||
constructor (sequelizeModels, language) { // Language needs to be passed with every request involving books.
|
||||
this.models = sequelizeModels;
|
||||
this.lang = language;
|
||||
}
|
||||
|
||||
static userOwnsShelf(user, shelf) {
|
||||
return typeof user !== 'undefined' && user.id === shelf.userId;
|
||||
}
|
||||
|
||||
static shelfCanBeModified(shelf) {
|
||||
return shelf.isDeletable === true;
|
||||
}
|
||||
|
||||
static newShelfNameIsValid (name, existingNames = []) {
|
||||
|
@ -32,7 +40,7 @@ class ShelfController {
|
|||
|
||||
async createDefaultShelves (user) {
|
||||
try {
|
||||
const defaultShelvesCreated = await this.model.bulkCreate([
|
||||
const defaultShelvesCreated = await this.models.Shelf.bulkCreate([
|
||||
{
|
||||
userId: user.id,
|
||||
name: 'Reading',
|
||||
|
@ -82,17 +90,9 @@ class ShelfController {
|
|||
}
|
||||
}
|
||||
|
||||
async renameShelf (userId, id, name) {
|
||||
async renameShelf (user, shelf, name) {
|
||||
try {
|
||||
return await this.model.update({
|
||||
name,
|
||||
}, {
|
||||
where: {
|
||||
id,
|
||||
userId,
|
||||
isDeletable: true, // You can only rename shelves not created by the system
|
||||
}
|
||||
});
|
||||
return await shelf.update({ name });
|
||||
} catch(error) {
|
||||
return {
|
||||
error,
|
||||
|
@ -186,10 +186,10 @@ class ShelfController {
|
|||
|
||||
return shelf;
|
||||
}
|
||||
|
||||
|
||||
async userCanViewShelf (user, shelf) {
|
||||
// This needs work when permissions are added.
|
||||
const userOwnsShelf = typeof user !== 'undefined' && user.id === shelf.userId;
|
||||
const userOwnsShelf = ShelfController.userOwnsShelf(user, shelf);
|
||||
console.log('owned?', userOwnsShelf);
|
||||
console.log('isPublic?', shelf.isPublic);
|
||||
return userOwnsShelf || shelf.isPublic;
|
||||
|
@ -248,6 +248,44 @@ class ShelfController {
|
|||
|
||||
return shelfData;
|
||||
}
|
||||
|
||||
async addShelfItem(shelf, bookReferenceId, source = null) {
|
||||
const bookReferenceController = new BookReferenceController(this.models, this.lang);
|
||||
|
||||
let bookId = bookReferenceId;
|
||||
if (source !== null) {
|
||||
const bookReference = await bookReferenceController.createOrUpdateReference(source, bookId);
|
||||
bookId = bookReference.id;
|
||||
}
|
||||
|
||||
if (shelf.ShelfItems.some(shelfItem => shelfItem.bookId === bookId)) {
|
||||
return {
|
||||
error: 'api.shelf.addItem.already_on_shelf', // This may need to change to account for editions later.
|
||||
}
|
||||
}
|
||||
|
||||
const shelfItem = await shelf.addShelfItem({ bookId }).catch(err => err);
|
||||
|
||||
if (!shelfItem) {
|
||||
return {
|
||||
error: shelfItem,
|
||||
};
|
||||
}
|
||||
|
||||
return shelfItem;
|
||||
}
|
||||
|
||||
async moveShelfItem(shelfItem, toShelf) {
|
||||
const success = await toShelf.addShelfItem(shelfItem);
|
||||
|
||||
if (!success) {
|
||||
return {
|
||||
error: shelfItem,
|
||||
};
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ShelfController;
|
|
@ -104,7 +104,7 @@ async function routes(fastify, options) {
|
|||
|
||||
fastify.post('/api/shelf/rename', async (request, reply) => {
|
||||
if (!request.isLoggedInUser) {
|
||||
return reply.code(400).send({
|
||||
return reply.code(401).send({
|
||||
error: true,
|
||||
message: 'api.not_logged_in',
|
||||
});
|
||||
|
@ -136,9 +136,25 @@ async function routes(fastify, options) {
|
|||
return reply.code(400).send(shelfNameIsValid);
|
||||
}
|
||||
|
||||
const shelf = await fastify.models.Shelf.findByPk(request.body.shelfId);
|
||||
|
||||
if (!ShelfController.userOwnsShelf(request.user, shelf)) {
|
||||
return reply.code(403).send({
|
||||
error: true,
|
||||
message: 'api.shelf.not_owner',
|
||||
});
|
||||
}
|
||||
|
||||
if (!ShelfController.shelfCanBeModified(shelf)) {
|
||||
return reply.code(403).send({
|
||||
error: true,
|
||||
message: 'api.shelf.not_editable',
|
||||
});
|
||||
}
|
||||
|
||||
const shelfController = new ShelfController(fastify.models);
|
||||
|
||||
const newShelf = shelfController.renameShelf(request.user, request.body.shelfId, request.body.shelfName);
|
||||
const newShelf = shelfController.renameShelf(request.user, shelf, request.body.shelfName);
|
||||
if (typeof newShelf.error !== 'undefined' && newShelf.error !== false) {
|
||||
newShelf.message = 'api.shelf.rename.fail';
|
||||
return reply.code(400).send(newShelf);
|
||||
|
@ -149,6 +165,113 @@ async function routes(fastify, options) {
|
|||
message: 'api.shelf.rename.success',
|
||||
});
|
||||
});
|
||||
|
||||
fastify.post('/api/shelf/addItem', async (request, reply) => {
|
||||
if (!request.isLoggedInUser) {
|
||||
return reply.code(401).send({
|
||||
error: true,
|
||||
message: 'api.not_logged_in',
|
||||
});
|
||||
}
|
||||
|
||||
if (typeof request.body.shelfId === 'undefined') {
|
||||
return reply.code(400).send({
|
||||
error: true,
|
||||
message: 'api.shelf.addItem.missing_id',
|
||||
});
|
||||
}
|
||||
|
||||
if (typeof request.body.bookId === 'undefined') {
|
||||
return reply.code(400).send({
|
||||
error: true,
|
||||
message: 'api.shelf.addItem.missing_id',
|
||||
});
|
||||
}
|
||||
|
||||
const shelf = await request.user.getShelf({
|
||||
where: { id: request.body.shelfId },
|
||||
include: [ fastify.models.ShelfItem ],
|
||||
});
|
||||
|
||||
if (!ShelfController.userOwnsShelf(request.user, shelf)) {
|
||||
return reply.code(403).send({
|
||||
error: true,
|
||||
message: 'api.shelf.not_owner',
|
||||
});
|
||||
}
|
||||
|
||||
const shelfController = new ShelfController(fastify.models, request.language);
|
||||
|
||||
const shelfItem = await shelfController.addShelfItem(shelf, request.body.bookId, request.body.source);
|
||||
|
||||
if (typeof shelfItem.error !== 'undefined') {
|
||||
return reply.code(400).send({
|
||||
error: shelfItem.error,
|
||||
message: 'api.shelf.addItem.could_not_add',
|
||||
});
|
||||
}
|
||||
|
||||
return reply.send({
|
||||
error: false,
|
||||
message: 'api.shelf.addItem.success',
|
||||
});
|
||||
});
|
||||
|
||||
fastify.post('/api/shelf/moveItem', 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.moveItem.missing_item_id',
|
||||
});
|
||||
}
|
||||
|
||||
if (typeof request.body.shelfId === 'undefined') {
|
||||
return reply.code(400).send({
|
||||
error: true,
|
||||
message: 'api.shelf.moveItem.missing_shelf_id',
|
||||
});
|
||||
}
|
||||
|
||||
const shelfItem = await fastify.models.ShelfItem.findByPk(request.body.itemId, {
|
||||
include: [ fastify.models.Shelf ],
|
||||
});
|
||||
|
||||
const toShelf = await request.user.getShelf({
|
||||
where: { id: request.body.shelfId },
|
||||
include: [ fastify.models.ShelfItem ],
|
||||
});
|
||||
|
||||
if (!ShelfController.userOwnsShelf(request.user, shelfItem.Shelf)
|
||||
|| !ShelfController.userOwnsShelf(request.user, toShelf)) {
|
||||
return reply.code(403).send({
|
||||
error: true,
|
||||
message: 'api.shelf.not_owner',
|
||||
});
|
||||
}
|
||||
|
||||
const shelfController = new ShelfController(fastify.models, request.language);
|
||||
|
||||
const shelfItem = await shelfController.moveShelfItem(shelfItem, toShelf);
|
||||
|
||||
if (typeof shelfItem.error !== 'undefined') {
|
||||
return reply.code(400).send({
|
||||
error: shelfItem.error,
|
||||
message: 'api.shelf.moveItem.could_not_move',
|
||||
});
|
||||
}
|
||||
|
||||
return reply.send({
|
||||
error: false,
|
||||
message: 'api.shelf.moveItem.success',
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = routes;
|
Loading…
Reference in New Issue