Skip to content

Commit

Permalink
fix: creating a item should return the newly created value. (#81)
Browse files Browse the repository at this point in the history
fixes #79
  • Loading branch information
lholmquist authored Mar 1, 2018
1 parent 8f8eef9 commit d5a8bc1
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/api/fruits.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function findAll () {
}

function create (name, stock) {
return db.query('INSERT INTO products (name, stock) VALUES ($1, $2)', [name, stock]);
return db.query('INSERT INTO products (name, stock) VALUES ($1, $2) RETURNING *', [name, stock]);
}

function update (options = {}) {
Expand Down
5 changes: 3 additions & 2 deletions lib/routes/fruits.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ router.get('/fruits', (request, response) => {

router.post('/fruits', validations.validateCreate, (request, response) => {
const {name, stock} = request.body;
return fruits.create(name, stock).then(() => {
response.sendStatus(201);
return fruits.create(name, stock).then((result) => {
response.status(201);
return response.send(result.rows[0]);
}).catch((err) => {
response.status(400);
response.send(err);
Expand Down
2 changes: 1 addition & 1 deletion test/fruits-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ test('test POST fruit', (t) => {
create: (name, stock) => {
t.equal(name, fruitData.name, `respone.body.name should be ${fruitData.name}`);
t.equal(stock, fruitData.stock, `respone.body.stock should be ${fruitData.stock}`);
return Promise.resolve();
return Promise.resolve({rows: []});
}
};

Expand Down

0 comments on commit d5a8bc1

Please sign in to comment.