Skip to content

Commit

Permalink
Merge df9cf90 into 853cd93
Browse files Browse the repository at this point in the history
  • Loading branch information
MBenedicte committed Jul 24, 2019
2 parents 853cd93 + df9cf90 commit e45e141
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 32 deletions.
41 changes: 11 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion src/models/articlebookmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ export default (sequelize, DataTypes) => {
articleSlug: {
type: DataTypes.STRING,
allowNull: false,
primaryKey: true
primaryKey: true,
references: {
model: 'Articles',
key: 'slug'
},
onUpdate: 'CASCADE',
onDelete: 'SET NULL'
},
createdAt: {
allowNull: false,
Expand All @@ -29,6 +35,11 @@ export default (sequelize, DataTypes) => {
);
ArticleBookmark.associate = (models) => {
ArticleBookmark.belongsTo(models.User, { foreignKey: 'userId' });
ArticleBookmark.belongsTo(models.Article, {
foreignKey: 'articleSlug',
targetKey: 'slug',
as: 'article'
});
};
return ArticleBookmark;
};
12 changes: 11 additions & 1 deletion src/queries/articleBookmarks/getAllBookmarkedArticles.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@ import db from '../../models';
export default async (bookmarkedBy) => {
try {
const bookmarkedArticles = bookmarkedBy
? await db.ArticleBookmark.findAll({ where: { userId: bookmarkedBy }, logging: false })
? await db.ArticleBookmark.findAll({
where: { userId: bookmarkedBy },
logging: false,
include: [
{
model: db.Article,
as: 'article',
attributes: ['id', 'title', 'description', 'coverUrl', 'readTime']
}
]
})
: null;

return bookmarkedArticles || [];
Expand Down

0 comments on commit e45e141

Please sign in to comment.