Skip to content

Instantly share code, notes, and snippets.

@nivesh2
Last active December 21, 2015 17:49
Show Gist options
  • Save nivesh2/6343288 to your computer and use it in GitHub Desktop.
Save nivesh2/6343288 to your computer and use it in GitHub Desktop.
hotel search API code Node.js
exports.findhotel = function(req, res) {
var q = [];
var flag = 0;
var queryString = req.query.id;
var idx = hotel_array.state.indexOf(queryString);
while (idx != -1) {
flag = 1;
q.push(hotel[idx]);
idx = hotel_array.state.indexOf(queryString, idx + 1);
}
if (q.length === 0 && flag === 0) {
console.log("hotel name loop");
idx = hotel_array.name.indexOf(queryString);
while (idx != -1) {
flag = 1;
q.push(hotel[idx]);
idx = hotel_array.name.indexOf(queryString, idx + 1);
}
if (q.length === 0 && flag === 0) {
var temp = new RegExp("[ ,-]" + queryString + "([ ,-]|$)", "i");
console.log("city loop");
for (var i = 0; i < hotel_array.address.length; i++) {
if (hotel_array.address[i].match(temp)) {
q.push(hotel[i]);
}
}
}
if (q.length === 0) {
res.send(404, 'No hotel found');
}
}
console.log("exit loop" + q.length);
res.json(q);
};
exports.findagent = function(req, res) {
var q = [];
var flag = 0;
var queryString = req.query.id;
var idx = agent_array.state.indexOf(queryString);
console.log(queryString);
while (idx != -1) {
flag = 1;
q.push(agent[idx]);
idx = agent_array.state.indexOf(queryString, idx + 1);
}
if (q.length === 0 && flag === 0) {
console.log('city loop');
idx = agent_array.city.indexOf(queryString);
while (idx != -1) {
flag = 1;
q.push(agent[idx]);
idx = agent_array.city.indexOf(queryString, idx + 1);
console.log('data pushed ');
}
if (q.length === 0 && flag === 0) {
console.log('name loop');
idx = agent_array.name.indexOf(queryString);
while (idx != -1) {
flag = 1;
q.push(agent[idx]);
idx = agent_array.name.indexOf(queryString, idx + 1);
}
}
if (q.length === 0) {
res.send(404, 'No hotel found');
}
}
console.log('returning data');
res.json(q);
};
exports.findhotel = function(req, res) {
var q = [];
var len = hotel.length;
var queryString = req.query.id;
var hotel_obj = function(obj, i) {
this.name = obj.name;
this.address = obj.address;
this.state = obj.state;
this.phone = obj.phone;
this.fax = obj.fax;
this.website = obj.website;
this.type = obj.type;
this.room = obj.room;
this.email = obj.email;
this.city = city[i];
this.hoteljson = true;
};
var temp = new RegExp("[ ,-]" + queryString + "([ ,-]|$)", "i");
var name_temp = new RegExp(queryString + "([ ,-]|$)", "i");
for (var i = 0; i < len; i++) {
try {
if (hotel_array.address[i].match(temp)) {
var newObj = new hotel_obj(hotel[i], i);
q.push(newObj);
continue;
} else if (hotel_array.name[i].match(name_temp)) {
var newObj = new hotel_obj(hotel[i], i);
q.push(newObj);
continue;
}
} catch (err) {
console.log("Catch error: " + err);
}
}
if (q.length === 0)
res.send(404, 'No hotel found');
res.json(q);
};
exports.findagent = function(req, res) {
var q = [];
var agent_obj = function(obj) {
this.name = obj.name;
this.address = obj.address;
this.state = obj.state;
this.phone = obj.phone;
this.fax = obj.fax;
this.email = obj.email;
this.website = obj.website;
this.type = obj.type;
this.region = obj.region;
this.person = obj.person;
this.city = obj.city;
this.hoteljson = false;
};
var len = agent.length;
var queryString = req.query.id;
var temp = new RegExp('[ ,-]' + queryString + '([ ,-]|$)', 'i');
var name_temp = new RegExp(queryString + "([ ,]|$)", "i");
for (var i = 0; i < len; i++) {
try {
if (agent_array.city[i].match(name_temp)) {
var newObj = new agent_obj(agent[i]);
q.push(newObj);
continue;
} else if (agent_array.state[i].match(name_temp)) {
var newObj = new agent_obj(agent[i]);
q.push(newObj);
continue;
} else if (agent_array.name[i].match(name_temp)) {
var newObj = new agent_obj(agent[i]);
q.push(newObj);
continue;
} else if (agent_array.region[i].match(name_temp)) {
var newObj = new agent_obj(agent[i]);
q.push(newObj);
continue;
} else if (agent_array.type[i].match(name_temp)) {
var newObj = new agent_obj(agent[i]);
q.push(newObj);
continue;
} else if (agent_array.address[i].match(temp)) {
var newObj = new agent_obj(agent[i]);
q.push(newObj);
continue;
}
} catch (err) {
console.log("Catch error: " + err);
}
}
if (q.length === 0)
res.send(404, 'No hotel found');
res.json(q);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment