Creazione tabella Product
This commit is contained in:
@@ -27,6 +27,12 @@ const reg = require('../reg/registration');
|
||||
|
||||
const { authenticate } = require('../middleware/authenticate');
|
||||
|
||||
const Cart = require('../models/cart');
|
||||
const CartClass = require('../modules/Cart')
|
||||
const Product = require('../models/product')
|
||||
const Variant = require('../models/variant')
|
||||
const TypedError = require('../modules/ErrorHandler')
|
||||
|
||||
const mongoose = require('mongoose');
|
||||
const Subscription = mongoose.model('subscribers');
|
||||
|
||||
@@ -793,5 +799,140 @@ router.post('/dbop', authenticate, async (req, res) => {
|
||||
|
||||
});
|
||||
|
||||
//GET cart
|
||||
router.get('/:userId/cart', authenticate, function (req, res, next) {
|
||||
let userId = req.params.userId
|
||||
Cart.getCartByUserId(userId, function (err, cart) {
|
||||
if (err) return next(err)
|
||||
if (cart.length < 1) {
|
||||
let err = new TypedError('cart error', 404, 'not_found', { message: "create a cart first" })
|
||||
return next(err)
|
||||
}
|
||||
return res.json({ cart: cart[0] })
|
||||
})
|
||||
})
|
||||
|
||||
//POST cart
|
||||
router.post('/:userId/cart', authenticate, function (req, res, next) {
|
||||
let userId = req.params.userId
|
||||
let { productId, increase, decrease } = req.body
|
||||
|
||||
Cart.getCartByUserId(userId, function (err, c) {
|
||||
if (err) return next(err)
|
||||
let oldCart = new CartClass(c[0] || { userId })
|
||||
// no cart save empty cart to database then return response
|
||||
if (c.length < 1 && !productId) {
|
||||
return Cart.createCart(oldCart.generateModel(), function (err, resultCart) {
|
||||
if (err) return next(err)
|
||||
return res.status(201).json({ cart: resultCart })
|
||||
})
|
||||
}
|
||||
Product.findById(productId, function (e, product) {
|
||||
if (e) {
|
||||
e.status = 406;
|
||||
return next(e);
|
||||
}
|
||||
if (product) {
|
||||
if (decrease) {
|
||||
oldCart.decreaseQty(product.id);
|
||||
} else if (increase) {
|
||||
oldCart.increaseQty(product.id);
|
||||
} else {
|
||||
oldCart.add(product, product.id);
|
||||
}
|
||||
let newCart = oldCart.generateModel()
|
||||
Cart.updateCartByUserId(
|
||||
userId,
|
||||
newCart,
|
||||
function (err, result) {
|
||||
if (err) return next(err)
|
||||
return res.status(200).json({ cart: result })
|
||||
})
|
||||
} else {
|
||||
// apply variant
|
||||
Variant.getVariantByID(productId, function (e, variant) {
|
||||
if (e) {
|
||||
e.status = 406;
|
||||
return next(e);
|
||||
}
|
||||
if (variant) {
|
||||
Product.getProductByID(variant.productID, function (e, p) {
|
||||
let color = (variant.color) ? "- " + variant.color : "";
|
||||
let size = (variant.size) ? "- " + variant.size : "";
|
||||
variant.title = p.title + " " + color + size
|
||||
variant.price = p.price
|
||||
if (decrease) {
|
||||
oldCart.decreaseQty(variant.id);
|
||||
} else if (increase) {
|
||||
oldCart.increaseQty(variant.id);
|
||||
} else {
|
||||
oldCart.add(variant, variant.id);
|
||||
}
|
||||
let newCart = oldCart.generateModel()
|
||||
Cart.updateCartByUserId(
|
||||
userId,
|
||||
newCart,
|
||||
function (err, result) {
|
||||
if (err) return next(err)
|
||||
res.status(200).json({ cart: result })
|
||||
})
|
||||
})
|
||||
}
|
||||
// no product and no variant find
|
||||
else {
|
||||
let err = new TypedError('/cart', 400, 'invalid_field', {
|
||||
message: "invalid request body"
|
||||
})
|
||||
return next(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
//PUT cart
|
||||
router.put('/:userId/cart', authenticate, function (req, res, next) {
|
||||
let userId = req.params.userId
|
||||
let requestProduct = req.body
|
||||
let { productId, color, size } = requestProduct.product
|
||||
|
||||
Cart.getCartByUserId(userId, function (err, c) {
|
||||
if (err) return next(err)
|
||||
let oldCart = new CartClass(c[0] || {})
|
||||
Product.getProductByID(productId, function (err, p) {
|
||||
if (err) return next(err)
|
||||
let newCart = oldCart.add(p, productId, { color, size })
|
||||
|
||||
//exist cart in databse
|
||||
if (c.length > 0) {
|
||||
Cart.updateCartByUserId(
|
||||
userId,
|
||||
{
|
||||
items: newCart.items,
|
||||
totalQty: newCart.totalQty,
|
||||
totalPrice: newCart.totalPrice,
|
||||
userId: userId
|
||||
},
|
||||
function (err, result) {
|
||||
if (err) return next(err)
|
||||
res.json(result)
|
||||
})
|
||||
} else {
|
||||
//no cart in database
|
||||
newCart = new Cart({
|
||||
items: newCart.items,
|
||||
totalQty: newCart.totalQty,
|
||||
totalPrice: newCart.totalPrice,
|
||||
userId: userId
|
||||
})
|
||||
Cart.createCart(newCart, function (err, resultCart) {
|
||||
if (err) return next(err)
|
||||
res.status(201).json(resultCart)
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
module.exports = router;
|
||||
|
||||
Reference in New Issue
Block a user