Add preplot line patching endpoint.

Allows us to change remarks, meta and ntba
fields in preplot lines.
This commit is contained in:
D. Berge
2020-10-01 18:25:32 +02:00
parent 3a87f8959a
commit f9ef971802
10 changed files with 67 additions and 9 deletions

View File

@@ -91,11 +91,11 @@ app.map({
'/project/:project/line/': {
get: [ mw.line.list ],
},
// '/project/:project/line/:line': {
'/project/:project/line/:line': {
// get: [ mw.line.get ],
// patch: [ mw.line.patch ],
// },
//
patch: [ mw.line.patch ],
},
'/project/:project/sequence/': {
get: [ mw.sequence.list ],
},

View File

@@ -1,4 +1,5 @@
module.exports = {
list: require('./list'),
// get: require('./get')
get: require('./get'),
patch: require('./patch')
};

View File

@@ -0,0 +1,17 @@
const { line } = require('../../../lib/db');
module.exports = async function (req, res, next) {
try {
const payload = req.body;
await line.patch(req.params.project, req.params.line, payload);
res.status(201).send();
next();
} catch (err) {
next(err);
}
};

View File

View File

View File

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

View File

@@ -1,4 +1,4 @@
const { setSurvey } = require('./connection');
const { setSurvey } = require('../connection');
async function list (projectId, opts = {}) {
const client = await setSurvey(projectId);
@@ -42,6 +42,4 @@ async function list (projectId, opts = {}) {
return res.rows;
}
module.exports = {
list
};
module.exports = list;

View File

@@ -0,0 +1,33 @@
const { setSurvey, transaction } = require('../connection');
async function patch (projectId, line, payload, opts = {}) {
const client = await setSurvey(projectId);
const patchables = {
"remarks": "UPDATE preplot_lines SET remarks = $2 WHERE line = $1;",
"meta": "UPDATE preplot_lines SET meta = $2 WHERE line = $1;",
"ntba": "UPDATE preplot_lines SET ntba = $2 WHERE line = $1;",
};
try {
transaction.begin(client);
for (const key in payload) {
const text = patchables[key];
const values = [ line, payload[key] ];
if (!text) {
throw {status: 400, message: "Invalid patch" };
}
await client.query(text, values);
}
transaction.commit(client);
} catch (err) {
transaction.rollback(err);
throw err;
} finally {
client.release();
}
}
module.exports = patch;

View File

View File