Refactor project DB functions.

The old db.project.list() function is now db.project.get()
and the old db.project.get() is not db.project.summary.get().

If a project does not exist, db.project.summary.get() now
throws a 404 rather than a database error.
This commit is contained in:
D. Berge
2023-08-21 14:36:02 +02:00
parent 44ad59130f
commit f2edd2bec5
5 changed files with 33 additions and 22 deletions

View File

@@ -1,16 +1,8 @@
const { setSurvey } = require('../connection');
const { setSurvey, pool } = require('../connection');
async function get (projectId, opts = {}) {
const client = await setSurvey(projectId);
const text = `
SELECT *
FROM project_summary;
`;
const res = await client.query(text);
client.release();
return res.rows[0];
async function get () {
const res = await pool.query("SELECT pid, name, schema FROM public.projects;");
return res.rows;
}
module.exports = get;

View File

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

View File

@@ -1,8 +0,0 @@
const { setSurvey, pool } = require('../connection');
async function list () {
const res = await pool.query("SELECT * FROM public.projects;");
return res.rows;
}
module.exports = list;

View File

@@ -0,0 +1,24 @@
const { setSurvey } = require('../../connection');
async function get (projectId, opts = {}) {
try {
const client = await setSurvey(projectId);
const text = `
SELECT *
FROM project_summary;
`;
const res = await client.query(text);
client.release();
return res.rows[0];
} catch (err) {
if (err.code == "42P01") {
throw { status: 404, message: "Not found" };
} else {
throw err;
}
}
}
module.exports = get;

View File

@@ -0,0 +1,3 @@
module.exports = {
get: require('./get'),
};