gancio-upstream/server/api/controller/place.js

77 lines
2.6 KiB
JavaScript
Raw Permalink Normal View History

const { Place, Event } = require('../models/models')
2022-05-23 14:41:07 +02:00
const eventController = require('./event')
const exportController = require('./export')
2022-05-31 15:27:46 +02:00
const log = require('../../log')
2022-06-01 14:14:32 +02:00
const { Op, where, col, fn, cast } = require('sequelize')
2022-05-23 14:41:07 +02:00
module.exports = {
2022-05-23 14:41:07 +02:00
async getEvents (req, res) {
2025-01-24 22:26:05 +01:00
const name = req.params.placeNameOrId
const id = Number(req.params.placeNameOrId)
const place = await Place.findOne({ where: { ...(isNaN(id) ? { name } : { id })}})
2022-05-23 14:41:07 +02:00
if (!place) {
log.warn(`Place ${placeNameOrId} not found`)
2022-05-23 14:41:07 +02:00
return res.sendStatus(404)
}
const format = req.params.format || 'json'
log.debug(`Events for place: ${place.name}`)
2025-01-22 12:11:21 +01:00
const events = await eventController._select({ places: String(place.id), show_recurrent: true, show_multidate: true, show_federated: true })
switch (format) {
case 'rss':
return exportController.feed(req, res, events,
`${res.locals.settings.title} - Place @${place.name}`,
`${res.locals.settings.baseurl}/feed/rss/place/${place.id}`)
case 'ics':
return exportController.ics(req, res, events)
default:
2025-01-22 12:11:21 +01:00
const pastEvents = await eventController._select({ places: String(place.id), show_recurrent: true, show_multidate: true, older: true, reverse: true, show_federated: true })
return res.json({ events, pastEvents, place })
}
2022-05-31 15:27:46 +02:00
},
async updatePlace (req, res) {
const place = await Place.findByPk(req.body.id)
await place.update(req.body)
res.json(place)
},
2022-06-01 14:14:32 +02:00
async getAll (_req, res) {
const places = await Place.findAll({
order: [[cast(fn('COUNT', col('events.placeId')),'INTEGER'), 'DESC']],
include: [{ model: Event, where: { is_visible: true }, required: true, attributes: [] }],
group: ['place.id'],
raw: true
})
return res.json(places)
},
async search (req, res) {
const search = req.query?.search?.toLocaleLowerCase() ?? ''
2022-05-31 15:27:46 +02:00
const places = await Place.findAll({
2022-06-01 14:14:32 +02:00
order: [[cast(fn('COUNT', col('events.placeId')),'INTEGER'), 'DESC']],
2022-05-31 15:27:46 +02:00
where: {
[Op.or]: [
2022-06-01 14:14:32 +02:00
{ name: where(fn('LOWER', col('name')), 'LIKE', '%' + search + '%' )},
{ address: where(fn('LOWER', col('address')), 'LIKE', '%' + search + '%')},
2022-05-31 15:27:46 +02:00
]
2022-06-01 14:14:32 +02:00
},
attributes: ['name', 'address', 'latitude', 'longitude', 'id'],
include: [{ model: Event, where: { is_visible: true, ap_id: null }, required: true, attributes: [] }],
2022-06-01 14:14:32 +02:00
group: ['place.id'],
raw: true,
limit: 10,
subQuery: false
2022-05-31 15:27:46 +02:00
})
2022-06-01 14:14:32 +02:00
// TOFIX: don't know why limit does not work
return res.json(places.slice(0, 10))
}
2022-05-31 15:27:46 +02:00
2022-09-02 08:32:13 +02:00
}