Compare commits

...

2 Commits

5 changed files with 89 additions and 2 deletions

View File

@ -1,4 +1,4 @@
import fetch from 'node-fetch'; const fetch = require('node-fetch');
class ShelfController { class ShelfController {
constructor (shelfModel, shelfItemModel) { constructor (shelfModel, shelfItemModel) {
@ -24,7 +24,7 @@ class ShelfController {
return true; return true;
} }
async static CheckExternalDomainForShelf (domain, shelfId) { static async CheckExternalDomainForShelf (domain, shelfId) {
const response = await fetch(`https://${domain}/api/shelf/get/${shelfId}/`).then(response => response.json()); const response = await fetch(`https://${domain}/api/shelf/get/${shelfId}/`).then(response => response.json());
// validate response somehow // validate response somehow
return response; return response;

View File

@ -4,6 +4,7 @@ module.exports = models => {
User, User,
PermissionLevel, PermissionLevel,
BookReference, BookReference,
Reactions,
} = models; } = models;
Review.belongsTo(User, { Review.belongsTo(User, {
@ -21,5 +22,11 @@ module.exports = models => {
onDelete: 'SET NULL', onDelete: 'SET NULL',
}); });
Review.hasMany(Reactions.scope('Review'), {
foreignKey: 'targetId',
constraints: false,
as: 'Reactions',
});
return Review; return Review;
} }

View File

@ -3,6 +3,7 @@ module.exports = models => {
Status, Status,
User, User,
ShelfItem, ShelfItem,
Reactions,
} = models; } = models;
Status.belongsTo(User, { Status.belongsTo(User, {
@ -20,5 +21,11 @@ module.exports = models => {
onDelete: 'SET NULL', onDelete: 'SET NULL',
}); });
Status.hasMany(Reactions.scope('Status'), {
foreignKey: 'targetId',
constraints: false,
as: 'Reactions',
});
return Status; return Status;
} }

View File

@ -0,0 +1,72 @@
const Sequelize = require('sequelize');
module.exports = sequelize => sequelize.define('Reaction', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
userId: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: sequelize.models.User,
key: 'id',
deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE,
},
},
targetType: {
type: Sequelize.ENUM,
values: ['Status', 'Review'],
allowNull: false,
comment: 'The model/table that `targetId` lives in.',
},
targetId: {
type: Sequelize.INTEGER,
allowNull: false,
comment: 'The id of the `targetType` record this Reaction relates to.',
},
reactionType: {
type: Sequelize.ENUM,
values: ['comment', 'like', 'emoji'],
allowNull: false,
comment: 'Stores what type of reaction this record is: Comment, Like, Emoji',
},
reaction: {
type: Sequelize.TEXT,
allowNull: true,
comment: 'Stores the Comment or Emoji value of the reaction if not a Like.',
},
// Timestamps
createdAt: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.NOW,
},
updatedAt: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.NOW,
},
}, {
indexes: [
{
fields: ['userId'],
},
{
fields: ['targetType', 'targetId'],
},
{
fields: ['reactionType'],
},
],
scopes: {
Status: {
where: { targetType: 'Status' },
},
Review: {
where: { targetType: 'Review' },
},
}
});

View File

@ -10,6 +10,7 @@ module.exports = sequelize => {
'Status', 'Status',
'Review', 'Review',
'Recommendation', 'Recommendation',
'Reaction',
]; ];
const models = {}; const models = {};