2019-12-01 19:03:55 +01:00
|
|
|
class ShelfController {
|
2019-12-01 02:02:24 +01:00
|
|
|
constructor (shelfModel, shelfItemModel) {
|
|
|
|
this.model = shelfModel;
|
|
|
|
this.itemModel = shelfItemModel;
|
|
|
|
}
|
|
|
|
|
2019-12-01 23:31:34 +01:00
|
|
|
static newShelfNameIsValid (name, existingNames = []) {
|
|
|
|
if (name.length < 1) {
|
|
|
|
return {
|
|
|
|
error: true,
|
|
|
|
message: 'api.shelf.create.name_too_short',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (existingNames.includes(name)) {
|
|
|
|
return {
|
|
|
|
error: true,
|
|
|
|
message: 'api.shelf.create.name_already_exists',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-12-01 02:02:24 +01:00
|
|
|
async createDefaultShelves (user) {
|
|
|
|
try {
|
|
|
|
const defaultShelvesCreated = await this.model.bulkCreate([
|
|
|
|
{
|
|
|
|
userId: user.id,
|
|
|
|
name: 'Reading',
|
|
|
|
isDeletable: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
userId: user.id,
|
|
|
|
name: 'Want to Read',
|
|
|
|
isDeletable: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
userId: user.id,
|
|
|
|
name: 'Finished',
|
|
|
|
isDeletable: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
userId: user.id,
|
|
|
|
name: 'Did Not Finish',
|
|
|
|
isDeletable: false,
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (defaultShelvesCreated.some(result => !result)) {
|
|
|
|
return {
|
|
|
|
error: true,
|
|
|
|
shelfResults: defaultShelvesCreated,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return defaultShelvesCreated;
|
|
|
|
} catch (error) {
|
|
|
|
return {
|
|
|
|
error,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-01 23:31:34 +01:00
|
|
|
|
|
|
|
async createShelf (user, name) {
|
|
|
|
try {
|
|
|
|
return await user.addShelf({
|
|
|
|
name,
|
|
|
|
});
|
|
|
|
} catch(error) {
|
|
|
|
return {
|
|
|
|
error,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-11 21:15:55 +01:00
|
|
|
|
|
|
|
async renameShelf (userId, id, name) {
|
|
|
|
try {
|
|
|
|
return await this.model.update({
|
|
|
|
name,
|
|
|
|
}, {
|
|
|
|
where: {
|
|
|
|
id,
|
|
|
|
userId,
|
|
|
|
isDeletable: true, // You can only rename shelves not created by the system
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch(error) {
|
|
|
|
return {
|
|
|
|
error,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-01 02:02:24 +01:00
|
|
|
|
|
|
|
async getLastUpdatedTimestamp (shelf) {
|
2019-12-24 00:51:26 +01:00
|
|
|
const lastEditedItem = await shelf.getShelfItems({
|
2019-12-01 02:02:24 +01:00
|
|
|
attributes: ['updatedAt'],
|
|
|
|
order: [
|
|
|
|
[
|
|
|
|
'updatedAt',
|
|
|
|
'DESC'
|
|
|
|
],
|
|
|
|
],
|
2019-12-24 00:51:26 +01:00
|
|
|
limit: 1,
|
2019-12-01 02:02:24 +01:00
|
|
|
});
|
|
|
|
|
2019-12-24 00:51:26 +01:00
|
|
|
if (lastEditedItem.length > 0 && (lastEditedItem[0].updatedAt > shelf.updatedAt)) {
|
2019-12-01 02:02:24 +01:00
|
|
|
return lastEditedItem.updatedAt;
|
|
|
|
}
|
|
|
|
return shelf.updatedAt;
|
|
|
|
}
|
2020-01-05 03:34:26 +01:00
|
|
|
|
|
|
|
async getShelfById(shelfId) {
|
2020-01-08 06:50:58 +01:00
|
|
|
if (isNaN(parseInt(shelfId))) {
|
2020-01-05 03:34:26 +01:00
|
|
|
return {
|
|
|
|
error: 'Shelf ID Provided is not a number.',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-01-08 06:50:58 +01:00
|
|
|
return this.getFakeShelf();
|
|
|
|
|
|
|
|
const shelf = await this.shelfModel.findByPk(shelfId);
|
2020-01-05 03:34:26 +01:00
|
|
|
if (shelf === null) {
|
|
|
|
return {
|
|
|
|
error: `Shelf with ID ${shelfId} not found.`,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
shelf.updatedAt = this.getLastUpdatedTimestamp(shelf);
|
|
|
|
return shelf;
|
|
|
|
}
|
|
|
|
|
2020-01-08 06:50:58 +01:00
|
|
|
getFakeShelf () {
|
|
|
|
const faker = require('faker');
|
|
|
|
const isOwnShelf = faker.random.boolean();
|
|
|
|
const fakeName = faker.random.boolean()
|
|
|
|
? faker.fake('{{name.firstName}} {{name.lastName}}')
|
|
|
|
: faker.fake('{{hacker.adjective}}{{hacker.noun}}');
|
|
|
|
|
|
|
|
const shelf = {
|
|
|
|
id: faker.random.number(),
|
|
|
|
userId: faker.random.number(),
|
|
|
|
user: isOwnShelf ? null : {
|
|
|
|
name: fakeName,
|
|
|
|
handle: faker.fake('@{{internet.userName}}@{{internet.domainName}}'),
|
|
|
|
},
|
|
|
|
name: faker.fake('{{hacker.ingverb}} {{hacker.noun}}'),
|
|
|
|
isPublic: Math.random() < 0.9,
|
|
|
|
updatedAt: faker.date.past(),
|
|
|
|
shelfItems: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
const numberOfShelfItems = faker.random.number(50);
|
|
|
|
for (let i = 0; i < numberOfShelfItems; i++) {
|
|
|
|
const source = {
|
|
|
|
source: faker.internet.domainWord(),
|
|
|
|
sourceId: faker.random.uuid(),
|
|
|
|
};
|
|
|
|
const shelfItem = {
|
|
|
|
name: faker.lorem.words().split(' ').map(word => (word[0].toUpperCase() + word.substr(1))).join(' '),
|
|
|
|
author: faker.fake('a work by {{name.firstName}} {{name.lastName}}'),
|
|
|
|
source,
|
|
|
|
coverURL: faker.image.imageUrl(),
|
|
|
|
coverEdition: `img_${source.sourceId}`,
|
|
|
|
rating: faker.random.number(5),
|
|
|
|
review: faker.random.boolean()
|
|
|
|
? null
|
|
|
|
: faker.lorem.paragraph(),
|
|
|
|
}
|
|
|
|
|
|
|
|
shelf.shelfItems.push(shelfItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isOwnShelf) {
|
|
|
|
shelf.createdAt = faker.date.past(undefined, shelf.updatedAt);
|
|
|
|
shelf.isDeletable = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return shelf;
|
|
|
|
}
|
|
|
|
|
2020-01-05 03:34:26 +01:00
|
|
|
async userCanViewShelf (user, shelf) {
|
|
|
|
// This needs work when permissions are added.
|
2020-01-08 06:50:58 +01:00
|
|
|
const userOwnsShelf = typeof user !== 'undefined' && user.id === shelf.userId;
|
|
|
|
console.log('owned?', userOwnsShelf);
|
|
|
|
console.log('isPublic?', shelf.isPublic);
|
|
|
|
return userOwnsShelf || shelf.isPublic;
|
|
|
|
}
|
|
|
|
|
|
|
|
async scrubShelfData (shelf, user) {
|
|
|
|
const userOwnsShelf = user.id === shelf.userId;
|
|
|
|
const shelfUser = userOwnsShelf ? null : shelf.getUser();
|
|
|
|
let userData = {};
|
|
|
|
if (shelfUser !== null) {
|
|
|
|
userData.name = shelfUser.displayName;
|
|
|
|
userData.handle = `@${shelfUser.username}`;
|
|
|
|
}
|
|
|
|
const shelfData = {
|
|
|
|
name: shelf.name,
|
|
|
|
user: userOwnsShelf ? null : shelf.getUser(),
|
|
|
|
}
|
2020-01-05 03:34:26 +01:00
|
|
|
}
|
2019-12-01 02:02:24 +01:00
|
|
|
}
|
|
|
|
|
2019-12-01 19:03:55 +01:00
|
|
|
module.exports = ShelfController;
|