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 */ /* READ */
router.get('/', async (req, res, next) => { router.get('/', async (req, res, next) => {
try { try {
const articles = await Article.findAll() const articles = await Article.findAll({
include: { model: Tag },
})
res.send(articles) res.send(articles)
} catch (err) { } catch (err) {
next(err) next(err)

View File

@ -5,8 +5,32 @@ module.exports = router
/* CREATE */ /* CREATE */
router.post('/', async (req, res, next) => { router.post('/', async (req, res, next) => {
try { try {
const tag = await Tag.create(req.body) // find or create tag and add article relation
res.json(tag) 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) { } catch (err) {
next(err) next(err)
} }
@ -15,7 +39,9 @@ router.post('/', async (req, res, next) => {
/* READ */ /* READ */
router.get('/', async (req, res, next) => { router.get('/', async (req, res, next) => {
try { try {
const tags = await Tag.findAll() const tags = await Tag.findAll({
include: { model: Article },
})
res.send(tags) res.send(tags)
} catch (err) { } catch (err) {
next(err) next(err)
@ -29,14 +55,34 @@ router.get('/:id', async (req, res, next) => {
} catch (err) { } catch (err) {
next(err) next(err)
} }
}) }) // remove tag from article
/* UPDATE */ /* UPDATE */ router.put('/:id', async (req, res, next) => {
router.put('/:id', async (req, res, next) => {
try { try {
const tag = await Tag.findByPk(req.params.id) //const tag = await Tag.findByPk(req.params.id)
await tag.update(req.body) //await tag.update(req.body)
res.json(tag) //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) { } catch (err) {
next(err) next(err)
} }

View File

@ -12,9 +12,9 @@ User.hasMany(Vote)
Project.hasMany(Task) Project.hasMany(Task)
Task.belongsTo(Project) Task.belongsTo(Project)
Article.hasMany(Comment) Article.hasMany(Comment)
Article.hasMany(Tag) Article.belongsToMany(Tag, { through: 'articleTags' })
Article.belongsTo(User) Article.belongsTo(User)
Tag.belongsTo(Article) Tag.belongsToMany(Article, { through: 'articleTags' })
Comment.belongsTo(Article) Comment.belongsTo(Article)
Comment.belongsTo(User) Comment.belongsTo(User)
Comment.hasMany(Vote) Comment.hasMany(Vote)
@ -27,4 +27,12 @@ Project.hasMany(User)
Task.belongsToMany(User, { through: 'userTask' }) Task.belongsToMany(User, { through: 'userTask' })
User.hasMany(Task) User.hasMany(Task)
module.exports = { Task, Project, User, Article, Tag, Comment, Vote } module.exports = {
Task,
Project,
User,
Article,
Tag,
Comment,
Vote,
}