refactoring tags page / controller
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
const dayjs = require('dayjs')
|
||||
const Tag = require('../models/tag')
|
||||
const Event = require('../models/event')
|
||||
const { where, fn, col, Op } = require('sequelize')
|
||||
const exportController = require('./export')
|
||||
const eventController = require('./event')
|
||||
const Sequelize = require('sequelize')
|
||||
|
||||
module.exports = {
|
||||
// async getEvents (req, res) {
|
||||
@@ -18,14 +18,37 @@ module.exports = {
|
||||
// return res.json({ events, place })
|
||||
// },
|
||||
|
||||
async get (req, res) {
|
||||
// /feed/rss/tag/tagname
|
||||
// /feed/ics/tag/tagname
|
||||
// /feed/json/tag/tagname
|
||||
async getEvents (req, res) {
|
||||
const format = req.params.format || 'json'
|
||||
const tags = req.params.tag
|
||||
const events = await eventController._select({ tags, show_recurrent: true })
|
||||
|
||||
switch (format) {
|
||||
case 'rss':
|
||||
return exportController.feed(req, res, events,
|
||||
`${res.locals.settings.title} - Tag #${tags}`,
|
||||
`${res.locals.settings.baseurl}/feed/rss/tag/${tags}`)
|
||||
case 'ics':
|
||||
return exportController.ics(req, res, events)
|
||||
default:
|
||||
return res.json(events)
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* search for tags by query string
|
||||
* sorted by usage
|
||||
*/
|
||||
async search (req, res) {
|
||||
const search = req.query.search
|
||||
console.error(search)
|
||||
const tags = await Tag.findAll({
|
||||
order: [[Sequelize.fn('COUNT', Sequelize.col('tag.tag')), 'DESC']],
|
||||
order: [[fn('COUNT', col('tag.tag')), 'DESC']],
|
||||
attributes: ['tag'],
|
||||
where: {
|
||||
tag: Sequelize.where(Sequelize.fn('LOWER', Sequelize.col('tag')), 'LIKE', '%' + search + '%'),
|
||||
tag: where(fn('LOWER', col('tag')), 'LIKE', '%' + search + '%'),
|
||||
},
|
||||
include: [{ model: Event, where: { is_visible: true }, attributes: [], through: { attributes: [] }, required: true }],
|
||||
group: ['tag.tag'],
|
||||
@@ -35,13 +58,4 @@ module.exports = {
|
||||
|
||||
return res.json(tags.map(t => t.tag))
|
||||
}
|
||||
|
||||
// async getPlaces (req, res) {
|
||||
// const search = req.params.search
|
||||
// const places = await Place.findAll({ where: {
|
||||
// [Op.or]: [
|
||||
// { name: }
|
||||
// ]
|
||||
// }})
|
||||
// }
|
||||
}
|
||||
Reference in New Issue
Block a user