2023-09-13 16:59:17 +02:00
|
|
|
import MapModel from '../db/models/map.js'
|
|
|
|
|
|
|
|
const getAllMaps = async (req, res) => {
|
|
|
|
const maps = await MapModel.query()
|
|
|
|
|
|
|
|
res.json({ maps })
|
|
|
|
}
|
|
|
|
|
|
|
|
const getMapByName = async (req, res) => {
|
|
|
|
const name = req.params.mapName
|
|
|
|
try {
|
|
|
|
let map = await MapModel.query().where({ name }).withGraphFetched('map_points').first()
|
|
|
|
|
|
|
|
if (!map) {
|
|
|
|
const created = await MapModel.query().insert({ name })
|
|
|
|
map = await MapModel.query().findById(created.id).withGraphFetched('map_points')
|
|
|
|
}
|
|
|
|
|
2023-09-14 13:39:47 +02:00
|
|
|
// convert location from string to point
|
|
|
|
if (map?.map_points.length > 0) {
|
|
|
|
map.map_points.forEach(p => {
|
|
|
|
if (
|
|
|
|
typeof p.location === 'string' ||
|
|
|
|
p.location instanceof String
|
|
|
|
) {
|
|
|
|
const locString = p.location.replace(/[()\s]/g, '')
|
|
|
|
const [ x, y ] = locString.split(',')
|
|
|
|
p.location = { x, y }
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-09-13 16:59:17 +02:00
|
|
|
res.json(map)
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export { getMapByName, getAllMaps }
|