-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateUser.js
151 lines (130 loc) · 3.38 KB
/
updateUser.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
var express = require('express')
// instantiating the Router middle. essentially middleware composition
var router = express.Router()
var formatedDate = require('date-format');
// set matched user to false.
let match = false;
// would be set with the match object.
let matchObject = "";
// array user information objects
let users = [
{
id : 1,
name : "mikko",
age : 20,
height: "80cm",
profession: "trader"
},
{
id : 2,
name : "jenni",
age : 25,
height: "80cm",
profession: "Accountant"
}
]
// Displays the time the request was made
// This would be executed first.
// Passes the execution phase to the next handle
let rquestDate = function requestDate(req, res, next) {
console.log('You viewed the page at ' + formatedDate('hh:mm', new Date() ) + ' on ' + formatedDate('dd.MM.yy', new Date() ));
next();
}
var updateName = function (id,name){
users.map(function(user){
if(id == user.id){
user.name = name;
//user.age = age;
//user.height = height;
//user.profession = profession;
matchObject = user;
match = true;
}
})
}
var updateNameAge = function (id,name,age){
users.map(function(user){
if(id == user.id){
user.name = name;
user.age = age;
//user.height = height;
//user.profession = profession;
matchObject = user;
match = true;
}
})
}
var updateNameAgeHeight = function (id,name,age,height){
users.map(function(user){
if(id == user.id){
user.name = name;
user.age = age;
user.height = height;
//user.profession = profession;
matchObject = user;
match = true;
}
})
}
var updateNameAgeHeightProfession = function (id,name,age,height,profession){
users.map(function(user){
if(id == user.id){
user.name = name;
user.age = age;
user.height = height;
user.profession = profession;
matchObject = user;
match = true;
}
})
}
// Handler on the
router.get('/:id/name/:name', rquestDate , function (req, res) {
updateName(req.params.id,req.params.name/*,req.params.age,req.params.height,req.params.profession*/);
if(match){
console.log("user is found! ...");
res.json(matchObject);
match = false;
}
else {
console.log("user not found! ...");
res.send("user with Id: " + req.params.id + "not found! ...")
}
})
router.get('/:id/name/:name/age/:age', rquestDate , function (req, res) {
updateNameAge(req.params.id,req.params.name,req.params.age);
if(match){
console.log("user is found! ...");
res.json(matchObject);
match = false;
}
else {
console.log("user not found! ...");
res.send("user with Id: " + req.params.id + "not found! ...")
}
})
router.get('/:id/name/:name/age/:age/height/:height', rquestDate , function (req, res) {
updateNameAgeHeight(req.params.id,req.params.name,req.params.age,req.params.height);
if(match){
console.log("user is found! ...");
res.json(matchObject);
match = false;
}
else {
console.log("user not found! ...");
res.send("user with Id: " + req.params.id + "not found! ...")
}
})
router.get('/:id/name/:name/age/:age/height/:height/profession/:profession', rquestDate , function (req, res) {
updateNameAgeHeightProfession(req.params.id,req.params.name,req.params.age,req.params.height,req.params.profession);
if(match){
console.log("user is found! ...");
res.json(matchObject);
match = false;
}
else {
console.log("user not found! ...");
res.send("user with Id: " + req.params.id + "not found! ...")
}
})
module.exports = router