-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackend.js
executable file
·165 lines (130 loc) · 3.43 KB
/
backend.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var cors = require('./cors');
var MongoClient = require('mongodb').MongoClient;
//configurar cabeceras http
app.use(cors.permisos);
//body parser
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
var url_mongo = "mongodb://193.146.116.148:27017/";
const pg = require('pg');
var data_pg = {
user: 'postgres',
host: '193.146.116.148',
database: 'ex_vote_dev',
password: 'postgres',
port: '5432'
};
const port = process.env.PORT || 3000;
app.delete('/requirement', function (req, res) {
MongoClient.connect(url_mongo, function(err, db) {
if (err) return res.status(500).json(err);
var queryReq = req.query;
var param = null;
if (queryReq.hasOwnProperty("requirement")){
param = queryReq.requirement;
console.log('Requirement to delete :' + param);
var query = {"status_id" : param};
const dbo = db.db('twitter_data');
dbo.collection('tweet').deleteOne((query),function(err) {
if (err) {
return res.status(500).json(err);
}
});
console.log("Requirement deleted");
db.close();
res.json({"result": "Delete"});
}else{
db.close();
res.json({"result": "Not found"});
}
});
})
app.delete('/requirements', function (req, res) {
MongoClient.connect(url_mongo, function(err, db) {
if (err) return res.status(500).json(err);
var queryReq = req.query;
var param = null;
if (queryReq.hasOwnProperty("account")){
param = queryReq.account;
var query = {"in_reply_to_screen_name" : param};
const dbo = db.db('twitter_data');
dbo.collection('tweet').remove((query),function(err) {
if (err) {
return res.status(500).json(err);
}
});
console.log("Requirements deleted");
db.close();
res.json({"result": "Delete"});
}else{
db.close();
res.json({"result": "Not found"});
}
});
})
app.delete('/edemocracyProject', function (req, res) {
const pool = new pg.Pool(data_pg);
var queryReq = req.query;
var project = null;
var id = null;
if (queryReq.hasOwnProperty("project")){
project = queryReq.project;
}
if (queryReq.hasOwnProperty("id")){
id = queryReq.id;
if(id == undefined){
id = null;
}
}
if(!id || !project){
return res.status(400).json({"result": "Incorrect params"});
}
pool.query("DELETE FROM projects where title='" + project + "'", (err, resp) => {
console.log(err, resp);
if (err) {
return res.status(500).json(err);
}
});
pool.query("DELETE FROM tickets where project_id=" + id, (err, resp) => {
console.log(err, resp);
if (err) {
return res.status(500).json(err);
}
});
pool.end();
res.json({"result": "Delete"});
})
app.get('/username', function (req, res) {
const pool = new pg.Pool(data_pg);
var queryReq = req.query;
var id = null;
var username = null;
if (queryReq.hasOwnProperty("id")){
id = queryReq.id;
}
if(!id){
return res.status(400).json({"result": "Incorrect params"});
}
pool.query("SELECT name FROM users where id=" + id, (err, result) => {
//console.log(err, res);
if (err) {
return res.status(500).json(err);
}
if(result){
username = result.rows[0].name;
console.log(username);
res.json({"username": username});
}
});
pool.end();
})
function uppercaseFirstLetter(string)
{
return string.charAt(0).toUpperCase() + string.slice(1);
}
app.listen(port, function () {
console.log('Example app listening on port ' + port);
});