Last active
October 17, 2019 15:56
-
-
Save Nocktiss/5575ddc806a1eae18a9782b3656a7992 to your computer and use it in GitHub Desktop.
BackProduct
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// route/Product/addProduct.js | |
//Here I modified some things, for example async await at my home that did not pass, | |
//I had to add an export module (app), and on this file I have like error "TypeError: Cannot read property 'create' of undefined" | |
const Order = require('../../models/order'); | |
const Product = require('../../models/product').Product; | |
const ProductOrder = require('../../models/productOrder').ProductOrder; | |
module.exports = app => { | |
app.post('/orders/create', (req, res) => { | |
try { | |
// Create and save the order | |
const savedOrder = Order.create(req.body, { w: 1 }, { returning: true }); | |
// Loop through all the items in req.products | |
req.body.products.forEach((item) => { | |
// Search for the product with the givenId and make sure it exists. If it doesn't, respond with status 400. | |
const product = Product.findById(item.id); | |
if (!product) { | |
return res.status(400); | |
} | |
// Create a dictionary with which to create the ProductOrder | |
const po = { | |
orderId: savedOrder.id, | |
productId: item.id, | |
qty: item.qty, | |
} | |
// Create and save a productOrder | |
const savedProductOrder = ProductOrder.create(po, { w: 1 }, { returning: true }); | |
console.log(savedProductOrder) | |
}); | |
// If everything goes well, respond with the order | |
return res.status(200).send(savedOrder) | |
} | |
catch (error) { | |
console.error("Order creation server error: ", error); | |
res.status(500).json(error) | |
}; | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const env = require('./env.js'); | |
import Sequelize from 'sequelize'; | |
import UserModel from '../models/user'; | |
import ProductModel from '../models/product'; | |
import OrderModel from '../models/order'; | |
import ProductOrderModel from '../models/productOrder'; | |
const sequelize = new Sequelize(env.database, env.username, env.password, { | |
host: env.host, | |
dialect: env.dialect, | |
}); | |
const User = UserModel(sequelize, Sequelize); | |
const Product = ProductModel(sequelize, Sequelize) | |
const Order = OrderModel(sequelize, Sequelize) | |
const ProductOrder = ProductOrderModel(sequelize, Sequelize); | |
sequelize.sync().then(() => { | |
console.log('Users db and user table have been created'); | |
}).catch((error)=> console.log(error)); | |
module.exports = User, Product, Order, ProductOrder; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//I do not know if in your tutorial, it's normal that the "associate" field is empty ? | |
'use strict'; | |
module.exports = (sequelize, DataTypes) => { | |
const ProductOrder = sequelize.define('ProductOrder', { | |
uuid: DataTypes.UUID, | |
productId: DataTypes.UUID, | |
orderId: DataTypes.UUID, | |
quantity: DataTypes.DECIMAL | |
}, {}); | |
ProductOrder.associate = function(models) { | |
// associations can be defined here | |
}; | |
return ProductOrder; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// here its my all route | |
import express from 'express'; | |
import passport from 'passport'; | |
import bodyParser from 'body-parser'; | |
import logger from 'morgan'; | |
import Cors from 'cors'; | |
const app = express(); | |
const API_PORT = process.env.API_PORT || 4242; | |
require('./config/passport'); | |
app.use(Cors()); | |
app.use(bodyParser.urlencoded({ extended: false })); | |
app.use(bodyParser.json()); | |
app.use(express.json()); | |
app.use(logger('dev')); | |
app.use(passport.initialize()); | |
require('./routes/User/loginUser')(app); | |
require('./routes/User/registerUser')(app); | |
require('./routes/User/updatePassword')(app); | |
require('./routes/User/findUsers')(app); | |
require('./routes/User/deleteUser')(app); | |
require('./routes/User/updateUser')(app); | |
require('./routes/Product/addProduct')(app); | |
require('./routes/Product/getProduct')(app); | |
require('./routes/Product/patchProduct')(app); | |
require('./routes/Product/deleteProduct')(app); | |
app.listen(API_PORT, () => | |
console.log(`Listening on port ${API_PORT}`)); | |
module.exports = app; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment