forked from NotHarshhaa/Cloud-Native-DevOps-Project
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
105 lines (94 loc) · 3.48 KB
/
index.js
File metadata and controls
105 lines (94 loc) · 3.48 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
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
const transactionService = require('./TransactionService');
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const os = require('os');
const fetch = require('node-fetch');
const moment = require('moment');
const app = express();
const port = 8080;
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cors());
// ROUTES FOR OUR API
// =======================================================
//Health Checking
app.get('/health',(req,res)=>{
res.json("This is the health check using CICD");
});
// ADD TRANSACTION
app.post('/transaction', (req,res)=>{
var response = "";
try{
t=moment().unix()
console.log("{ \"timestamp\" : %d, \"msg\", \"Adding Expense\", \"amount\" : %d, \"Description\": \"%s\" }", t, req.body.amount, req.body.desc);
var success = transactionService.addTransaction(req.body.amount,req.body.desc);
if (success = 200) res.json({ message: 'added transaction successfully'});
}catch (err){
res.json({ message: 'something went wrong', error : err.message});
}
});
// GET ALL TRANSACTIONS
app.get('/transaction',(req,res)=>{
try{
var transactionList = [];
transactionService.getAllTransactions(function (results) {
//console.log("we are in the call back:");
for (const row of results) {
transactionList.push({ "id": row.id, "amount": row.amount, "description": row.description });
}
t=moment().unix()
console.log("{ \"timestamp\" : %d, \"msg\" : \"Getting All Expenses\" }", t);
console.log("{ \"expenses\" : %j }", transactionList);
res.statusCode = 200;
res.json({"result":transactionList});
});
}catch (err){
res.json({message:"could not get all transactions",error: err.message});
}
});
//DELETE ALL TRANSACTIONS
app.delete('/transaction',(req,res)=>{
try{
transactionService.deleteAllTransactions(function(result){
t=moment().unix()
console.log("{ \"timestamp\" : %d, \"msg\" : \"Deleted All Expenses\" }", t);
res.statusCode = 200;
res.json({message:"delete function execution finished."})
})
}catch (err){
res.json({message: "Deleting all transactions may have failed.", error:err.message});
}
});
//DELETE ONE TRANSACTION
app.delete('/transaction/id', (req,res)=>{
try{
//probably need to do some kind of parameter checking
transactionService.deleteTransactionById(req.body.id, function(result){
res.statusCode = 200;
res.json({message: `transaction with id ${req.body.id} seemingly deleted`});
})
} catch (err){
res.json({message:"error deleting transaction", error: err.message});
}
});
//GET SINGLE TRANSACTION
app.get('/transaction/id',(req,res)=>{
//also probably do some kind of parameter checking here
try{
transactionService.findTransactionById(req.body.id,function(result){
res.statusCode = 200;
var id = result[0].id;
var amt = result[0].amount;
var desc= result[0].desc;
res.json({"id":id,"amount":amt,"desc":desc});
});
}catch(err){
res.json({message:"error retrieving transaction", error: err.message});
}
});
app.listen(port, () => {
t=moment().unix()
console.log("{ \"timestamp\" : %d, \"msg\" : \"App Started on Port %s\" }", t, port)
})
//