colouring-montreal/app/src/api/api.ts

110 lines
2.8 KiB
TypeScript
Raw Normal View History

2019-08-14 09:05:49 -04:00
import bodyParser from 'body-parser';
2019-11-07 02:39:26 -05:00
import express from 'express';
2019-08-14 05:54:13 -04:00
2019-08-14 09:05:49 -04:00
import buildingsRouter from './routes/buildingsRouter';
import extractsRouter from './routes/extractsRouter';
2019-11-07 02:39:26 -05:00
import usersRouter from './routes/usersRouter';
import { queryLocation } from './services/search';
import { authUser, getNewUserAPIKey, logout } from './services/user';
2019-08-14 05:54:13 -04:00
const server = express.Router();
2019-08-14 05:54:13 -04:00
2019-08-14 09:05:49 -04:00
// parse POSTed json body
server.use(bodyParser.json());
2019-08-14 05:54:13 -04:00
2019-08-14 09:05:49 -04:00
server.use('/buildings', buildingsRouter);
server.use('/users', usersRouter);
server.use('/extracts', extractsRouter);
2019-08-14 05:54:13 -04:00
// POST user auth
server.post('/login', function (req, res) {
authUser(req.body.username, req.body.password).then(function (user: any) { // TODO: remove any
if (user.user_id) {
req.session.user_id = user.user_id;
} else {
req.session.user_id = undefined;
}
res.send(user);
}).catch(function (error) {
res.send(error);
2019-11-07 03:13:30 -05:00
});
2019-08-14 05:54:13 -04:00
});
// POST user logout
server.post('/logout', function (req, res) {
logout(req.session).then(() => {
2019-08-14 05:54:13 -04:00
res.send({ success: true });
}).catch(err => {
console.error(err);
res.send({ error: 'Failed to end session'});
2019-08-14 05:54:13 -04:00
});
});
// POST generate API key
server.post('/api/key', function (req, res) {
if (!req.session.user_id) {
res.send({ error: 'Must be logged in' });
2019-11-07 03:13:30 -05:00
return;
2019-08-14 05:54:13 -04:00
}
getNewUserAPIKey(req.session.user_id).then(function (apiKey) {
res.send(apiKey);
}).catch(function (error) {
res.send(error);
});
2019-11-07 03:13:30 -05:00
});
2019-08-14 05:54:13 -04:00
// GET search
server.get('/search', function (req, res) {
const searchTerm = req.query.q;
if (!searchTerm) {
res.send({
error: 'Please provide a search term'
2019-11-07 03:13:30 -05:00
});
return;
2019-08-14 05:54:13 -04:00
}
queryLocation(searchTerm).then((results) => {
if (typeof (results) === 'undefined') {
res.send({
error: 'Database error'
2019-11-07 03:13:30 -05:00
});
return;
2019-08-14 05:54:13 -04:00
}
res.send({
results: results.map(item => {
// map from DB results to GeoJSON Feature objects
2019-11-07 03:13:30 -05:00
const geom = JSON.parse(item.st_asgeojson);
2019-08-14 05:54:13 -04:00
return {
type: 'Feature',
attributes: {
label: item.search_str,
zoom: item.zoom || 9
},
geometry: geom
2019-11-07 03:13:30 -05:00
};
2019-08-14 05:54:13 -04:00
})
2019-11-07 03:13:30 -05:00
});
2019-08-14 05:54:13 -04:00
}).catch(function (error) {
res.send(error);
});
2019-11-07 03:13:30 -05:00
});
2019-08-14 05:54:13 -04:00
2019-08-21 09:47:44 -04:00
server.use((err, req, res, next) => {
if (res.headersSent) {
return next(err);
}
if (err != undefined) {
console.log('Global error handler: ', err);
res.status(500).send({ error: 'Server error' });
}
});
2019-08-14 09:13:14 -04:00
server.use((req, res) => {
res.status(404).json({ error: 'Resource not found'});
2019-11-07 03:13:30 -05:00
});
2019-08-14 09:13:14 -04:00
2019-08-21 09:47:44 -04:00
export default server;