1 /*******************************************************************************
2 * Copyright (c) 2017 Kumar Rishabh and others.
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Apache License, Version 2.0
6 * which accompanies this distribution, and is available at
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *******************************************************************************/
10 var express = require('express');
11 var router = express.Router();
12 var async = require('async');
15 var renderer = function(res, err, results) {
17 res.render('search_projects', { title: 'Express', json: results });
20 var get_tags = function(result, callback) {
21 db_pool.getConnection(function(err, connection) {
22 sql_query = 'select tag_name from tag where tag_id in (select tag_id from vnf_tags where vnf_id = ' + result['vnf_id'] + ') limit 5';
23 // TODO find why it works and not above
24 connection.query(sql_query, function (error, results, fields) {
26 result['tags'] = results;
27 callback(null, result);
28 //connection.release();
29 if (error) throw error;
35 var get_images = function(result, callback) {
36 db_pool.getConnection(function(err, connection) {
37 sql_query = 'select photo_url from photo where photo_id = ' + result['photo_id'];
38 // TODO find why it works here and not when declared outside the method
39 console.log(sql_query);
40 connection.query(sql_query, function (error, results, fields) {
41 console.log(results[0].photo_url);
42 result['photo_url'] = results[0].photo_url;
43 callback(null, result);
44 //connection.release();
45 if (error) throw error;
50 var sql_data = function(tags, renderer, res) {
51 var tag_array = "\'" + tags.map(function (item) { return item; }).join("\',\'") + "\'";
52 console.log(tag_array);
54 db_pool.getConnection(function(err, connection) {
55 sql_query = 'select tag_id from tag where tag_name in (' + tag_array + ')';
56 connection.query(sql_query, function (error, results, fields) {
57 condition = 'SELECT * FROM vnf as v';
58 for (var i in results) {
59 condition += (i == 0) ? ' WHERE ' : ' AND ';
60 condition += 'v.vnf_id IN (SELECT vnf_id from vnf_tags where tag_id = ' + results[i]['tag_id'] + ')';
63 connection.query(condition, function (error, results, fields) {
65 async.map(results, get_images, function(error, results) {
66 async.map(results, get_tags, renderer.bind(null, res));
68 //connection.release();
69 if (error) throw error;
73 if (error) throw error;
79 router.get('/', function(req, res) {
81 console.log(typeof(req.param('tags')));
82 var tags = req.param('tags');
85 tags = tags.toLowerCase().split(/[ ,]+/);
87 sql_data(tags, renderer, res);
89 res.render('search_projects', { title: 'Express', json: false});
94 module.exports = router;