add /remove tags to articles

This commit is contained in:
data 2019-07-10 20:46:05 +02:00
parent 9ed521c298
commit d4d4f1a6e3
3 changed files with 69 additions and 13 deletions

View File

@ -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)

View File

@ -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)
}

View File

@ -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,
}