Skip to content

Commit

Permalink
finished GET /api/schemes/:scheme_id
Browse files Browse the repository at this point in the history
  • Loading branch information
JL1172 committed Oct 10, 2023
1 parent 0f55106 commit 7236d5e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
7 changes: 4 additions & 3 deletions api/schemes/scheme-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ const db = require("../../data/db-config");
"message": "scheme with scheme_id <actual id> not found"
}
*/
const checkSchemeId = async (req, res, next) => {
const checkSchemeId = async(req, res, next) => {
try {
const isValidSID = await db("schemes").where({scheme_id : req.params.id}).first();
const {scheme_id} = req.params
const isValidSID = await db("schemes").where("scheme_id",scheme_id).first();
if (!isValidSID) {
next({status : 404, message : `scheme with scheme_id ${req.params.id} not found`});
next({status : 404, message : `scheme with scheme_id ${scheme_id} not found`});
} else {
next();
}
Expand Down
23 changes: 23 additions & 0 deletions api/schemes/scheme-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,29 @@ async function find() { // EXERCISE A
}

async function findById(scheme_id) { // EXERCISE B
const firstResult = await db("schemes as sc")
.select("sc.scheme_name","st.*")
.leftJoin("steps as st", "sc.scheme_id","st.scheme_id")
.where("sc.scheme_id",scheme_id)
.orderBy("st.step_number","asc");

const steps = [];
for (let key of firstResult) {
if (key.instructions) {
steps.push({
step_id : key.step_id,
step_number : key.step_number,
instructions : key.instructions
})
}
}
const returnObject = {
scheme_id : firstResult[0].scheme_id,
scheme_name : firstResult[0].scheme_name,
steps : steps,
}
return returnObject

/*
1B- Study the SQL query below running it in SQLite Studio against `data/schemes.db3`:
Expand Down

0 comments on commit 7236d5e

Please sign in to comment.