-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
145 lines (125 loc) · 3.76 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
var express = require('express');
var bodyParser = require('body-parser');
var db = require('./db.js');
var _ = require('lodash');
// var cors = require('cors');
var app = express();
// app.use(cors());
var PORT = process.env.PORT || 3000;
app.use(bodyParser.json());
//********************CORS Fix Attempt*****************************************************
app.all('*', function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
});
app.get('/', function(req, res, next) {
// Handle the get for this route
});
app.post('/', function(req, res, next) {
// Handle the post for this route
});
//****************************************************************************************
app.get('/login', function(req, res){
var userId = _.pick(req.body, 'userId');
var password = _.pick(req.body, 'password');
db.user.findOne({where: user.id == userId}).then(function(user) {
if (user.password == password) {
res.json(user);
} else {
res.json("Wrong password");
}
}, function (e) {
res.json(e);
});
});
app.post('/users', function(req,res) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
var user = _.pick(req.body, 'name' , 'email', 'password', 'pi', 'pgi');
var sites = _.pick(req.body, 'sites').sites;
var faceIds = _.pick(req.body, 'faceIds').faceIds;
var userId;
db.user.create(user).then(function (user) {
userId = user.get('id'); //userId is used to link user to child objects
//Promise chain calls class methods create_faceIds and create_sites in database
db.site.create_sites(sites, userId).then(function(){
db.faceId.create_faceIds(faceIds, userId).then(function(){
res.json(user.toJSON());
},function(err) {
console.log('Error adding FaceId ' + index + ' to user');
res.status(400).json(err);
});
},function(err) {
console.log('Error adding Site ' + index + ' to user');
res.status(400).json(err);
});
}, function (err) {
console.log('Error adding user' + user.name);
res.status(400).json(err);
});
});
app.put('/users/sites/:id', function(req, res) {
var user = _.pick(req.body, 'name' , 'email', 'password', 'pi', 'pgi');
var sites = _.pick(req.body, 'sites').sites;
var faceIds = _.pick(req.body, 'faceIds').faceIds;
var userId = parseInt(req.params.id, 10);
var attributes = {};
var appendants = {};
if (user.hasOwnProperty('name')) {
attributes.name = z.name;
}
if (user.hasOwnProperty('email')) {
attributes.email = user.email;
}
if (user.hasOwnProperty('password')) {
attributes.password = user.password;
}
if (user.hasOwnProperty('sites')) {
appendants.sites = user.sites;
}
if (user.hasOwnProperty('faceIds')) {
appendants.faceIds = user.faceIds;
}
db.user.findOne({
where: {
id: userId,
}
}).then(function(user){
if(user) {
user.update(attributes).then(function(user) {
if(appendants.sites) {
user.sites.concat(appendants.sites);
}
if(appendants.faceIds) {
user.faceIds.concat(appendants.faceIds);
}
return user.reload();
}).then(function(user){
res.json(user);
}, function(e) {
res.status(500);
});
}else {
res.status(404);
}
})
});
//Only meant for testing database in development
app.get("/users", function(req, res) {
db.user.findAll().then(function(users){
res.json(users);
}), function(e) {
res.status(e);
}
});
//Initialize and synchronize the database
db.sequelize.sync({
force: false
}).then(function() {
//Tell Express.js Server to start listening for requests on a port
app.listen(PORT, function() {
console.log('Express listening on port ' + PORT + '!');
});
});