diff --git a/client/app/products/productView/productView.controller.js b/client/app/products/productView/productView.controller.js index 41ef69a..9029f05 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(); @@ -12,6 +12,7 @@ angular.module('stackStoreApp') $scope.newReview.userId = user._id; }); } + $scope.recommendations = []; $scope.quantity = 1; $scope.isAdmin = Auth.isAdmin; $scope.reviews = []; @@ -20,6 +21,15 @@ angular.module('stackStoreApp') Product.get({id: $routeParams.id}, function(product) { $scope.product = product; + $http.post('http://localhost:3000/'+product._id) + .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) { // order.lineItems.forEach(function(lineItem) { diff --git a/client/app/products/productView/productView.html b/client/app/products/productView/productView.html index 2b6ba45..479aa8e 100644 --- a/client/app/products/productView/productView.html +++ b/client/app/products/productView/productView.html @@ -32,7 +32,14 @@

{{product.price | currency}}

- +
+

Frequently purchased with:

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

Reviews

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..e8a7858 --- /dev/null +++ b/engine-server/server.js @@ -0,0 +1,106 @@ +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){ + // 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) + // 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