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('project_profile', { title: 'Express', json: results });
20 var renderer_post = function(res, err, results) {
22 res.end(JSON.stringify(results));
25 var get_tags = function(result, callback) {
26 db_pool.getConnection(function(err, connection) {
27 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';
28 // TODO find why it works and not above
29 connection.query(sql_query, function (error, results, fields) {
31 result['tags'] = results;
37 result['tags'] = results;
39 callback(null, result);
40 //if (error) throw error;
46 var get_images = function(result, callback) {
47 db_pool.getConnection(function(err, connection) {
48 sql_query = 'select photo_url from photo where photo_id = ' + result['photo_id'];
49 // TODO find why it works here and not when declared outside the method
50 console.log(sql_query);
51 connection.query(sql_query, function (error, results, fields) {
52 console.log(results[0].photo_url);
53 //result['photo_url'] = results[0].photo_url;
56 result['photo_url'] = false;
59 result['photo_url'] = results[0].photo_url;
61 callback(null, result);
62 //if (error) throw error;
67 var sql_data = function(vnf_id, renderer, res) {
68 db_pool.getConnection(function(err, connection) {
69 sql_query = 'select * from vnf where vnf_id = '+ vnf_id;
70 connection.query(sql_query, function (error, results, fields) {
74 console.log('connection error occurred');
76 async.map(results, get_images, function(error, results) {
77 async.map(results, get_tags, renderer.bind(null, res));
80 //connection.release();
81 //if (error) throw error;
87 router.get('/', function(req, res) {
88 console.log(typeof(req.param('vnf_id')));
89 var vnf_id = req.param('vnf_id');
92 //tags = tags.toLowerCase().split(/[ ,]+/);
94 sql_data(vnf_id, renderer, res);
96 res.render('project_profile', { title: 'Express', json: false});
100 router.post('/', function(req, res) {
101 console.log(typeof(req.param('vnf_id')));
102 var vnf_id = req.param('vnf_id');
105 //tags = tags.toLowerCase().split(/[ ,]+/);
107 sql_data(vnf_id, renderer_post, res);
109 res.end('{"error" : "VNF Project could not get loaded", "status" : 500}');
115 module.exports = router;