Fix all the config files alignment issues
[samplevnf.git] / VNF_Catalogue / routes / add_project.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 multer = require('multer');
13 var async = require('async');
14
15 var max_size = 1 * 1000 * 1000; // image size_limit
16
17 var storage =   multer.diskStorage({
18   destination: function (req, file, callback) {
19     callback(null, './public/uploads');
20   },
21   filename: function (req, file, callback) {
22     console.log(file);
23     console.log(req.body);
24     callback(null, file.fieldname + '-' + Date.now() + '.png');
25   }
26 });
27
28 var fileFilter = function (req, file, cb) {
29   if (file.mimetype !== 'image/png') {
30     //req.fileValidationError = 'goes wrong on the mimetype';
31     cb(null, false);
32   } else {
33     cb(null, true);
34   }
35 }
36
37 var upload = multer({ fileFilter: fileFilter, limits: { fileSize: max_size }, storage : storage}).single('file_upload');
38
39 var renderer = function(res, error, results) {
40     //res.render('project_profile', { title: 'Express', json: results });
41     if(error) {
42       res.status(500);
43       res.send({'error': 'Adding VNF did not succeed'});
44       return;
45     } else {
46       res.end('{"success" : "Updated Successfully", "status" : 200}');
47       return;
48     }
49 }
50
51
52 var add_vnf_name_tag = function(vnf_name, vnf_id, cb) {
53   db_pool.getConnection(function(err, connection) {
54     sql_query = 'INSERT INTO tag(tag_name, is_vnf_name) values(\'' + vnf_name + '\', 1)\;SELECT LAST_INSERT_ID() tag_id';
55     connection.query(sql_query, function (error, results, fields) {
56         tag_id = results[1][0].tag_id;
57         connection.release();
58         if(error) {
59             cb(null, error, -1, -1);
60         } else {
61             cb(null, null, vnf_id, tag_id);
62         }
63       });
64   });
65 }
66
67 var add_vnf_tag_relationship = function(err, vnf_id, tag_id, cb) {
68   console.log('here');
69   console.log(err); console.log(vnf_id); console.log(tag_id); console.log(cb);
70   if(err) cb(null, err, -1); // err propagated from add_vnf_name_tag
71   db_pool.getConnection(function(err, connection) {
72     sql_query = 'INSERT INTO vnf_tags(tag_id, vnf_id) values(' + tag_id + ', ' + vnf_id + ')\;';
73     console.log(sql_query);
74     connection.query(sql_query, function (error, results, fields) {
75         connection.release();
76         console.log('here');
77         if(error) {
78             cb(null, error, -1);
79         } else {
80             cb(null, null, results);
81         }
82       });
83   });
84 }
85
86
87 router.post('/', function(req, res) {
88   upload(req,res,function(err) {
89         console.log(req.body);
90         console.log(req.file)
91         if(req.file == null && req.body['file_url'] != '') {
92             response = 'File Upload error: wrong Filetype, only png supported as of now';
93             res.status(500);
94             res.end(JSON.stringify({'error': response}));
95             return;
96         }
97         console.log(err);
98         if(err) {
99             console.log(err);
100             response = 'File Upload error: ' + err;
101             console.log(response);
102             //return res.end(req.fileValidationError);
103             res.status(500);
104             res.end(JSON.stringify({'error': response}));
105             console.log('here here here here');
106             return;
107         }
108         console.log('here here');
109
110         console.log(req.file);
111         req.body['photo_url'] = (req.file) ? req.file['filename'] : 'logo.png';
112         console.log(req.body);
113
114         req.checkBody("vnf_name", "VNF Name must not be empty").notEmpty();
115         req.checkBody("repo_url", "Repository URL must not be empty").notEmpty();
116         req.checkBody("license", "Please select a License").notEmpty();
117         req.checkBody("opnfv_indicator", "Please select an OPNFV Indicator").notEmpty();
118         //req.checkBody("repo_url", "Must be a Github URL").matches('.*github\.com.*');
119
120         var errors = req.validationErrors();
121         console.log(errors);
122
123         var response = '';  for(var i = 0; i < errors.length; i++) {
124             console.log(errors[i]['msg']);
125             response = response + errors[i]['msg'] + '; ';
126         }
127
128         if(errors) {    res.status(500);
129             res.send({'error': response});
130             return;
131         }
132
133         req.body['vnf_name'] = req.body['vnf_name'].toLowerCase();
134
135         var vnf_details = req.body;
136         delete vnf_details.file_url;
137
138         db_pool.getConnection(function(err, connection) {
139             // Use the connection
140
141           sql_query = 'INSERT INTO photo(photo_url) values(\'' + req.body['photo_url'] + '\')\;SELECT LAST_INSERT_ID() photo_id';
142           // TODO look above query prone to sql_injections
143
144           console.log(sql_query);
145           connection.query(sql_query, function (error, results, fields) {
146              console.log('hola');
147              console.log(results[1][0].photo_id);
148              //connection.query(sql_query, vnf_details, function (error, results, fields) {
149              delete vnf_details.photo_url;
150              vnf_details['photo_id'] = results[1][0].photo_id;
151              sql_query = 'INSERT INTO vnf SET ?; select last_insert_id() vnf_id;'
152
153              connection.query(sql_query, vnf_details, function (error, results, fields) {
154                 // And done with the connection.
155                 connection.release();
156                 //if (error) throw error;
157                 if(error) {
158                     res.status(500);
159                     res.send({'error': 'Adding VNF did not succeed'});
160                     return;
161                 } else {
162
163                         console.log(results);
164                         console.log(results[1][0].vnf_id);
165                         vnf_id = results[1][0].vnf_id;
166                         async.waterfall([                                                          
167                           async.apply(add_vnf_name_tag, vnf_details['vnf_name'], vnf_id),
168                           add_vnf_tag_relationship,
169                         ], renderer.bind(null, res));
170
171                     // Handle error after the release.
172                     //res.end('{"success" : "Updated Successfully", "status" : 200}');
173                     //return;
174                 }
175                 // Don't use the connection here, it has been returned to the pool.
176             });
177           });
178         });
179
180
181   });
182
183 });
184
185 module.exports = router;