Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion client/app/products/productView/productView.controller.js
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -12,6 +12,7 @@ angular.module('stackStoreApp')
$scope.newReview.userId = user._id;
});
}
$scope.recommendations = [];
$scope.quantity = 1;
$scope.isAdmin = Auth.isAdmin;
$scope.reviews = [];
Expand All @@ -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) {
Expand Down
9 changes: 8 additions & 1 deletion client/app/products/productView/productView.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ <h3>{{product.price | currency}}</h3>
</form>
</div>
</div>

<div class="row">
<h4>Frequently purchased with:</h4>
<div class="col-lg-4 col-md-4 col-sm-4" ng-repeat="rec in recommendations">
<a href="/products/{{rec._id}}"><img ng-src="{{rec.images[0]}}" style="height: 50px" />
{{rec.name}}</a>
{{rec.price | currency}}
</div>
</div>
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-12">
<h3>Reviews</h3>
Expand Down
16 changes: 16 additions & 0 deletions engine-server/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
106 changes: 106 additions & 0 deletions engine-server/server.js
Original file line number Diff line number Diff line change
@@ -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);