forked from mit-teaching-systems-lab/threeflows
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const { sql } = require('../../util/sqlHelpers'); | ||
const { query } = require('../../util/db'); | ||
|
||
const omitThese = ['ResponseRecall']; | ||
|
||
exports.getComponentsByUserId = async user_id => { | ||
const results = await query(sql` | ||
SELECT scenario_id, components | ||
FROM slide | ||
WHERE scenario_id IN ( | ||
SELECT scenario.id AS scenario_id | ||
FROM scenario | ||
JOIN scenario_user_role ON scenario_user_role.scenario_id = scenario.id | ||
WHERE deleted_at IS NULL | ||
AND scenario_user_role.user_id = ${user_id} | ||
AND scenario_user_role.role IN ('owner', 'author') | ||
) | ||
AND is_finish IS FALSE | ||
AND jsonb_array_length( components::jsonb ) != 0 | ||
ORDER BY "order" | ||
`); | ||
|
||
return results.rows.reduce((accum, {scenario_id, components}) => { | ||
return accum.concat( | ||
components.reduce((accum, component) => { | ||
if (omitThese.includes(component.type)) { | ||
return accum; | ||
} | ||
component.scenario_id = scenario_id; | ||
return accum.concat([component]); | ||
}, []) | ||
); | ||
}, []); | ||
}; | ||
|
||
exports.getComponentsByScenarioId = async scenario_id => { | ||
const results = await query(sql` | ||
SELECT scenario_id, components | ||
FROM slide | ||
WHERE scenario_id = ${scenario_id} | ||
AND is_finish IS FALSE | ||
AND jsonb_array_length( components::jsonb ) != 0 | ||
ORDER BY "order" | ||
`); | ||
|
||
return results.rows.reduce((accum, {scenario_id, components}) => { | ||
return accum.concat( | ||
components.reduce((accum, component) => { | ||
if (omitThese.includes(component.type)) { | ||
return accum; | ||
} | ||
component.scenario_id = scenario_id; | ||
return accum.concat([component]); | ||
}, []) | ||
); | ||
}, []); | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const { asyncMiddleware } = require('../../util/api'); | ||
const db = require('./db'); | ||
|
||
async function getComponentsAsync(req, res) { | ||
const components = await db.getComponentsByUserId(req.session.user.id); | ||
res.json({ components, status: 200 }); | ||
} | ||
|
||
async function getComponentsByUserIdAsync(req, res) { | ||
const components = await db.getComponentsByUserId(req.params.user_id); | ||
res.json({ components, status: 200 }); | ||
} | ||
|
||
async function getComponentsByScenarioIdAsync(req, res) { | ||
const components = await db.getComponentsByScenarioId(req.params.scenario_id); | ||
res.json({ components, status: 200 }); | ||
} | ||
|
||
exports.getComponents = asyncMiddleware(getComponentsAsync); | ||
exports.getComponentsByUserId = asyncMiddleware(getComponentsByUserIdAsync); | ||
exports.getComponentsByScenarioId = asyncMiddleware(getComponentsByScenarioIdAsync); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const { Router } = require('express'); | ||
const components = new Router(); | ||
const { | ||
getComponents, | ||
getComponentsByUserId, | ||
getComponentsByScenarioId, | ||
} = require('./endpoints'); | ||
|
||
components.get('/', getComponents); | ||
components.get('/user/:user_id', getComponentsByUserId); | ||
components.get('/scenario/:scenario_id', getComponentsByScenarioId); | ||
|
||
module.exports = components; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters