-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·70 lines (60 loc) · 2.16 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
var ecommerce_store_artifacts = require('./build/contracts/EcommerceStore.json')
var contract = require('truffle-contract')
var Web3 = require('Web3')
var provider = new Web3.providers.HttpProvider("http://localhost:8545");
var EcommerceStore = contract(ecommerce_store_artifacts);
EcommerceStore.setProvider(provider);
//Mongoose setup to interact with the mongodb database
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
var ProductModel = require('./product');
mongoose.connect("mongodb://localhost:27017/ebay_dapp");
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
// Express server which the frontend with interact with
var express = require('express');
var app = express();
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.listen(3000, function() {
console.log('Ebay Ethereum server listening on port 3000!');
});
function setupProductEventListner() {
let productEvent;
EcommerceStore.deployed().then(function(i) {
productEvent = i.NewProduct({fromBlock: 0, toBlock: 'latest'});
productEvent.watch(function(err, result) {
if (err) {
console.log(err)
return;
}
console.log("Have Watch response")
saveProduct(result.args);
});
})
}
setupProductEventListner();
function saveProduct(product) {
ProductModel.findOne({ 'blockchainId': product._productId.toLocaleString() }, function (err, dbProduct) {
if (dbProduct != null) {
console.log("Found dbProduct " + product._productId);
return;
}
var p = new ProductModel({name: product._name, blockchainId: product._productId, category: product._category,
ipfsImageHash: product._imageLink, ipfsDescHash: product._descLink, auctionStartTime: product._auctionStartTime,
auctionEndTime: product._auctionEndTime, price: product._startPrice, condition: product._productCondition,
productStatus: 0});
p.save(function (err) {
if (err) {
handleError(err);
} else {
ProductModel.count({}, function(err, count) {
console.log("count is " + count);
})
}
});
})
}