refactoring tags page / controller

This commit is contained in:
lesion 2022-06-18 01:07:43 +02:00
parent 44ed5881cc
commit e021ec56ba
No known key found for this signature in database
GPG key ID: 352918250B012177
2 changed files with 42 additions and 15 deletions

View file

@ -11,11 +11,24 @@
</template>
<script>
import { mapState } from 'vuex'
import Event from '@/components/Event'
export default {
name: 'Tag',
components: { Event },
head ({ $route }) {
const tag = $route.params.tag
const title = `${this.settings.title} #${tag}`
return {
title,
link: [
{ rel: 'alternate', type: 'application/rss+xml', title, href: this.settings.baseurl + `/feed/rss/tag/${tag}` },
{ rel: 'alternate', type: 'text/calendar', title, href: this.settings.baseurl + `/feed/ics/tag/${tag}` }
]
}
},
computed: mapState(['settings']),
async asyncData ({ $axios, params, error }) {
try {
const tag = params.tag

View file

@ -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: }
// ]
// }})
// }
}