-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
110 lines (94 loc) · 3.03 KB
/
app.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
const BidModel = require('./models/Bid.js');
const EventModel = require('./models/Event.js');
const express = require('express');
const mongoose = require('mongoose');
const app = express();
const port = 3000;
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static('assets'));
mongoose.connect('mongodb+srv://prikolica:[email protected]/bids');
app.get('/', (req, res) => res.sendFile('views/index.html' , { root : __dirname}));
app.get('/bid/1', (req, res) => res.sendFile('views/single-1.html' , { root : __dirname}));
app.get("/api/events", function (req, res) {
EventModel.find().exec(function (error, events) {
res.json({ data: events });
});
});
app.get("/api/bids", function (req, res) {
BidModel.find().exec(function (error, bids) {
res.json({ data: bids });
});
});
app.post("/api/bids", function (req, res) {
var bid = new BidModel(req.body);
bid.save(function () {
res.json(bid);
});
});
app.get("/api/bids/:id", function (req, res) {
BidModel.findOne({_id: req.params.id}).exec(function (error, bid) {
if (bid) {
res.json(bid);
} else {
res.json({ error: "Not Found" });
}
});
});
app.put("/api/bids/:id", function (req, res) {
if (!req.body || !req.body.customer || !req.body.value) {
res.json({ error: "Missing required parameters" });
return;
}
BidModel.findOne({ _id: req.params.id}).exec(function (error, bid) {
if (bid) {
if (req.body.value <= bid.highest_bid) {
res.json({ error: "Invalid bid value" });
return;
}
var event = new EventModel({
customer: req.body.customer,
action: "bid_place",
data: {
bid: req.params.id,
value: req.body.value,
},
});
event.save();
bid.customer = req.body.customer;
bid.highest_bid = req.body.value;
bid.save();
res.json(bid);
} else {
res.json({ error: "Not Found" });
}
});
});
// Update the bid object
app.post("/api/bids/:id", function (req, res) {
BidModel.findOne({_id: req.params.id}).exec(function (error, bid) {
if (bid) {
var body = req.body;
for (var key in body) {
bid[key] = body[key];
}
bid.save(function () {
res.json(bid);
});
} else {
res.json({error: "Not Found"});
}
});
});
app.delete("/api/bids/:id", function (req, res) {
BidModel.findOne({_id: req.params.id}).exec(function (error, bid) {
if (bid) {
bid.delete(function () {
res.json({ deleted: true });
});
} else {
res.json({ error: "Not Found" });
}
});
});
app.listen(port, () => console.log(`Example app listening on port ${port}!`));