Creazione tabella Product

This commit is contained in:
Paolo Arena
2020-12-21 02:16:42 +01:00
parent 9ee59c0fc1
commit 67d2872e61
21 changed files with 767 additions and 42 deletions

67
src/server/models/cart.js Executable file
View 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
View 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
View 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 };

View File

@@ -165,6 +165,9 @@ const UserSchema = new mongoose.Schema({
subaccount: {
type: Boolean
},
cart: {
type: Object
},
profile: {
img: {
type: String

View 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)
}