diff --git a/lib/www/server/lib/db/sequence/get.js b/lib/www/server/lib/db/sequence/get.js index 1ff205f..d725543 100644 --- a/lib/www/server/lib/db/sequence/get.js +++ b/lib/www/server/lib/db/sequence/get.js @@ -15,11 +15,50 @@ async function getSummary (projectId, sequence, opts = {}) { return res.rows[0]; } +async function getPoints (projectId, sequence, opts = {}) { + + const offset = Math.abs(opts.offset) || Math.abs((opts.page-1)*opts.itemsPerPage) || 0; + const limit = Math.abs(opts.limit) || Math.abs(Number(opts.itemsPerPage)) || null; + + const client = await setSurvey(projectId); + + const restriction = sequence + ? "sequence = $3" + : "TRUE OR $3"; + + const text = ` + SELECT line, point, sequence, st_x(ST_Transform(geometry, 4326)) longitude, st_y(ST_Transform(geometry, 4326)) latitude + FROM final_shots + WHERE ${restriction} + ORDER BY sequence, point + OFFSET $1 + LIMIT $2; + `; + + try { + const res = await client.query({text, values: [offset, limit, sequence], rowMode: 'array'}); + return res.rows; + } catch (err) { + console.error(err); + // throw { status: 500, message: "Internal error" }; + } finally { + client.release(); + } + +} async function get (projectId, sequence, opts = {}) { if (opts.summary) { return await getSummary(projectId, sequence, opts); } + if (opts.type == 4) { + // The user is request that we send just the bare details: + // sequence, sailline, line, longitude, latitude. + // + // This will probably be a binary data request (though doesn't + // need to). + return await getPoints(projectId, sequence, opts); + } const client = await setSurvey(projectId);