Add info API endpoint.

It queries a project's `info` table.
This commit is contained in:
D. Berge
2020-09-09 15:55:04 +02:00
parent 1a612f74d6
commit db25878fdd
14 changed files with 70 additions and 1 deletions

View File

@@ -125,6 +125,10 @@ app.map({
get: [ mw.configuration.get ],
// post: [ mw.label.post ],
},
'/project/:project/info/:path(*)': {
get: [ mw.info.get ],
// post: [ mw.info.post ],
},
//
// '/project/:id/permissions/:mode(read|write)?': {
// get: [ mw.permissions.get ],

View File

@@ -8,5 +8,6 @@ module.exports = {
gis: require('./gis'),
label: require('./label'),
navdata: require('./navdata'),
configuration: require('./configuration')
configuration: require('./configuration'),
info: require('./info')
};

View File

@@ -0,0 +1,18 @@
const { info } = require('../../../lib/db');
module.exports = async function (req, res, next) {
try {
res.status(200).json(await info.get(req.params.project, req.params.path, req.query));
} catch (err) {
if (err instanceof TypeError) {
res.status(404).json(null);
} else {
next(err);
return;
}
}
next();
};

View File

@@ -0,0 +1,8 @@
module.exports = {
list: require('./list'),
get: require('./get'),
post: require('./post'),
put: require('./put'),
delete: require('./delete')
}

View File

@@ -7,5 +7,6 @@ module.exports = {
gis: require('./gis'),
label: require('./label'),
configuration: require('./configuration'),
info: require('./info'),
navdata: require('./navdata')
};

View File

View File

@@ -0,0 +1,30 @@
const { setSurvey } = require('../connection');
async function get (projectId, path, opts = {}) {
const client = await setSurvey(projectId);
const [key, ...subkey] = path.split("/");
const text = `
SELECT value
FROM info
WHERE key = $1;
`;
console.log("k, sk", key, subkey);
const res = await client.query(text, [key]);
client.release();
const value = res.rows[0] && res.rows[0].value;
if (subkey.length) {
console.log("Reducing value");
const res = subkey.reduce( (obj, idx) => typeof obj != "undefined" ? obj[idx] : obj, value);
console.log(res);
return res;
} else {
console.log("Returning value");
return value;
}
}
module.exports = get;

View File

@@ -0,0 +1,7 @@
module.exports = {
get: require('./get'),
post: require('./post'),
put: require('./put'),
delete: require('./delete')
}

View File

View File