From 6a2127a7bbc56d6c400a5a57c91f9cc333766cc1 Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Sun, 30 Aug 2020 12:53:41 -0400 Subject: [PATCH] code complete --- controllers/people.js | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/controllers/people.js b/controllers/people.js index c28c591..c2454f6 100644 --- a/controllers/people.js +++ b/controllers/people.js @@ -9,26 +9,27 @@ router.get('/', (req, res) => { }); router.post('/', (req, res) => { - data.push(req.body); - res.json(data); + postgres.query(`INSERT INTO people (name, age) VALUES ('${req.body.name}', ${req.body.age})`, (err, results) => { + postgres.query('SELECT * FROM people ORDER BY id ASC;', (err, results) => { + res.json(results.rows) + }); + }) }); router.delete('/:id', (req, res) => { - data = data.filter(person => person.id != req.params.id, 1); - res.json(data); + postgres.query(`DELETE FROM people WHERE id = ${req.params.id};`, (err, results) => { + postgres.query('SELECT * FROM people ORDER BY id ASC;', (err, results) => { + res.json(results.rows) + }); + }); }); router.put('/:id', (req, res) => { - req.body.id = req.params.id; - - data = data.map((person) => { - if(person.id == req.params.id){ - return req.body - } else { - return person - } + postgres.query(`UPDATE people SET name = '${req.body.name}', AGE = ${req.body.age} WHERE id = ${req.params.id}`, (err, results) => { + postgres.query('SELECT * FROM people ORDER BY id ASC;', (err, results) => { + res.json(results.rows) + }); }) - res.json(data); }); module.exports = router;