Fix warning when compiling with ninja
[samplevnf.git] / VNF_Catalogue / routes / vnf_tag_association.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
13 /* Post controller for VNF_TAG Association */
14 router.post('/', function(req, res) {
15   req.checkBody("tag_name", "TAG Name must not be empty").notEmpty();
16   req.checkBody("vnf_name", "VNF Name must not be empty").notEmpty();
17
18   var errors = req.validationErrors();
19   console.log(errors);
20
21   var response = '';  for(var i = 0; i < errors.length; i++) {
22     console.log(errors[i]['msg']);
23     response = response + errors[i]['msg'] + '; ';
24   }
25
26   if(errors) {  res.status(500);
27     res.send({'error': response});
28     return;
29   }
30
31   var tag_name = req.param('tag_name').toLowerCase();
32   var vnf_name = req.param('vnf_name').toLowerCase();
33
34   db_pool.getConnection(function(err, connection) {
35     sql_query = 'select vnf_tag_id from vnf_tags where vnf_id = (select vnf_id from vnf where vnf_name = \'' + vnf_name + '\' limit 1) and tag_id = (select tag_id from tag where tag_name = \'' + tag_name + '\' and is_vnf_name = 0 limit 1)';
36     console.log(sql_query);
37     connection.query(sql_query, function (error, results, fields) {
38         console.log(results);
39         if (results.length >= 1) {
40             connection.release();
41             res.status(500);
42             res.send('{"error" : "Tag VNF connection already exists...."}');
43             return;
44         } else {
45             // Use the connection
46             //sql_query = 'INSERT INTO tag SET ?'
47             sql_query = 'insert into vnf_tags(vnf_id, tag_id) values ((select vnf_id from vnf where vnf_name = \'' + vnf_name + '\' limit 1), (select tag_id from tag where tag_name = \'' + tag_name + '\' and is_vnf_name = 0 limit 1))';
48             console.log(sql_query);
49             connection.query(sql_query, function (error, results, fields) {
50                 // And done with the connection.
51
52                 connection.release();
53
54                 // Handle error after the release.
55                 if (error) {
56                     console.log(error);
57                     res.status(500);
58                     res.send({'error' : 'Adding vnf tag association did not succeed, Check names(rely on autocomplete)'});
59                     return;
60                 } else {
61                     res.end('{"success" : "Updated Successfully", "status" : 200}');
62                     return;
63                 }
64                 // Don't use the connection here, it has been returned to the pool.
65             });
66         }
67     });
68
69   });
70
71   //res.end('{"success" : "Updated Successfully", "status" : 200}');
72   //res.render('vnf_tag_association', { title: 'Express' });
73 });
74
75 module.exports = router;