Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Express for webserver
- const express = require('express');
- // sequelize for database
- const Sequelize = require('sequelize');
- const bodyParser = require('body-parser');
- const app = express();
- app.use(bodyParser.urlencoded({extended: true }));
- app.use(bodyParser.json());
- const port = 3000;
- const DB_USER = "alex";
- const DB_PASS = '';
- const DB_HOST = "localhost";
- const DB_PORT = 5432;
- const DB_NAME = "pgguide";
- const DB_DIALECT = "postgres";
- //`${DB_DIALECT}://${DB_USER}:${DB_PASS}@${DB_HOST}:${DB_PORT}/${DB_NAME}`
- const sequelize = new Sequelize(`${DB_DIALECT}://${DB_USER}@${DB_HOST}:${DB_PORT}/${DB_NAME}`);
- // Operators
- const op = Sequelize.Op;
- const Users = sequelize.define('users', {
- id: {
- type: Sequelize.INTEGER,
- field: 'id',
- primaryKey: true
- },
- email: { type: Sequelize.STRING, field: 'email' },
- password: { type: Sequelize.STRING, field: 'password' },
- details: { type: Sequelize.HSTORE, field: 'details' },
- created_at: { type: Sequelize.DATE },
- deleted_at: { type: Sequelize.DATE }
- }, {
- timestamps: false
- });
- const Products = sequelize.define('products', {
- id: {
- type: Sequelize.INTEGER,
- field: 'id',
- primaryKey: true
- },
- title: { type: Sequelize.STRING },
- price: { type: Sequelize.NUMERIC },
- tags: { type: Sequelize.ARRAY(Sequelize.STRING)},
- created_at: { type: Sequelize.DATE },
- deleted_at: { type: Sequelize.DATE }
- }, {
- timestamps: false
- });
- app.get('/products', (req, res, next) => {
- const name = req.query.name; // ?name=
- if (name !== undefined) {
- Products.findAll({
- where: {
- title: {
- [op.iLike]: `%${name}%`
- }
- },
- order: [
- ['price', 'ASC']
- ]
- }).then((products) => {
- res.json(products);
- res.end();
- });
- } else {
- Products.findAll({
- order: [
- ['price', 'ASC']
- ]
- }).then((products) => {
- res.json(products);
- });
- }
- });
- app.get('/products/:id', (req, res, next) => {
- const id = req.params.id;
- if (id !== undefined) {
- Products.findOne({
- where: {
- id: {
- [op.eq]: id
- }
- }
- }).then((product) => {
- res.json(product);
- res.end();
- });
- } else {
- res.status(404);
- res.end();
- }
- });
- // Create new shizzle
- app.put('/products', (req, res, next) => {
- const booty = req.body;
- Products.create({
- id: sequelize.literal('DEFAULT'),
- title: booty.title,
- price: booty.price,
- tags: booty.tags,
- created_at: sequelize.literal('CURRENT_TIMESTAMP')
- }).then((product) => {
- res.json(product);
- res.end();
- });
- });
- // Update my gf
- app.post('/products/:id', (req, res, next) => {
- const ass = req.params.id;
- const tits = req.body;
- Products.update({
- title: tits.title,
- price: tits.price,
- tags: tits.tags
- }, {
- where: {
- id: {
- [op.eq]: ass
- }
- }
- }).then((product) => {
- res.json(product);
- res.end();
- });
- });
- // Dump that ho
- app.delete('/products/:id', (req, res, next) => {
- const phone = req.params.id;
- Products.destroy({
- where: {
- id: {
- [op.eq]: phone
- }
- }
- }).then((prod) => {
- res.json(prod);
- res.end();
- });
- });
- app.listen(port, () => {
- console.log(`Example app listening on port ${port}!`)
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement