-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
134 lines (114 loc) · 3.75 KB
/
Copy pathserver.js
File metadata and controls
134 lines (114 loc) · 3.75 KB
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
var express = require('express'),
http = require('http'),
mongoose = require('mongoose'),
bodyparser = require('body-parser'),
app;
app = express();
http.createServer(app).listen(3000);
var CardSchema = mongoose.Schema({
"rank": String,
"suit": String,
"id": Number
});
app.use(express.urlencoded());
app.use(bodyparser.urlencoded({extended: false}));
app.use(bodyparser.json());
mongoose.connect('mongodb://localhost/pokerhand', function(err) {
useMongoClient: true;
if (err) { console.log(err) }
});
var Card = mongoose.model('Card', CardSchema);
app.post('/hands', function(req, res){
var handid;
var status = true;
handid = generateID();
req.body.forEach(function(c) {
var card = new Card();
card.rank = c.rank;
card.suit = c.suit;
card.id = handid;
card.save(function(err){
if(err !== null){
console.log(err);
status = false;
} else{
//console.log("Card Saved!");
}
});
});
if(status){
console.log("Hand Saved Successfully");
res.status(200).json({"id": handid });
} else{
res.status(500).send("Internal Error");
}
});
app.get('/hands/:handid', function(req, res){
var id = parseInt(req.params.handid);
var handResult = {};
var Card = mongoose.model('Card', CardSchema);
Card.find({'id': id}, '-_id rank suit', function(err, card){
if(err){
console.log("ERROR: " + err);
res.status(500).send("Internal Error");
} else if ( card.length !== 0){
console.log(card);
handResult.id = id;
handResult.cards = card;
res.status(200).json(handResult);
} else{
console.log("Data Not Find!");
res.status(404).send("Data Not Found!");
}
});
});
app.get('/hands/:handid/cards', function(req, res){
var id = parseInt(req.params.handid);
var handResult = {};
var Card = mongoose.model('Card', CardSchema);
Card.find({'id': id}, '-_id rank suit', function(err, card){
if(err){
console.log("ERROR: " + err);
res.status(500).send("Internal Error");
} else if ( card.length !== 0){
console.log(card);
res.status(200).json(card);
} else{
console.log("Data Not Find!");
res.status(404).send("Data Not Found!");
}
});
});
app.put('/hands/:handid', function(req, res){
var id = parseInt(req.params.handid);
var Card = mongoose.model('Card', CardSchema);
Card.remove({'id': id}, function(err, done){
if(err){
console.log(" ERROR in removing!!! " + err);
res.status(500).send("Internall Error in PUT Operation (Phase 1)");
} else if (done.result.n === 0){
console.log("Hand Not Found.");
res.status(404).send("Cannot find any hand with specified handId.");
} else{
console.log('Phase 1 of PUT operation did successfully.');
req.body.forEach(function(h) {
var card = new Card();
card.rank = h.rank;
card.suit = h.suit;
card.id = id;
card.save(function(err, done){
if(err !== null){
console.log(err);
res.status(500).send("Internall Error in PUT Operation (Phase 2)");
} else{
console.log("Data Updated Successfully");
}
});
});
res.status(204).send("Data Updated Successfully");
}
});
});
function generateID(){
return Date.now();
}