-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
169 lines (129 loc) · 5.04 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
"use strict"
// server.js
// BASE SETUP
// =============================================================================
//TO-DO:create mongo util
// call the packages we need
var express = require('express'); // call express
var app = express(); // define our app using express
var bodyParser = require('body-parser');
var Receipt = require('./models/receipt');
var mongoose = require('mongoose');
mongoose.connect('mongodb://user:[email protected]:19220/receipts',function (err,db) {
if(err){
console.log("Error connecting");
process.exit(1);
}
console.log("Connected to Mongo");
}); // connect to our database
// 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());
var port = process.env.PORT || 8080; // set our port
// ROUTES FOR OUR API
// =============================================================================
var router = express.Router(); // get an instance of the express Router
router.use(function(req, res, next){
console.log('Request is being made');
next();
});
// test route to make sure everything is working (accessed at GET http://localhost:8080/api)
router.get('/', function(req, res) {
res.json({ message: 'hooray! welcome to our api!' });
});
router.route('/receipts')
// create a receipt (accessed at POST http://localhost:8080/api/receipts)
.post(function(req, res) {
var receipt = new Receipt();
// create a new instance of the receipt model
receipt.name = req.body.name;
receipt.address = req.body.address;
receipt.receiptNumber = req.body.receiptNumber;
receipt.receiptDate = req.body.receiptDate;
receipt.total = req.body.total;
receipt.item = [];
// var item = req.body.item;
// var item = {"name":"","quantity":""};
// item.name = "Oil";
// item.quantity = "5";
for (let i in req.body.item) {
let item = req.body.item[i];
let itemObj = { name: item['name'], quantity: item['quantity'], unitPrice: item['unitPrice'], amount: item['amount'] };
receipt.item.push(itemObj);
}
// set the receipts name (comes from the request)
// save the receipt and check for errors
receipt.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Receipt created!' });
});
})
// get all the receipts (accessed at GET http://localhost:8080/api/receipts)
.get(function(req, res) {
var receipt = new Receipt();
Receipt.find(function(err, receipts) {
if (err)
res.send(err);
res.json(receipts);
});
});
router.route('/receipts/:receipt_id')
// get the receipt with that id (accessed at GET http://localhost:8080/api/receipts/:receipt_id)
.get(function(req, res) {
Receipt.findById(req.params.receipt_id, function(err, receipt) {
if (err)
res.send(err);
res.json(receipt);
});
})
.put(function(req, res) {
// use our receipt model to find the receipt we want
Receipt.findById(req.params.receipt_id, function(err, receipt) {
if (err)
res.send(err);
// save the receipt
if (receipt !== null){
// update the receipts info
receipt.name = req.body.name;
receipt.address = req.body.address;
receipt.receiptNumber = req.body.receiptNumber;
receipt.receiptDate = req.body.receiptDate;
receipt.total = req.body.total;
receipt.item = [];
for (let i in req.body.item) {
let item = req.body.item[i];
let itemObj = { name: item['name'], quantity: item['quantity'], unitPrice: item['unitPrice'], amount: item['amount'] };
receipt.item.push(itemObj);
}
receipt.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'receipt updated!' });
});
}
else
{
res.send('No Such item with that id');
}
});
})
// delete the receipt with this id (accessed at DELETE http://localhost:8080/api/receipts/:receipt_id)
.delete(function(req, res) {
Receipt.remove({
_id: req.params.receipt_id
}, function(err, receipt) {
if (err)
res.send(err);
res.json({ message: 'Successfully deleted' });
});
});
// more routes for our API will happen here
// REGISTER OUR ROUTES -------------------------------
// all of our routes will be prefixed with /api
app.use('/api', router);
// START THE SERVER
// =============================================================================
app.listen(port);
console.log('Magic happens on port ' + port);