2022-05-23 14:41:07 +02:00
|
|
|
const dayjs = require('dayjs')
|
|
|
|
const Place = require('../models/place')
|
2022-05-31 15:27:46 +02:00
|
|
|
const Event = require('../models/event')
|
2022-05-23 14:41:07 +02:00
|
|
|
const eventController = require('./event')
|
2022-05-31 15:27:46 +02:00
|
|
|
const log = require('../../log')
|
|
|
|
const { Op, where, col, fn } = require('sequelize')
|
2022-05-23 14:41:07 +02:00
|
|
|
|
|
|
|
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 })
|
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)
|
|
|
|
},
|
|
|
|
|
|
|
|
async get (req, res) {
|
|
|
|
const search = req.query.search
|
|
|
|
const places = await Place.findAll({
|
|
|
|
where: {
|
|
|
|
[Op.or]: [
|
|
|
|
{ name: where(fn('LOWER', col('name')), 'LIKE', '%' + search + '%') },
|
|
|
|
{ address: where(fn('LOWER', col('address')), 'LIKE', '%' + search + '%') },
|
|
|
|
]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return res.json(places)
|
|
|
|
}
|
|
|
|
|
2022-05-23 14:41:07 +02:00
|
|
|
}
|