allow to edit tags in admin panel, fix #170

This commit is contained in:
lesion
2022-12-13 15:41:39 +01:00
parent 854bd6538a
commit 4463c75536
9 changed files with 194 additions and 6 deletions

View File

@@ -1,6 +1,8 @@
const Tag = require('../models/tag')
const Event = require('../models/event')
const uniq = require('lodash/uniq')
const log = require('../../log')
const { where, fn, col, Op } = require('sequelize')
const exportController = require('./export')
@@ -45,6 +47,20 @@ module.exports = {
}
},
async getAll (_req, res) {
const tags = await Tag.findAll({
order: [[fn('COUNT', col('tag.tag')), 'DESC']],
attributes: ['tag', [fn('COUNT', col('tag.tag')), 'count']],
include: [{ model: Event, where: { is_visible: true }, attributes: [], through: { attributes: [] }, required: true }],
group: ['tag.tag'],
raw: true,
})
return res.json(tags)
},
/**
* search for tags by query string
* sorted by usage
@@ -60,9 +76,48 @@ module.exports = {
include: [{ model: Event, where: { is_visible: true }, attributes: [], through: { attributes: [] }, required: true }],
group: ['tag.tag'],
limit: 10,
subQuery:false
subQuery: false
})
return res.json(tags.map(t => t.tag))
},
async updateTag (req, res) {
const tag = await Tag.findByPk(req.body.tag)
await tag.update(req.body)
res.json(place)
},
async updateTag (req, res) {
const oldtag = await Tag.findByPk(req.body.tag)
const newtag = await Tag.findByPk(req.body.newTag)
// if the new tag does not exists, just rename the old one
if (!newtag) {
oldtag.tag = req.body.newTag
await oldtag.update({ tag: req.body.newTag })
} else {
// in case it exists:
// - search for events with old tag
const events = await oldtag.getEvents()
// - substitute it with the new one
await oldtag.removeEvents(events)
await newtag.addEvents(events)
}
res.sendStatus(200)
},
async remove (req, res) {
log.info('Remove tag', req.params.tag)
const tagName = req.params.tag
try {
const tag = await Tag.findByPk(tagName)
await tag.destroy()
res.sendStatus(200)
} catch (e) {
log.error('Tag removal failed:', e)
res.sendStatus(404)
}
}
}