From 81b5508210626309eb20401ab9718635538b65dc Mon Sep 17 00:00:00 2001 From: Chuck Pierce and Melissa Moy Date: Wed, 21 Jan 2015 02:23:25 -0500 Subject: [PATCH 1/2] send arr of ids of 3 most commonly co-occurring products --- .../productView/productView.controller.js | 7 +- engine-server/package.json | 16 +++ engine-server/server.js | 103 ++++++++++++++++++ 3 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 engine-server/package.json create mode 100644 engine-server/server.js diff --git a/client/app/products/productView/productView.controller.js b/client/app/products/productView/productView.controller.js index 41ef69a..4b1318a 100644 --- a/client/app/products/productView/productView.controller.js +++ b/client/app/products/productView/productView.controller.js @@ -1,7 +1,7 @@ 'use strict'; angular.module('stackStoreApp') - .controller('ProductViewCtrl', function ($scope, Product, Auth, Order, Review, User, $routeParams, Cart, $sce) { + .controller('ProductViewCtrl', function ($scope, Product, Auth, Order, Review, User, $routeParams, Cart, $sce, $http) { $scope.cart; // why? whyyyyyyyyyyyyyyyy $scope.user = Auth.getCurrentUser(); @@ -20,6 +20,11 @@ angular.module('stackStoreApp') Product.get({id: $routeParams.id}, function(product) { $scope.product = product; + // shhhh + $http.post('http://localhost:3000/'+product._id) + .then(function(){ + console.log("hello"); + }) // this doesn't do anything yet bc orders, man // $scope.user.orders.forEach(function(order) { // order.lineItems.forEach(function(lineItem) { diff --git a/engine-server/package.json b/engine-server/package.json new file mode 100644 index 0000000..06c104b --- /dev/null +++ b/engine-server/package.json @@ -0,0 +1,16 @@ +{ + "name": "engine-server", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "start": "nodemon ./server.js" + }, + "author": "", + "license": "ISC", + "dependencies": { + "body-parser": "^1.10.1", + "express": "^4.11.0", + "mongoose": "^3.8.21" + } +} diff --git a/engine-server/server.js b/engine-server/server.js new file mode 100644 index 0000000..9bb86dd --- /dev/null +++ b/engine-server/server.js @@ -0,0 +1,103 @@ +var express = require('express'); +var bodyParser = require('body-parser'); +var mongoose = require('mongoose'); +var Schema = mongoose.Schema; + +var ProductSchema = new Schema({ + name: {type: String, required: true}, + price: {type: Number, required: true}, + description: {blurb: {type: String, required: true}, full: {type: String, required: true}}, + categories: [{ type: Schema.Types.ObjectId, ref: 'Category', index: true}], + images: {type: Array, default: ['http://lorempixel.com/400/400']}, + reviews: [{ type: Schema.Types.ObjectId, ref: 'Review' }], + quantity: Number // quantity available--needs to increment when order placed +}); + +var Product = mongoose.model('Product', ProductSchema); + +var lineItemsSchema = new Schema({ + productId: String, + productName: String, + price: Number, + quantity: Number, + image: {type: String, default:'http://lorempixel.com/400/400'} +}); +var states = 'created processing processing_guest cancelled cancelled_guest completed completed_guest'.split(' '); + +var OrderSchema = new Schema({ + userId: {type: mongoose.Schema.Types.ObjectId, ref: 'User'}, + lineItems: {type:[lineItemsSchema], required:true }, + status: {type: String, default:'created', enum: states}, + date: Date, + shipping: {}, + billing: {} // includes chargeId +}); + +var Order = mongoose.model('Order', OrderSchema); + +// let's set up mongoose +mongoose.connect('mongodb://localhost/stackstore-dev'); +mongoose.connection.on('error', console.error.bind(console, 'connection error:')); + + +var server = express(); + +server.use(bodyParser.json()); +server.use(bodyParser.urlencoded({extended: false})); + +// allow cross-domain +server.use(function(req, res, next) { + res.header('Access-Control-Allow-Origin', '*'); + res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE'); + res.header('Access-Control-Allow-Headers', 'Content-Type'); + next(); +}); + +var obj = {}; +Order.find(function(err, orders) { + orders.forEach(function(order) { + // each item id is a key in the hash + // each item id is a key in that key's value, e.g. + // {id1: {id2: val, id3: val...}, id2: {id1: val, id3: val...}} + // increment values for the object + // go through the rest of the goddamn objects in the line item. + order.lineItems.forEach(function(item1, outerIndex) { + if (!obj[item1.productId]) obj[item1.productId] = {}; + order.lineItems.forEach(function (item2, innerIndex) { + if (item1.productId !== item2.productId) { + if (!obj[item1.productId][item2.productId]) { + obj[item1.productId][item2.productId] = 1 + } + else { + obj[item1.productId][item2.productId]++; + } + } + }); + }); + }); + // console.log(obj); +}); + +server.post('/:id', function(req, res) { + Product.findById(req.params.id, function(err, product){ + var highestValues = [{num: 0}, {num: 0}, {num: 0}]; + // go to that item's section of the hash (obj[product._id]) + // go through each entry in that section (by key) + // and see if its value (number of co-occurrences) is higher than + // any of the values currently in the "highestValues" array. + for (var key in obj[product._id]) { + for (var i = 0; i < 3; i++) { + if (obj[product._id][key] > highestValues[i]['num']) { + highestValues[i] = {productId: key, num: obj[product._id][key]} + break; + } + } + } + var ids = [highestValues[0].productId, highestValues[1].productId, highestValues[2].productId]; + console.log(ids); + // console.log(highestValues); + res.send(ids); + }); +}); + +server.listen(3000); \ No newline at end of file From 5fd26bd07161782e500dcbd48a6d410094f8f017 Mon Sep 17 00:00:00 2001 From: Chuck Pierce and Melissa Moy Date: Wed, 21 Jan 2015 10:47:28 -0500 Subject: [PATCH 2/2] three recs on product view --- client/app/main/main.controller.js | 19 +++++++++++++++++-- .../productView/productView.controller.js | 11 ++++++++--- .../app/products/productView/productView.html | 9 ++++++++- engine-server/server.js | 5 ++++- server/config/environment/development.js | 2 +- 5 files changed, 38 insertions(+), 8 deletions(-) diff --git a/client/app/main/main.controller.js b/client/app/main/main.controller.js index 5aa2699..e8b5abc 100644 --- a/client/app/main/main.controller.js +++ b/client/app/main/main.controller.js @@ -1,9 +1,24 @@ 'use strict'; angular.module('stackStoreApp') - .controller('MainCtrl', function ($scope, $http, Product, Category) { + .controller('MainCtrl', function ($scope, $http, Product, Category, Auth, Cart, User) { $scope.products = Product.query(); - $scope.categories = Category.query(); + $scope.user = Auth.getCurrentUser(); + if ($scope.user.$promise) { + $scope.user.$promise.then(function(user) { + if (user.provider == 'google') { + if (user.__v == 0) { + user.__v++; + Cart.startAuthCart(user._id); + User.update(user); + Cart.mergeCarts(); + } + else { + Cart.mergeCarts(user._id); + } + } + }); + } }); diff --git a/client/app/products/productView/productView.controller.js b/client/app/products/productView/productView.controller.js index 4b1318a..9029f05 100644 --- a/client/app/products/productView/productView.controller.js +++ b/client/app/products/productView/productView.controller.js @@ -12,6 +12,7 @@ angular.module('stackStoreApp') $scope.newReview.userId = user._id; }); } + $scope.recommendations = []; $scope.quantity = 1; $scope.isAdmin = Auth.isAdmin; $scope.reviews = []; @@ -20,10 +21,14 @@ angular.module('stackStoreApp') Product.get({id: $routeParams.id}, function(product) { $scope.product = product; - // shhhh $http.post('http://localhost:3000/'+product._id) - .then(function(){ - console.log("hello"); + .then(function(res){ + var ids = res.data; + angular.forEach(ids, function(id) { + Product.get({id: id}).$promise.then(function(prod){ + $scope.recommendations.push(prod); + }) + }); }) // this doesn't do anything yet bc orders, man // $scope.user.orders.forEach(function(order) { diff --git a/client/app/products/productView/productView.html b/client/app/products/productView/productView.html index ffeab4f..57ae14a 100644 --- a/client/app/products/productView/productView.html +++ b/client/app/products/productView/productView.html @@ -33,7 +33,14 @@

BUY YOUR TIME NOW!

- +
+

Frequently purchased with:

+
+ + {{rec.name}} + {{rec.price | currency}} +
+

Reviews

diff --git a/engine-server/server.js b/engine-server/server.js index 9bb86dd..e8a7858 100644 --- a/engine-server/server.js +++ b/engine-server/server.js @@ -80,6 +80,9 @@ Order.find(function(err, orders) { server.post('/:id', function(req, res) { Product.findById(req.params.id, function(err, product){ + // console.log(obj); + // console.log(product._id); + // console.log(obj[product._id]); var highestValues = [{num: 0}, {num: 0}, {num: 0}]; // go to that item's section of the hash (obj[product._id]) // go through each entry in that section (by key) @@ -94,7 +97,7 @@ server.post('/:id', function(req, res) { } } var ids = [highestValues[0].productId, highestValues[1].productId, highestValues[2].productId]; - console.log(ids); + // console.log(ids); // console.log(highestValues); res.send(ids); }); diff --git a/server/config/environment/development.js b/server/config/environment/development.js index 43075cc..098951d 100644 --- a/server/config/environment/development.js +++ b/server/config/environment/development.js @@ -8,5 +8,5 @@ module.exports = { uri: 'mongodb://localhost/stackstore-dev' }, - seedDB: true + seedDB: false };