Skip to content

Commit

Permalink
Merge pull request #55 from fac19/refactored-update-with-coalesce
Browse files Browse the repository at this point in the history
Refactored update function with SQL COALESCE keyword and added some c…
  • Loading branch information
Roger-Heathcote committed Apr 18, 2020
2 parents bd60bbd + 6cac8f1 commit a66cec1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 41 deletions.
4 changes: 3 additions & 1 deletion handlers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
52 changes: 16 additions & 36 deletions model/examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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,
Expand Down
5 changes: 1 addition & 4 deletions tests/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const {

const {
getExample,
// updateExample,
updateExamplebyID
} = require("../model/examples");

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit a66cec1

Please sign in to comment.