Guest User

Untitled

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