Advertisement
Guest User

Untitled

a guest
Aug 20th, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Express = require('Express')
  2. var app = new Express()
  3. var pg = require('pg')
  4.  
  5. function connect() {
  6.     console.log('Connecting to database..');
  7.  
  8.     const pg = require('pg');
  9.     const client = new pg.Client({user: 'hgpdgbosvgatfe',
  10.         host: 'ec2-50-16-196-138.compute-1.amazonaws.com',
  11.         database: 'durmvjt2ug0c1',
  12.         password: '7d88c3deb02ef15d683435b3906aedc7c9b8b6a0db0a3109943d37e8dcccdb54',
  13.         port: 5432,
  14.         ssl: true});
  15.     client.connect().then(res => {app.listen(8080)}, err =>{console.log(err)});
  16.     console.log("connected");
  17. }
  18.  
  19.  
  20. connect()
  21.  
  22. app.get('/addProduct', (req, res) =>
  23. {
  24.     if (!req.query.name || !req.query.description || !req.query.price)
  25.     {
  26.         res.send("Заполните все поля (name, description, price)")
  27.     }
  28.     else
  29.     {  
  30.         client.query("INSERT INTO products (name, description, price) VALUES"+
  31.         `(\'${req.query.name}\', \'${req.query.description}\', ${req.query.price} )`, (err, result) =>
  32.         {
  33.             if (err)
  34.             {
  35.                 res.send(err)
  36.             }
  37.             else
  38.             {
  39.                 res.send("Продукт успешно добавлен")
  40.             }
  41.         })     
  42.     }
  43. })
  44.  
  45. app.get('/getProduct', (req,res) =>
  46. {
  47.     if(req.query.id)
  48.     {
  49.         client.query(`SELECT * FROM products WHERE id =${req.query.id}`, (err,result) =>
  50.         {
  51.             if (err)
  52.             {
  53.                 res.send(err)
  54.             }
  55.             else
  56.             {
  57.                 res.send(result)
  58.             }
  59.         })
  60.     }
  61.     else if(req.query.name)
  62.     {
  63.         client.query(`SELECT * FROM products WHERE name =\'${req.query.name}\'`, (err,result) =>
  64.         {
  65.             if (err)
  66.             {
  67.                 res.send(err)
  68.             }
  69.             else
  70.             {
  71.                 res.send(result)
  72.             }
  73.         })
  74.     }
  75.     else
  76.     {
  77.         res.send("заполните имя или ид продукта ")
  78.     }
  79. })
  80.  
  81. app.get('/order', (req, res) =>
  82. {
  83.     if (!req.query.phone || !req.query.adress || !req.query.id || !req.query.count)
  84.     {
  85.         res.send("Заполните все поля заказа")
  86.     }
  87.     else
  88.     {
  89.         client.query("INSERT INTO orders (phone, adress, id, count)"+
  90.         `\'${req.query.phone}\', \'${req.query.adress}\', ${req.query.id}, ${req.query.count}`,(err, result) =>
  91.         {
  92.             if (err)
  93.             {
  94.                 res.send(err)
  95.             }
  96.             else
  97.             {
  98.                 res.send("Заказ успешно добавлен")
  99.             }
  100.         })
  101.     }
  102. })
  103.  
  104. app.get('/getOrders', (req,res) =>
  105. {
  106.     if(req.query.phone)
  107.     {
  108.         client.query(`Select * from orders where phone =${req.query.phone}`,(err, result)=>
  109.         {
  110.             if (err)
  111.             {
  112.                 res.send(err);
  113.             }
  114.             else
  115.             {
  116.                 res.send(result)
  117.             }
  118.         })
  119.     }
  120.     else
  121.     {
  122.         res.send("Введите номер телефона")
  123.     }
  124. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement