diff --git a/handlers/users.js b/handlers/users.js index b2f09fe..74de171 100644 --- a/handlers/users.js +++ b/handlers/users.js @@ -49,7 +49,9 @@ function signup(req, res, next) { }) .catch(console.error); } - +// login function +// IMPROVEMENTS +// display error message if email or password are missing function login(req, res, next) { model .getUser(req.body.email) diff --git a/model/examples.js b/model/examples.js index 6101c09..3ad6a35 100644 --- a/model/examples.js +++ b/model/examples.js @@ -61,47 +61,13 @@ function getExample(id) { .then((res) => res.rows[0]); } - //must update ALL VALUES otherwise any value not updated will return NULL - -// function updateExample(id, newdata) { -// return ( -// db -// .query( -// "UPDATE examples SET language=($1), title=($2), example=($3) WHERE id =($4) RETURNING *", -// [newdata.language, newdata.title, newdata.example, id] -// ) -// -// .then((res) => res.rows[0]) -// ); - -// } function updateExamplebyID(id, newdata, userId) { return getExample(id) .then(dbExample => { if(dbExample.id === userId){ - - var query = ["UPDATE examples"]; - query.push("SET"); - - // Create new array and store each set parameter - // assign a number value for parameterized query - const set = []; - const values = []; - Object.keys(newdata).forEach((key, i) => { - set.push(key + "=($" + (i + 1) + ")"); - values.push(newdata[key]); - }); - query.push(set.join(", ")); - - // Add WHERE statement to look up by id - query.push("WHERE id=" + id + " RETURNING *"); - - // Return a complete query string - // console.log(query.join(" ")) - // console.log(values) - - return db.query(query.join(" "), values) + const vals = [ newdata.language, newdata.title, newdata.example, id ]; + return db.query("UPDATE examples SET language = COALESCE($1, language), title = COALESCE($2, title), example = COALESCE($3, example) WHERE id =($4) RETURNING *", vals) .then((res) => res.rows[0]); } else { const error = new Error("You do not own this example") @@ -110,7 +76,21 @@ function updateExamplebyID(id, newdata, userId) { } }) } + +// var query = ["UPDATE examples"]; +// query.push("SET"); +// const set = []; +// const values = []; +// Object.keys(newdata).forEach((key, i) => { +// set.push(key + "=($" + (i + 1) + ")"); +// values.push(newdata[key]); +// }); +// query.push(set.join(", ")); + +// query.push("WHERE id=" + id + " RETURNING *"); + +// return db.query(query.join(" "), values) module.exports = { getAllExamples, diff --git a/tests/model.test.js b/tests/model.test.js index 68ae6cb..6a30e31 100644 --- a/tests/model.test.js +++ b/tests/model.test.js @@ -12,7 +12,6 @@ const { const { getExample, - // updateExample, updateExamplebyID } = require("../model/examples"); @@ -76,17 +75,15 @@ test("Can get an example by id", (t) => { }); -test("Can get update an example by id without all values", (t) => { +test.only("Can get update an example by id without all values", (t) => { build().then(() => { const data = { language: "sql", - // title: 'SQL example snippet', example: "This is an example of SQL", }; updateExamplebyID(4, data, 4) .then((res) => { t.equal(res.language, "sql", "Language updated OK"); - // t.equal(res.title, 'SQL example snippet') t.equal(res.title, "Test example 4", "Title not altered"); t.equal(res.example, "This is an example of SQL", "Example text updated OK"); t.end();