forked from NotHarshhaa/Cloud-Native-DevOps-Project
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTransactionService.js
More file actions
63 lines (55 loc) · 1.84 KB
/
TransactionService.js
File metadata and controls
63 lines (55 loc) · 1.84 KB
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
const dbcreds = require('./DbConfig');
const mysql = require('mysql2'); // Change to mysql2
const con = mysql.createConnection({
host: process.env.DB_HOST || dbcreds.DB_HOST,
user: process.env.DB_USER || dbcreds.DB_USER,
password: process.env.DB_PWD || dbcreds.DB_PWD,
database: process.env.DB_DATABASE || dbcreds.DB_DATABASE
});
function addTransaction(amount,desc){
var mysql = `INSERT INTO \`transactions\` (\`amount\`, \`description\`) VALUES ('${amount}','${desc}')`;
con.query(mysql, function(err,result){
if (err) throw err;
//console.log("Adding to the table should have worked");
})
return 200;
}
function getAllTransactions(callback){
var mysql = "SELECT * FROM transactions";
con.query(mysql, function(err,result){
if (err) throw err;
//console.log("Getting all transactions...");
return(callback(result));
});
}
function findTransactionById(id,callback){
var mysql = `SELECT * FROM transactions WHERE id = ${id}`;
con.query(mysql, function(err,result){
if (err) throw err;
console.log(`retrieving transactions with id ${id}`);
return(callback(result));
})
}
function deleteAllTransactions(callback){
var mysql = "DELETE FROM transactions";
con.query(mysql, function(err,result){
if (err) throw err;
//console.log("Deleting all transactions...");
return(callback(result));
})
}
function deleteTransactionById(id, callback){
var mysql = `DELETE FROM transactions WHERE id = ${id}`;
con.query(mysql, function(err,result){
if (err) throw err;
console.log(`Deleting transactions with id ${id}`);
return(callback(result));
})
}
module.exports = {
addTransaction,
getAllTransactions,
findTransactionById,
deleteAllTransactions,
deleteTransactionById
};