Creazione tabella Product
This commit is contained in:
67
src/server/models/cart.js
Executable file
67
src/server/models/cart.js
Executable file
@@ -0,0 +1,67 @@
|
||||
const mongoose = require('mongoose');
|
||||
const Schema = mongoose.Schema;
|
||||
|
||||
const CartSchema = new Schema({
|
||||
userId: { type: Schema.Types.ObjectId, ref: 'User' },
|
||||
totalQty: { type: Number, default: 0 },
|
||||
totalPrice: { type: Number, default: 0 },
|
||||
items: [{
|
||||
item: { type: Schema.Types.ObjectId, ref: 'Product' },
|
||||
quantity: { type: Number, default: 1 },
|
||||
price: { type: Number, default: 0 }
|
||||
}]
|
||||
});
|
||||
|
||||
module.exports.getCartByUserId = function (uid, callback) {
|
||||
let query = { userId: uid }
|
||||
Cart.find(query, callback)
|
||||
}
|
||||
|
||||
module.exports.getCartById = function (id, callback) {
|
||||
Cart.findById(id, callback)
|
||||
}
|
||||
|
||||
module.exports.updateCartByUserId = function (userId, newCart, callback) {
|
||||
let query = { userId: userId }
|
||||
Cart.find(query, function (err, c) {
|
||||
if (err) throw err
|
||||
|
||||
//exist cart in databse
|
||||
if (c.length > 0) {
|
||||
Cart.findOneAndUpdate(
|
||||
{ userId: userId },
|
||||
{
|
||||
$set: {
|
||||
items: newCart.items,
|
||||
totalQty: newCart.totalQty,
|
||||
totalPrice: newCart.totalPrice,
|
||||
userId: userId
|
||||
}
|
||||
},
|
||||
{ new: true },
|
||||
callback
|
||||
)
|
||||
} else {
|
||||
//no cart in database
|
||||
newCart.save(callback)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
module.exports.updateCartByCartId = function (cartId, newCart, callback) {
|
||||
Cart.findById(
|
||||
{ _id: cartId },
|
||||
{
|
||||
$set: newCart
|
||||
},
|
||||
callback
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
|
||||
module.exports.createCart = function (newCart, callback) {
|
||||
newCart.save(callback)
|
||||
}
|
||||
|
||||
module.exports = mongoose.model('Cart', CartSchema);
|
||||
30
src/server/models/category.js
Executable file
30
src/server/models/category.js
Executable file
@@ -0,0 +1,30 @@
|
||||
const mongoose = require('mongoose');
|
||||
const Schema = mongoose.Schema;
|
||||
|
||||
mongoose.Promise = global.Promise;
|
||||
mongoose.level = "F";
|
||||
|
||||
|
||||
// Resolving error Unknown modifier: $pushAll
|
||||
mongoose.plugin(schema => {
|
||||
schema.options.usePushEach = true
|
||||
});
|
||||
|
||||
const CategorySchema = new Schema({
|
||||
name: {
|
||||
type: String,
|
||||
unique: true,
|
||||
index: true,
|
||||
lowercase: true
|
||||
}
|
||||
});
|
||||
|
||||
module.exports.getAllCategories = function (callback) {
|
||||
Category.find(callback)
|
||||
}
|
||||
|
||||
module.exports.getCategoryById = function (id, callback) {
|
||||
Category.findById(id, callback);
|
||||
}
|
||||
|
||||
module.exports = mongoose.model('Category', CategorySchema);
|
||||
115
src/server/models/product.js
Executable file
115
src/server/models/product.js
Executable file
@@ -0,0 +1,115 @@
|
||||
mongoose = require('mongoose');
|
||||
const Schema = mongoose.Schema;
|
||||
|
||||
const tools = require('../tools/general');
|
||||
|
||||
mongoose.Promise = global.Promise;
|
||||
mongoose.level = "F";
|
||||
|
||||
|
||||
// Resolving error Unknown modifier: $pushAll
|
||||
mongoose.plugin(schema => {
|
||||
schema.options.usePushEach = true
|
||||
});
|
||||
|
||||
const productSchema = new Schema({
|
||||
idapp: {
|
||||
type: String,
|
||||
},
|
||||
idProducer: {
|
||||
type: String
|
||||
},
|
||||
name: {
|
||||
type: String,
|
||||
},
|
||||
description: {
|
||||
type: String,
|
||||
},
|
||||
department: {
|
||||
type: String
|
||||
},
|
||||
category: {
|
||||
type: mongoose.Schema.Types.ObjectId, ref: 'Category'
|
||||
// type: String
|
||||
},
|
||||
price: {
|
||||
type: Number
|
||||
},
|
||||
color: {
|
||||
type: String
|
||||
},
|
||||
size: {
|
||||
type: String
|
||||
},
|
||||
quantity: {
|
||||
type: Number
|
||||
},
|
||||
date: {
|
||||
type: Number
|
||||
},
|
||||
icon: {
|
||||
type: String,
|
||||
},
|
||||
img: {
|
||||
type: String,
|
||||
},
|
||||
});
|
||||
|
||||
var Product = module.exports = mongoose.model('Product', productSchema);
|
||||
|
||||
module.exports.getFieldsForSearch = function () {
|
||||
return [{field: 'name', type: tools.FieldType.string}]
|
||||
};
|
||||
|
||||
module.exports.executeQueryTable = function (idapp, params) {
|
||||
params.fieldsearch = this.getFieldsForSearch();
|
||||
return tools.executeQueryTable(this, idapp, params);
|
||||
};
|
||||
|
||||
module.exports.findAllIdApp = async function (idapp) {
|
||||
const myfind = { idapp };
|
||||
|
||||
return await Product.find(myfind);
|
||||
};
|
||||
|
||||
module.exports.getAllProducts = function (query, sort, callback) {
|
||||
Product.find(query, null, sort, callback)
|
||||
}
|
||||
|
||||
module.exports.getProductByDepartment = function (query,sort, callback) {
|
||||
Product.find(query, null, sort, callback)
|
||||
}
|
||||
|
||||
module.exports.getProductByCategory = function (query,sort, callback) {
|
||||
Product.find(query, null, sort, callback)
|
||||
}
|
||||
|
||||
module.exports.getProductByTitle = function (query,sort, callback) {
|
||||
Product.find(query, null, sort, callback)
|
||||
}
|
||||
|
||||
module.exports.filterProductByDepartment = function (department, callback) {
|
||||
let regexp = new RegExp(`${department}`, 'i')
|
||||
var query = { department: { $regex: regexp } };
|
||||
Product.find(query, callback)
|
||||
}
|
||||
|
||||
module.exports.filterProductByCategory = function (category, callback) {
|
||||
let regexp = new RegExp(`${category}`, 'i')
|
||||
var query = { category: { $regex: regexp } };
|
||||
Product.find(query, callback);
|
||||
}
|
||||
|
||||
module.exports.filterProductByTitle = function (title, callback) {
|
||||
let regexp = new RegExp(`${title}`, 'i')
|
||||
var query = { title: { $regex: regexp } };
|
||||
Product.find(query, callback);
|
||||
}
|
||||
|
||||
module.exports.getProductByID = function (id, callback) {
|
||||
Product.findById(id, callback);
|
||||
}
|
||||
|
||||
// const Product = mongoose.model('Product', ProductSchema);
|
||||
|
||||
// module.exports = { Product };
|
||||
@@ -165,6 +165,9 @@ const UserSchema = new mongoose.Schema({
|
||||
subaccount: {
|
||||
type: Boolean
|
||||
},
|
||||
cart: {
|
||||
type: Object
|
||||
},
|
||||
profile: {
|
||||
img: {
|
||||
type: String
|
||||
|
||||
40
src/server/models/variant.js
Normal file
40
src/server/models/variant.js
Normal file
@@ -0,0 +1,40 @@
|
||||
|
||||
var mongoose = require('mongoose');
|
||||
|
||||
var variantSchema = mongoose.Schema({
|
||||
productID: {
|
||||
type: String
|
||||
},
|
||||
imagePath: {
|
||||
type: String
|
||||
},
|
||||
color: {
|
||||
type: String
|
||||
},
|
||||
size: {
|
||||
type: String
|
||||
},
|
||||
quantity: {
|
||||
type: Number
|
||||
},
|
||||
title: {
|
||||
type: String
|
||||
},
|
||||
price: {
|
||||
type: Number
|
||||
}
|
||||
});
|
||||
|
||||
var Variant = module.exports = mongoose.model('Variant', variantSchema);
|
||||
|
||||
module.exports.getVariantByID = function(id, callback){
|
||||
Variant.findById(id, callback);
|
||||
}
|
||||
|
||||
module.exports.getVariantProductByID = function(id, callback){
|
||||
var query = {productID: id};
|
||||
Variant.find(query, callback);
|
||||
}
|
||||
module.exports.getAllVariants = function(callback){
|
||||
Variant.find(callback)
|
||||
}
|
||||
Reference in New Issue
Block a user