forked from jmg2107/wedding-chart
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
79 lines (63 loc) · 1.74 KB
/
Copy pathserver.js
File metadata and controls
79 lines (63 loc) · 1.74 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
var express = require('express');
var db = require('./server/config');
var bodyParser = require('body-parser');
var Guest = require('./server/models/guest');
var Guests = require('./server/collections/guest');
var app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(express.static(__dirname + '/public/client/'));
app.get('/', function(req,res){
console.log('Do something');
});
app.post('/api/guests/', function(req, res){
console.log("inputting new name: ", req.body.name);
var name = req.body.name;
new Guest({name : name})
.fetch()
.then(function(user) {
if (!user) {
var newGuest = new Guest({
name: name,
tableId: 0
});
newGuest.save()
.then(function(newUser) {
Guests.add(newUser);
});
} else {
user.destroy()
.then(function(){
console.log("Removed guest");
})
}
res.status(200).end();
});
});
app.get('/api/guests/', function(req, res){
Guests.reset().fetch().then(function(guests) {
res.status(200).send(guests.models);
});
});
app.post('/api/table/', function(req, res){
console.log("inputting new table ", req.body.data);
req.body.data.forEach(function(obj){
var name = obj.name;
var table = obj.tableId
console.log("looking for ", name);
new Guest({name : name})
.fetch()
.then(function(guest){
if(guest){
guest.save({tableId : table}, {patch: true})
.then(function(changed){
console.log("changed model ", changed);
});
} else {
console.log('Guest not found!');
}
});
});
res.status(200).end();
});
module.exports = app;