Fix all the config files alignment issues
[samplevnf.git] / VNF_Catalogue / routes / index.js
1 /*******************************************************************************
2  * Copyright (c) 2017 Kumar Rishabh and others.
3  *
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  *******************************************************************************/
9
10 var express = require('express');
11 var router = express.Router();
12 var async = require('async');
13
14 var renderer = function(res, err, results) {
15     console.log(results);
16     res.render('index', { title: 'Express', json: results });
17 }
18
19 var get_images = function(result, callback) {
20     db_pool.getConnection(function(err, connection) {
21         sql_query = 'select photo_url from photo where photo_id = ' + result['photo_id'];
22         // TODO find why it works here and not when declared outside the method
23         console.log(sql_query);
24         connection.query(sql_query, function (error, results, fields) {
25             console.log(results[0].photo_url);
26             //result['photo_url'] = results[0].photo_url;
27             connection.release();
28             if (error) {
29                 result['photo_url'] = false;
30                 //throw error;
31             } else {
32                 result['photo_url'] = results[0].photo_url;
33             }
34             callback(null, result);
35             //if (error) throw error;
36         });
37     });
38 }
39
40 /* GET VNF_Catalogue Home Page. */
41 router.get('/', function(req, res) {
42     db_pool.getConnection(function(err, connection) {
43         sql_query = 'select * from vnf order by lines_of_code desc limit 8';
44         // TODO find why it works and not above
45         connection.query(sql_query, function (error, results, fields) {
46             //console.log(results);
47             connection.release();
48             if (error) {
49                 res.render('index', { title: 'Express', json: false});
50             } else {
51                 async.map(results, get_images, renderer.bind(null, res));
52                 //res.render('index', { title: 'Express', json: results});    
53             }
54         });
55     });
56 });
57
58 module.exports = router;