d1a3d726edcacf663af459a20bb3b14214a61515
[releng.git] / utils / test / vnfcatalogue / 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');
32   var vnf_name = req.param('vnf_name');
33
34   db_pool.getConnection(function(err, connection) {
35     // Use the connection
36     //sql_query = 'INSERT INTO tag SET ?'
37     sql_query = 'insert into vnf_tags(vnf_id, tag_id) values ((select vnf_id from vnf where vnf_name = \'' + vnf_name + '\'), (select tag_id from tag where tag_name = \'' + tag_name + '\'))';
38     console.log(sql_query);
39     connection.query(sql_query, function (error, results, fields) {
40         // And done with the connection.
41
42         connection.release();
43         res.end('{"success" : "Updated Successfully", "status" : 200}');
44
45         // Handle error after the release.
46         if (error) throw error;
47         // Don't use the connection here, it has been returned to the pool.
48     });
49   });
50
51   res.end('{"success" : "Updated Successfully", "status" : 200}');
52   //res.render('vnf_tag_association', { title: 'Express' });
53 });
54
55 module.exports = router;