Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. //router
  2.  
  3. 'use strict';
  4.  
  5. const express = require('express');
  6. const router = express.Router();
  7.  
  8. const controller = require('../controllers/index');
  9. //const connectionString = 'postgresql://postgres:root@localhost:5432/shop';
  10. //const client = new Client({connectionString});
  11.  
  12. router.get('/', (req, res, next) => {
  13. controller.get((err, result) => {
  14. if(err){
  15. res.status(500).send({'Erro': err});
  16. }else{
  17. res.status(200).send({
  18. data: result
  19. })
  20. }
  21. })
  22. });
  23.  
  24. router.get('/:id', controller.getId);
  25.  
  26. module.exports = router;
  27.  
  28. //controller
  29. const client = require('../../db/index');
  30.  
  31. client.connect();
  32.  
  33. exports.get = (callback) => {
  34. const re = [];
  35.  
  36. client.query('SELECT nome FROM cliente', (err, result) => {
  37. if(err){
  38. //done();
  39. callback(err);
  40. }else{
  41. result.rows.map( r => {
  42. re.push(r.nome);
  43. });
  44. callback(null, re);
  45. }
  46. });
  47. };
  48.  
  49. exports.getId = (req, res, next) => {
  50. const re = [];
  51.  
  52. client.query('SELECT nome FROM cliente WHERE id=$1',[req.params.id], (err, result) => {
  53. if(err){
  54. //done();
  55. console.log(err);
  56. res.status(400).send('error no banco');
  57. }
  58. result.rows.map( r => {
  59. re.push(r.nome);
  60. });
  61. //client.end();
  62. res.status(200).send({'nome':re});
  63.  
  64.  
  65. });
  66. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement