-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
executable file
·220 lines (185 loc) · 7.01 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
var express = require('express');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
//A connection is a fairly standard wrapper around a database connection.
mongoose.connect('mongodb://localhost/stockExchangeMarket');
var app = express();
var logger = require('./logger');
app.use(logger);
// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(express.static('public'));
// define buyOrder Schema
var buyOrderSchema = mongoose.Schema({
timeStamp: Date,
size: Number,
price: Number,
company: { type: mongoose.Schema.Types.ObjectId, ref: 'Companies' }
});
// define saleOrder Schema
var saleOrderSchema = mongoose.Schema({
timeStamp: Date,
size: Number,
price: Number,
company: { type: mongoose.Schema.Types.ObjectId, ref: 'Companies'}
});
//define transaction Schema
var transactionSchema = mongoose.Schema({
timeStamp: Date,
size: Number,
price: Number,
company: { type: mongoose.Schema.Types.ObjectId, ref: 'Companies' }
});
// define company Schema
var companySchema = mongoose.Schema({
name: String,
symbolURL: String,
openPrice: { type: Number, default: 0 },
currentPrice: { type: Number, default: 0 },
changeValue: { type: Number, default: 0 },
changeIcon: { type: String, default: 'images/noChange.png' },
changePercentage: { type: Number, default: 0 },
changeDirection: { type: Number, default: 0 },
shareVolume: { type: Number, default: 0 },
buyOrders: [{ type: mongoose.Schema.Types.ObjectId, ref: 'BuyOrders' }],
saleOrders: [{ type: mongoose.Schema.Types.ObjectId, ref: 'SaleOrders' }],
transactions: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Transactions' }] //to save the transaction when we find a deal
});
/* compile company model - instances of models are document.
* Now I have companys model or document
* A model is an object that gives you easy access to a named collection.
*/
var Companies = mongoose.model('Companies', companySchema);
var BuyOrders = mongoose.model('BuyOrders', buyOrderSchema);
var SaleOrders = mongoose.model('SaleOrders', saleOrderSchema);
var Transactions = mongoose.model('Transactions', transactionSchema);
// compile buyOrders model
// Get all the companies
app.get('/companies', function (request, response) {
Companies.find(function (error, companies) {
if (error) response.send(error);
response.json({companies: companies});
});
});
// Get all the buy orders
app.get('/buyOrders', function (request, response) {
BuyOrders.find(function (error, buyOrders) {
if (error) response.send(error);
response.json({BuyOrders: buyOrders});
});
});
// Get all teh sale orders
app.get('/saleOrders', function (request, response) {
SaleOrders.find(function (error, saleOrders) {
if (error) response.send(error);
response.json({saleOrders: saleOrders});
});
});
// Create a company
app.post('/companies', function (request, response) {
var company = new Companies (request.body.company);
// save the company
company.save(function (error) {
if (error) response.send(error);
response.status(201).json({Companies: company});
});
});
// Create a buy orders
app.post('/buyOrders', function (request, response) {
var buyOrder = new BuyOrders (request.body.buyOrder);
//console.log(request.body);
//console.log(request.body.buyOrder.company);
Companies.findById(request.body.buyOrder.company, function (error, company) {
if (error) response.send(error);
company.buyOrders.push(buyOrder);
company.save();
});
// save the buyOrder
buyOrder.save(function (error) {
if (error) response.send(error);
response.status(201).json({BuyOrders: buyOrder});
});
});
// Create a sale orders
app.post('/saleOrders', function (request, response) {
var saleOrder = new SaleOrders (request.body.saleOrder);
//console.log(request.body);
//console.log(request.body.saleOrder.company);
Companies.findById(request.body.saleOrder.company, function (error, company) {
if (error) response.send(error);
company.saleOrders.push(saleOrder);
company.save();
});
// save the buyOrder
saleOrder.save(function (error) {
if (error) response.send(error);
response.status(201).json({SaleOrders: saleOrder});
});
});
// Create a transcation
app.post('/transactions', function (request, response) {
console.log(request.body);
var transaction = new Transactions (request.body.transaction);
Companies.findById(request.body.transaction.company, function (error, company) {
if (error) response.send(error);
company.transactions.push(transaction);
company.save();
});
// save the buyOrder
transaction.save(function (error) {
if (error) response.send(error);
response.status(201).json({Transactions: transaction});
});
});
// Update a company with new info.
app.put('/companies/:company_id', function(request, response) {
Companies.findById(request.params.company_id, function(error, company) {
if (error) response.send(error);
//console.log(company);
//undate the company information
company.currentPrice = request.body.company.currentPrice;
company.changeValue = request.body.company.changeValue;
company.changeIcon = request.body.company.changeIcon;
company.changePercentage = request.body.company.changePercentage;
company.changeDirection = request.body.company.changeDirection;
company.shareVolume = request.body.company.shareVolume;
//company.buyOrders = request.body.company.buyOrders;
//company.saleOrders = request.body.company.saleOrders;
//company.transaction = request.body.company.transactions;
// save the company information
company.save(function(error) {
if (error) response.send(error);
response.status(201).json({companies: company});
});
});
});
// Delete a buy order
app.delete('/buyOrders/:buyOrder_id', function (request, response) {
BuyOrders.remove({
_id: request.params.buyOrder_id
}, function(error, buyOrder) {
if (error) response.send(err);
response.status(201).json({BuyOrders: buyOrder});
});
});
// Delete a sale order
app.delete('/saleOrders/:saleOrder_id', function (request, response) {
SaleOrders.remove({
_id: request.params.saleOrder_id
}, function(error, saleOrder) {
if (error) response.send(err);
response.status(201).json({SaleOrders: saleOrder});
});
});
// Get a company with matching company_id
app.get('/companies/:company_id', function(request, response) {
Companys.findById(request.params.company_id, function (error, company) {
if (error || !company) response.send(error);
response.json({companies: company});
});
});
app.listen(3700, function () {
console.log('Listening on port 3700');
});