Advertisement
Guest User

One love

a guest
Feb 15th, 2019
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Express for webserver
  2. const express = require('express');
  3. // sequelize for database
  4. const Sequelize = require('sequelize');
  5.  
  6. const bodyParser = require('body-parser');
  7. const app = express();
  8. app.use(bodyParser.urlencoded({extended: true }));
  9. app.use(bodyParser.json());
  10. const port = 3000;
  11.  
  12. const DB_USER = "alex";
  13. const DB_PASS = '';
  14. const DB_HOST = "localhost";
  15. const DB_PORT = 5432;
  16. const DB_NAME = "pgguide";
  17. const DB_DIALECT = "postgres";
  18.  
  19. //`${DB_DIALECT}://${DB_USER}:${DB_PASS}@${DB_HOST}:${DB_PORT}/${DB_NAME}`
  20. const sequelize = new Sequelize(`${DB_DIALECT}://${DB_USER}@${DB_HOST}:${DB_PORT}/${DB_NAME}`);
  21. // Operators
  22. const op = Sequelize.Op;
  23.  
  24. const Users = sequelize.define('users', {
  25.     id: {
  26.         type: Sequelize.INTEGER,
  27.         field: 'id',
  28.         primaryKey: true
  29.     },
  30.     email: { type: Sequelize.STRING, field: 'email' },
  31.     password: { type: Sequelize.STRING, field: 'password' },
  32.     details: { type: Sequelize.HSTORE, field: 'details' },
  33.     created_at: { type: Sequelize.DATE },
  34.     deleted_at: { type: Sequelize.DATE }
  35. }, {
  36.     timestamps: false
  37. });
  38.  
  39. const Products = sequelize.define('products', {
  40.     id: {
  41.         type: Sequelize.INTEGER,
  42.         field: 'id',
  43.         primaryKey: true
  44.     },
  45.     title: { type: Sequelize.STRING },
  46.     price: { type: Sequelize.NUMERIC },
  47.     tags: { type: Sequelize.ARRAY(Sequelize.STRING)},
  48.     created_at: { type: Sequelize.DATE },
  49.     deleted_at: { type: Sequelize.DATE }
  50. }, {
  51.     timestamps: false
  52. });
  53.  
  54. app.get('/products', (req, res, next) => {
  55.     const name = req.query.name; // ?name=
  56.  
  57.     if (name !== undefined) {
  58.         Products.findAll({
  59.             where: {
  60.                 title: {
  61.                     [op.iLike]: `%${name}%`
  62.                 }
  63.             },
  64.             order: [
  65.                 ['price', 'ASC']
  66.             ]
  67.         }).then((products) => {
  68.             res.json(products);
  69.             res.end();
  70.         });
  71.     } else {
  72.         Products.findAll({
  73.             order: [
  74.                 ['price', 'ASC']
  75.             ]
  76.         }).then((products) => {
  77.             res.json(products);
  78.         });
  79.     }
  80. });
  81.  
  82. app.get('/products/:id', (req, res, next) => {
  83.     const id = req.params.id;
  84.  
  85.     if (id !== undefined) {
  86.         Products.findOne({
  87.             where: {
  88.                 id: {
  89.                     [op.eq]: id
  90.                 }
  91.             }
  92.         }).then((product) => {
  93.             res.json(product);
  94.             res.end();
  95.         });
  96.     } else {
  97.         res.status(404);
  98.         res.end();
  99.     }
  100. });
  101.  
  102. // Create new shizzle
  103. app.put('/products', (req, res, next) => {
  104.     const booty = req.body;
  105.     Products.create({
  106.         id: sequelize.literal('DEFAULT'),
  107.         title: booty.title,
  108.         price: booty.price,
  109.         tags: booty.tags,
  110.         created_at: sequelize.literal('CURRENT_TIMESTAMP')
  111.     }).then((product) => {
  112.         res.json(product);
  113.         res.end();
  114.     });
  115. });
  116.  
  117. // Update my gf
  118. app.post('/products/:id', (req, res, next) => {
  119.     const ass = req.params.id;
  120.     const tits = req.body;
  121.     Products.update({
  122.         title: tits.title,
  123.         price: tits.price,
  124.         tags: tits.tags
  125.     }, {
  126.         where: {
  127.             id: {
  128.                 [op.eq]: ass
  129.             }
  130.         }
  131.     }).then((product) => {
  132.         res.json(product);
  133.         res.end();
  134.     });
  135. });
  136.  
  137. // Dump that ho
  138. app.delete('/products/:id', (req, res, next) => {
  139.     const phone = req.params.id;
  140.  
  141.     Products.destroy({
  142.         where: {
  143.             id: {
  144.                 [op.eq]: phone
  145.             }
  146.         }
  147.     }).then((prod) => {
  148.         res.json(prod);
  149.         res.end();
  150.     });
  151. });
  152.  
  153. app.listen(port, () => {
  154.     console.log(`Example app listening on port ${port}!`)
  155. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement