mirror of
https://gitlab.com/wgp/dougal/software.git
synced 2025-12-06 09:07:09 +00:00
Implement PUT method for project configuration endpoint.
In short: POST creates a new project PUT overwrites a project configuration with a new one PATCH merges the request body with the existing configuration
This commit is contained in:
@@ -114,6 +114,7 @@ app.map({
|
|||||||
'/project/:project/configuration': {
|
'/project/:project/configuration': {
|
||||||
get: [ mw.project.configuration.get ], // Get project configuration
|
get: [ mw.project.configuration.get ], // Get project configuration
|
||||||
patch: [ mw.auth.access.admin, mw.project.configuration.patch ], // Modify project configuration
|
patch: [ mw.auth.access.admin, mw.project.configuration.patch ], // Modify project configuration
|
||||||
|
put: [ mw.auth.access.admin, mw.project.configuration.put ], // Overwrite configuration
|
||||||
},
|
},
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
get: require('./get'),
|
get: require('./get'),
|
||||||
// post: require('./post'),
|
// post: require('./post'),
|
||||||
// put: require('./put'),
|
put: require('./put'),
|
||||||
patch: require('./patch'),
|
patch: require('./patch'),
|
||||||
// delete: require('./delete'),
|
// delete: require('./delete'),
|
||||||
};
|
};
|
||||||
|
|||||||
16
lib/www/server/api/middleware/project/configuration/put.js
Normal file
16
lib/www/server/api/middleware/project/configuration/put.js
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
|
||||||
|
const { project } = require('../../../../lib/db');
|
||||||
|
|
||||||
|
module.exports = async function (req, res, next) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
// TODO
|
||||||
|
// Implement If-Match header requirements
|
||||||
|
res.send(await project.configuration.put(req.params.project, req.body));
|
||||||
|
next();
|
||||||
|
} catch (err) {
|
||||||
|
next(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
get: require('./get'),
|
get: require('./get'),
|
||||||
// post: require('./post'),
|
// post: require('./post'),
|
||||||
// put: require('./put'),
|
put: require('./put'),
|
||||||
patch: require('./patch'),
|
patch: require('./patch'),
|
||||||
// delete: require('./delete'),
|
// delete: require('./delete'),
|
||||||
};
|
};
|
||||||
|
|||||||
58
lib/www/server/lib/db/project/configuration/put.js
Normal file
58
lib/www/server/lib/db/project/configuration/put.js
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
const { setSurvey } = require('../../connection');
|
||||||
|
const { deepMerge, removeNulls } = require('../../../utils');
|
||||||
|
const { modify } = require('../create');
|
||||||
|
|
||||||
|
|
||||||
|
async function put (projectId, payload, opts = {}) {
|
||||||
|
let client;
|
||||||
|
try {
|
||||||
|
client = await setSurvey(); // Use public schema
|
||||||
|
|
||||||
|
const text = `
|
||||||
|
SELECT meta
|
||||||
|
FROM projects
|
||||||
|
WHERE pid = $1;
|
||||||
|
`;
|
||||||
|
|
||||||
|
const res = await client.query(text, [projectId]);
|
||||||
|
|
||||||
|
const source = res.rows[0].meta;
|
||||||
|
|
||||||
|
if (!source) {
|
||||||
|
throw { status: 404, message: "Not found" };
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("PAYLOAD ID", payload.id, typeof payload);
|
||||||
|
if (("id" in payload) && (projectId.toLowerCase() != payload.id.toLowerCase())) {
|
||||||
|
throw {
|
||||||
|
status: 422,
|
||||||
|
message: "Project ID cannot be changed in this Dougal version"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (("name" in payload) && source.name && (source.name != payload.name)) {
|
||||||
|
throw {
|
||||||
|
status: 422,
|
||||||
|
message: "Project name cannot be changed in this Dougal version"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// We do not allow users to change the schema
|
||||||
|
delete payload.schema;
|
||||||
|
|
||||||
|
const dest = removeNulls(payload);
|
||||||
|
await modify(projectId, dest);
|
||||||
|
return dest;
|
||||||
|
|
||||||
|
} catch (err) {
|
||||||
|
if (err.code == "42P01") {
|
||||||
|
throw { status: 404, message: "Not found" };
|
||||||
|
} else {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
client.release();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = put;
|
||||||
Reference in New Issue
Block a user