From d4d4f1a6e3dedcd044459ad0d902004882b50f80 Mon Sep 17 00:00:00 2001 From: data Date: Wed, 10 Jul 2019 20:46:05 +0200 Subject: [PATCH] add /remove tags to articles --- api/articles.js | 4 ++- api/tags.js | 64 +++++++++++++++++++++++++++++++++++++++------- db/models/index.js | 14 +++++++--- 3 files changed, 69 insertions(+), 13 deletions(-) diff --git a/api/articles.js b/api/articles.js index f7ef716..4639564 100644 --- a/api/articles.js +++ b/api/articles.js @@ -15,7 +15,9 @@ router.post('/', async (req, res, next) => { /* READ */ router.get('/', async (req, res, next) => { try { - const articles = await Article.findAll() + const articles = await Article.findAll({ + include: { model: Tag }, + }) res.send(articles) } catch (err) { next(err) diff --git a/api/tags.js b/api/tags.js index 8be62a7..ba873ea 100644 --- a/api/tags.js +++ b/api/tags.js @@ -5,8 +5,32 @@ module.exports = router /* CREATE */ router.post('/', async (req, res, next) => { try { - const tag = await Tag.create(req.body) - res.json(tag) + // find or create tag and add article relation + var tag = await Tag.findOne({ + where: { name: req.body.name }, + include: { model: Article }, + }) + if (!tag) { + const newTag = { name: req.body.name } + tag = await Tag.create(newTag, { include: { model: Article } }) + } + tag.addArticle(req.body.articleId) + + // find selected article and add tag relation + var article = await Article.findByPk(req.body.articleId, { + include: { model: Tag }, + }) + article.addTag(tag.id) + + // update article and tag after adding the relation + article = await Article.findByPk(req.body.articleId, { + include: { model: Tag }, + }) + tag = await Tag.findOne({ + where: { name: req.body.name }, + include: { model: Article }, + }) + res.json({ tag: tag, article: article }) } catch (err) { next(err) } @@ -15,7 +39,9 @@ router.post('/', async (req, res, next) => { /* READ */ router.get('/', async (req, res, next) => { try { - const tags = await Tag.findAll() + const tags = await Tag.findAll({ + include: { model: Article }, + }) res.send(tags) } catch (err) { next(err) @@ -29,14 +55,34 @@ router.get('/:id', async (req, res, next) => { } catch (err) { next(err) } -}) +}) // remove tag from article -/* UPDATE */ -router.put('/:id', async (req, res, next) => { +/* UPDATE */ router.put('/:id', async (req, res, next) => { try { - const tag = await Tag.findByPk(req.params.id) - await tag.update(req.body) - res.json(tag) + //const tag = await Tag.findByPk(req.params.id) + //await tag.update(req.body) + //res.json(tag) + + // find or create tag and add article relation + var tag = await Tag.findByPk(req.body.id, { + include: { model: Article }, + }) + tag.removeArticle(req.body.articleId) + + // find selected article and add tag relation + var article = await Article.findByPk(req.body.articleId, { + include: { model: Tag }, + }) + article.removeTag(tag.id) + + // update article and tag after adding the relation + article = await Article.findByPk(req.body.articleId, { + include: { model: Tag }, + }) + tag = await Tag.findByPk(req.body.id, { + include: { model: Article }, + }) + res.json({ tag: tag, article: article }) } catch (err) { next(err) } diff --git a/db/models/index.js b/db/models/index.js index dd8533e..ee7ca68 100755 --- a/db/models/index.js +++ b/db/models/index.js @@ -12,9 +12,9 @@ User.hasMany(Vote) Project.hasMany(Task) Task.belongsTo(Project) Article.hasMany(Comment) -Article.hasMany(Tag) +Article.belongsToMany(Tag, { through: 'articleTags' }) Article.belongsTo(User) -Tag.belongsTo(Article) +Tag.belongsToMany(Article, { through: 'articleTags' }) Comment.belongsTo(Article) Comment.belongsTo(User) Comment.hasMany(Vote) @@ -27,4 +27,12 @@ Project.hasMany(User) Task.belongsToMany(User, { through: 'userTask' }) User.hasMany(Task) -module.exports = { Task, Project, User, Article, Tag, Comment, Vote } +module.exports = { + Task, + Project, + User, + Article, + Tag, + Comment, + Vote, +}