mirror of
https://framagit.org/les/gancio.git
synced 2025-01-31 16:42:22 +01:00
searchMeta api
This commit is contained in:
parent
8be335cd55
commit
c8f40e2e17
3 changed files with 39 additions and 71 deletions
|
@ -10,7 +10,7 @@ export default ({ $axios }, inject) => {
|
||||||
* end_datetime: unix_timestamp
|
* end_datetime: unix_timestamp
|
||||||
* tags: [tag, list],
|
* tags: [tag, list],
|
||||||
* places: [place_id],
|
* places: [place_id],
|
||||||
* limit: (default ∞)
|
* max: (default ∞)
|
||||||
* }
|
* }
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
@ -22,7 +22,8 @@ export default ({ $axios }, inject) => {
|
||||||
end: params.end,
|
end: params.end,
|
||||||
places: params.places && params.places.join(','),
|
places: params.places && params.places.join(','),
|
||||||
tags: params.tags && params.tags.join(','),
|
tags: params.tags && params.tags.join(','),
|
||||||
show_recurrent: !!params.show_recurrent
|
show_recurrent: !!params.show_recurrent,
|
||||||
|
max: params.maxs
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return events.map(e => Object.freeze(e))
|
return events.map(e => Object.freeze(e))
|
||||||
|
|
|
@ -22,38 +22,51 @@ const log = require('../../log')
|
||||||
|
|
||||||
const eventController = {
|
const eventController = {
|
||||||
|
|
||||||
async _getMeta () {
|
async searchMeta (req, res) {
|
||||||
|
const search = req.query.search
|
||||||
|
|
||||||
const places = await Place.findAll({
|
const places = await Place.findAll({
|
||||||
order: [[Sequelize.literal('weigth'), 'DESC']],
|
order: [[Sequelize.col('w'), 'DESC']],
|
||||||
attributes: {
|
where: {
|
||||||
include: [[Sequelize.fn('count', Sequelize.col('events.placeId')), 'weigth']],
|
[Op.or]: [
|
||||||
exclude: ['createdAt', 'updatedAt']
|
{ name: Sequelize.where(Sequelize.fn('LOWER', Sequelize.col('name')), 'LIKE', '%' + search + '%' )},
|
||||||
|
{ address: Sequelize.where(Sequelize.fn('LOWER', Sequelize.col('address')), 'LIKE', '%' + search + '%')},
|
||||||
|
]
|
||||||
},
|
},
|
||||||
|
attributes: [['name', 'label'], 'address', 'id', [Sequelize.cast(Sequelize.fn('COUNT', Sequelize.col('events.placeId')),'INTEGER'), 'w']],
|
||||||
include: [{ model: Event, where: { is_visible: true }, required: true, attributes: [] }],
|
include: [{ model: Event, where: { is_visible: true }, required: true, attributes: [] }],
|
||||||
group: ['place.id']
|
group: ['place.id'],
|
||||||
|
raw: true
|
||||||
})
|
})
|
||||||
|
|
||||||
const tags = await Tag.findAll({
|
const tags = await Tag.findAll({
|
||||||
order: [[Sequelize.literal('w'), 'DESC']],
|
order: [[Sequelize.col('w'), 'DESC']],
|
||||||
attributes: {
|
where: {
|
||||||
include: [[Sequelize.fn('COUNT', Sequelize.col('tag.tag')), 'w']]
|
tag: Sequelize.where(Sequelize.fn('LOWER', Sequelize.col('tag')), 'LIKE', '%' + search + '%'),
|
||||||
},
|
},
|
||||||
|
attributes: [['tag','label'], [Sequelize.cast(Sequelize.fn('COUNT', Sequelize.col('tag.tag')), 'INTEGER'), 'w']],
|
||||||
include: [{ model: Event, where: { is_visible: true }, attributes: [], through: { attributes: [] }, required: true }],
|
include: [{ model: Event, where: { is_visible: true }, attributes: [], through: { attributes: [] }, required: true }],
|
||||||
group: ['tag.tag']
|
group: ['tag.tag'],
|
||||||
|
raw: true
|
||||||
})
|
})
|
||||||
|
|
||||||
return { places, tags }
|
const ret = places.map(p => {
|
||||||
|
p.type = 'place'
|
||||||
|
return p
|
||||||
|
}).concat(tags.map(t => {
|
||||||
|
t.type = 'tag'
|
||||||
|
return t
|
||||||
|
})).sort( (a, b) => b.w - a.w).slice(0, 10)
|
||||||
|
|
||||||
|
return res.json(ret)
|
||||||
},
|
},
|
||||||
|
|
||||||
async getMeta (req, res) {
|
|
||||||
res.json(await eventController._getMeta())
|
|
||||||
},
|
|
||||||
|
|
||||||
async search (req, res) {
|
async search (req, res) {
|
||||||
const search = req.query.search
|
const search = req.query.search
|
||||||
|
|
||||||
// search for events
|
// search for events
|
||||||
const events = Event.findAll({
|
const events = await Event.findAll({
|
||||||
logging: console.log,
|
logging: console.log,
|
||||||
order: [['start_datetime', 'DESC']],
|
order: [['start_datetime', 'DESC']],
|
||||||
attributes: {
|
attributes: {
|
||||||
|
@ -65,48 +78,11 @@ const eventController = {
|
||||||
parentId: null,
|
parentId: null,
|
||||||
title: Sequelize.where(Sequelize.fn('LOWER', Sequelize.col('title')), 'LIKE', '%' + search + '%'),
|
title: Sequelize.where(Sequelize.fn('LOWER', Sequelize.col('title')), 'LIKE', '%' + search + '%'),
|
||||||
},
|
},
|
||||||
limit: 3
|
limit: 10
|
||||||
})
|
})
|
||||||
|
|
||||||
// search for tags
|
|
||||||
const tags = Tag.findAll({
|
|
||||||
order: [[Sequelize.literal('w'), 'DESC']],
|
|
||||||
attributes: {
|
|
||||||
include: [[Sequelize.fn('COUNT', Sequelize.col('tag.tag')), 'w']],
|
|
||||||
},
|
|
||||||
where: { tag: { [Op.substring]: search } },
|
|
||||||
include: [{ model: Event, where: { is_visible: true, recurrent: null, parentId: null }, attributes: [], through: { attributes: [] }, required: true }],
|
|
||||||
group: ['tag.tag'],
|
|
||||||
limit: 3
|
|
||||||
})
|
|
||||||
|
|
||||||
// let places
|
return res.json(events)
|
||||||
// try {
|
|
||||||
// places = await Place.findAll({
|
|
||||||
// logging: console.log,
|
|
||||||
// where: { name: { [Op.substring]: search } },
|
|
||||||
// order: [[Sequelize.literal('weigth'), 'DESC']],
|
|
||||||
// attributes: {
|
|
||||||
// include: [[Sequelize.fn('count', Sequelize.col('events.placeId')), 'weigth']],
|
|
||||||
// // exclude: ['createdAt', 'updatedAt']
|
|
||||||
// },
|
|
||||||
// include: [{ model: Event, where: { is_visible: true }, through: { attributes: [] }, attributes: [] }],
|
|
||||||
// limit: 3,
|
|
||||||
// group: ['place.id'],
|
|
||||||
// })
|
|
||||||
// } catch (e) {
|
|
||||||
// console.error(e)
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
const [ tmpevents, tmptags] = await Promise.all([events, tags])
|
|
||||||
|
|
||||||
let ret = tmpevents
|
|
||||||
.map(e => ({ ...e.get(), type: 'event' }))
|
|
||||||
.concat(tmptags.map(t => ({ ...t.get(), type: 'tag' })))
|
|
||||||
|
|
||||||
|
|
||||||
return res.json(ret)
|
|
||||||
},
|
},
|
||||||
|
|
||||||
async getNotifications (event, action) {
|
async getNotifications (event, action) {
|
||||||
|
@ -137,12 +113,6 @@ const eventController = {
|
||||||
return notifications.filter(notification => match(event, notification.filters))
|
return notifications.filter(notification => match(event, notification.filters))
|
||||||
},
|
},
|
||||||
|
|
||||||
async updatePlace (req, res) {
|
|
||||||
const place = await Place.findByPk(req.body.id)
|
|
||||||
await place.update(req.body)
|
|
||||||
res.json(place)
|
|
||||||
},
|
|
||||||
|
|
||||||
async _get(slug) {
|
async _get(slug) {
|
||||||
// retrocompatibility, old events URL does not use slug, use id as fallback
|
// retrocompatibility, old events URL does not use slug, use id as fallback
|
||||||
const id = Number(slug) || -1
|
const id = Number(slug) || -1
|
||||||
|
|
|
@ -80,9 +80,6 @@ if (config.status !== 'READY') {
|
||||||
// get all users
|
// get all users
|
||||||
api.get('/users', isAdmin, userController.getAll)
|
api.get('/users', isAdmin, userController.getAll)
|
||||||
|
|
||||||
// update a place (modify address..)
|
|
||||||
api.put('/place', isAdmin, eventController.updatePlace)
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get events
|
* Get events
|
||||||
* @category Event
|
* @category Event
|
||||||
|
@ -132,7 +129,7 @@ if (config.status !== 'READY') {
|
||||||
api.delete('/event/:id', isAuth, eventController.remove)
|
api.delete('/event/:id', isAuth, eventController.remove)
|
||||||
|
|
||||||
// get tags/places
|
// get tags/places
|
||||||
api.get('/event/meta', eventController.getMeta)
|
api.get('/event/meta', eventController.searchMeta)
|
||||||
|
|
||||||
// get unconfirmed events
|
// get unconfirmed events
|
||||||
api.get('/event/unconfirmed', isAdmin, eventController.getUnconfirmed)
|
api.get('/event/unconfirmed', isAdmin, eventController.getUnconfirmed)
|
||||||
|
@ -157,11 +154,11 @@ if (config.status !== 'READY') {
|
||||||
|
|
||||||
|
|
||||||
api.get('/place/:placeName/events', cors, placeController.getEvents)
|
api.get('/place/:placeName/events', cors, placeController.getEvents)
|
||||||
// api.get('/place', cors, placeController.)
|
api.get('/place', cors, placeController.get)
|
||||||
|
api.put('/place', isAdmin, placeController.updatePlace)
|
||||||
|
|
||||||
api.get('/tag', cors, tagController.get)
|
api.get('/tag', cors, tagController.get)
|
||||||
|
|
||||||
|
|
||||||
// - FEDIVERSE INSTANCES, MODERATION, RESOURCES
|
// - FEDIVERSE INSTANCES, MODERATION, RESOURCES
|
||||||
api.get('/instances', isAdmin, instanceController.getAll)
|
api.get('/instances', isAdmin, instanceController.getAll)
|
||||||
api.get('/instances/:instance_domain', isAdmin, instanceController.get)
|
api.get('/instances/:instance_domain', isAdmin, instanceController.get)
|
||||||
|
@ -192,10 +189,10 @@ if (config.status !== 'READY') {
|
||||||
api.post('/client', oauthController.createClient)
|
api.post('/client', oauthController.createClient)
|
||||||
}
|
}
|
||||||
|
|
||||||
api.use((req, res) => res.sendStatus(404))
|
api.use((_req, res) => res.sendStatus(404))
|
||||||
|
|
||||||
// Handle 500
|
// Handle 500
|
||||||
api.use((error, req, res, next) => {
|
api.use((error, _req, res, _next) => {
|
||||||
log.error('[API ERROR]', error)
|
log.error('[API ERROR]', error)
|
||||||
res.status(500).send('500: Internal Server Error')
|
res.status(500).send('500: Internal Server Error')
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue