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

View File

@@ -162,15 +162,21 @@ if (config.status !== 'READY') {
api.get('/export/:type', cors, exportController.export)
api.get('/place/all', isAdmin, placeController.getAll)
// - PLACES
api.get('/places', isAdmin, placeController.getAll)
api.get('/place/:placeName', cors, placeController.getEvents)
api.get('/place', cors, placeController.search)
api.get('/placeOSM/Nominatim/:place_details', cors, placeController._nominatim)
api.get('/placeOSM/Photon/:place_details', cors, placeController._photon)
api.put('/place', isAdmin, placeController.updatePlace)
// - TAGS
api.get('/tags', isAdmin, tagController.getAll)
api.get('/tag', cors, tagController.search)
api.get('/tag/:tag', cors, tagController.getEvents)
api.delete('/tag/:tag', isAdmin, tagController.remove)
api.put('/tag', isAdmin, tagController.updateTag)
// - FEDIVERSE INSTANCES, MODERATION, RESOURCES
api.get('/instances', isAdmin, instanceController.getAll)