From eaeb6c272ffd738dcf10d2ad8e98fb2f53d3a7b6 Mon Sep 17 00:00:00 2001 From: lesion Date: Wed, 25 May 2022 10:53:51 +0200 Subject: [PATCH] add a tag controller --- server/api/controller/tag.js | 47 ++++++++++++++++++++++++++++++++++++ server/api/index.js | 5 ++++ 2 files changed, 52 insertions(+) create mode 100644 server/api/controller/tag.js diff --git a/server/api/controller/tag.js b/server/api/controller/tag.js new file mode 100644 index 00000000..97b6cd2e --- /dev/null +++ b/server/api/controller/tag.js @@ -0,0 +1,47 @@ +const dayjs = require('dayjs') +const Tag = require('../models/tag') +const Event = require('../models/event') +const eventController = require('./event') +const Sequelize = require('sequelize') + +module.exports = { + // async getEvents (req, res) { + // const name = req.params.placeName + // const place = await Place.findOne({ where: { name }}) + // if (!place) { + // log.warn(`Place ${name} not found`) + // return res.sendStatus(404) + // } + // const start = dayjs().unix() + // const events = await eventController._select({ start, places: `${place.id}`, show_recurrent: true}) + + // return res.json({ events, place }) + // }, + + async get (req, res) { + const search = req.query.search + console.error(search) + const tags = await Tag.findAll({ + order: [[Sequelize.fn('COUNT', Sequelize.col('tag.tag')), 'DESC']], + attributes: ['tag'], + where: { + tag: Sequelize.where(Sequelize.fn('LOWER', Sequelize.col('tag')), 'LIKE', '%' + search + '%'), + }, + include: [{ model: Event, where: { is_visible: true }, attributes: [], through: { attributes: [] }, required: true }], + group: ['tag.tag'], + limit: 10, + subQuery:false + }) + + 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: } + // ] + // }}) + // } +} \ No newline at end of file diff --git a/server/api/index.js b/server/api/index.js index 3bcd96a5..4749295f 100644 --- a/server/api/index.js +++ b/server/api/index.js @@ -24,6 +24,7 @@ if (config.status !== 'READY') { const { isAuth, isAdmin } = require('./auth') const eventController = require('./controller/event') const placeController = require('./controller/place') + const tagController = require('./controller/tag') const settingsController = require('./controller/settings') const exportController = require('./controller/export') const userController = require('./controller/user') @@ -156,6 +157,10 @@ if (config.status !== 'READY') { api.get('/place/:placeName/events', cors, placeController.getEvents) + // api.get('/place', cors, placeController.) + + api.get('/tag', cors, tagController.get) + // - FEDIVERSE INSTANCES, MODERATION, RESOURCES api.get('/instances', isAdmin, instanceController.getAll)