VNF_Catalogue Codebase
[samplevnf.git] / VNF_Catalogue / routes / add_tag.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 router.post('/', function(req, res) {
14   console.log(req.body);
15   req.checkBody("tag_name", "TAG Name must not be empty").notEmpty();
16
17   var errors = req.validationErrors();
18   console.log(errors);
19
20   var response = '';  for(var i = 0; i < errors.length; i++) {
21     console.log(errors[i]['msg']);
22     response = response + errors[i]['msg'] + '; ';
23   }
24
25   if(errors) {  res.status(500);
26     res.send({'error': response});
27     return;
28   }
29
30   req.body['tag_name'] = req.body['tag_name'].toLowerCase();
31   var tag_details = req.body;
32
33   db_pool.getConnection(function(err, connection) {
34     // Use the connection
35     sql_query = 'INSERT INTO tag SET ?'
36     connection.query(sql_query, tag_details, function (error, results, fields) {
37         // And done with the connection.
38         // Handle error after the release.
39         console.log('here');
40         if(error) {
41             res.end('{"error" : "Adding tag did not succeed", "status" : 500}');
42             connection.release();
43             return;
44         } else {
45             res.end('{"success" : "Updated Successfully", "status" : 200}'); 
46             connection.release();
47             return;
48         }
49         //if (error) throw error;
50         // Don't use the connection here, it has been returned to the pool.
51     });
52   });
53
54
55   //res.end('{"success" : "Updated Successfully", "status" : 200}');
56   //return;
57
58 });
59
60 module.exports = router;