Skip to content

Commit

Permalink
Completed get expenses
Browse files Browse the repository at this point in the history
  • Loading branch information
hemanth-s-a committed Mar 11, 2016
1 parent 26383ad commit ebfe5ce
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ sql.json
*.txt
*.log

server/logger.js
server/logger.js
web/
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"license": "MIT",
"dependencies": {
"angular": "~1.3.15",
"angular-route": "~1.3.15"
"angular-route": "~1.3.15",
"angular-smart-table": "^2.1.7"
}
}
5 changes: 4 additions & 1 deletion client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en" ng-app="moneyApp">
<head>
<meta charset="utf-8">
<title>AngularJS Skeleton</title>
<title>Money Management</title>
<link rel="stylesheet" href="styles/main.css">
<script src="/scripts/angular/angular.js"></script>
<script src="/scripts/angular-route/angular-route.js"></script>
Expand Down Expand Up @@ -32,6 +32,9 @@ <h1>Welcome to Money Management App</h1>
<td>
<a href="#/addExpense">Add expense</a>
</td>
<td>
<a href="#/getExpense">Expenses</a>
</td>
</tr>
</table>
</div>
Expand Down
12 changes: 12 additions & 0 deletions client/js/controllers/expenseController.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ function expenseController($scope, $location, expenseService, userStore) {
}
};

$scope.getExpenses = function() {
if(!self.userData.getId()) return;
expenseService.getExpenses({
"userId": self.userData.getId()
}).then(function(result) {
console.log(result);
$scope.expenseStore = result.data.expenses;
}, function(error) {
console.log(error);
});
};

$scope.$watch('expenseTypeId', function(newValue, oldValue) {
$scope.expenseSubType = null;
if (!$scope.expenseTypeId) {
Expand Down
5 changes: 5 additions & 0 deletions client/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ function($routeProvider) {
controller: 'expenseController'
})

.when('/getExpense', {
templateUrl: 'views/getExpenses.html',
controller: 'expenseController'
})

.when('/home', {
templateUrl: 'views/home.html'
})
Expand Down
10 changes: 10 additions & 0 deletions client/js/services/expenseService.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ function expenseService($http) {
}
});
};

this.getExpenses = function(data) {
return $http({
"method": "GET",
"url": "/expense",
"params": {
"userId": data.userId
}
});
};
};

expenseService.$inject = ['$http'];
Expand Down
4 changes: 4 additions & 0 deletions client/styles/main.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
body {
background-color: #eeeeee;
}

table, th, td {
border: 1px solid black;
}
13 changes: 13 additions & 0 deletions client/views/getExpenses.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<span>Expenses</span>
<table ng-init="getExpenses()">
<tr>
<th>Date</th>
<th>Description</th>
<th>Amount</th>
</tr>
<tr ng-repeat="expense in expenseStore">
<td>{{ expense.date }}</td>
<td>{{ expense.description }}</td>
<td>{{ expense.amount }}</td>
</tr>
</table>
2 changes: 2 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ app.use(bodyParser.json());
app.use("/scripts", express.static(__dirname + "/bower_components"));

app.use("/", express.static(__dirname + "/client"));
app.use("/web", express.static(__dirname + "/web"));

app.get("/test", test.getMe);

Expand All @@ -54,6 +55,7 @@ app.post("/login", login.login());
app.post("/register", register.register());

app.get("/expenseTypes", expenses.getExpenseTypes());
app.get("/expense", expenses.getExpenses());
app.post("/expense", expenses.saveExpense());

console.log("Starting...");
Expand Down
40 changes: 40 additions & 0 deletions server/expenses.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,25 @@ exports.saveExpense = function() {
};
};

exports.getExpenses = function() {
return function(request, response) {
var queryAndData = prepareGetExpense(request.query);
console.log("Getting expenses");
mysql.query(queryAndData.query, queryAndData.data, function(error, rows, fields) {
if (error) {
console.log(error);
console.error("Error getting expenses");
response.status(500).send("Server error");
} else {
console.log(rows);
response.status(200).send({
"expenses": rows
});
}
});
};
};

exports.getExpenseTypes = function() {
return function(request, response) {
var queryString, queryData = [];
Expand All @@ -46,6 +65,27 @@ exports.getExpenseTypes = function() {
};
};

function prepareGetExpense(parameters) {
var query, data = [];

query = 'SELECT * FROM Expenses WHERE';

if (!parameters.userId) {
return {
"query": ' user IS NULL',
"data": []
};
} else {
query += ' user=?';
data.push(parameters.userId);
}

return {
"query": query,
"data": data
};
};

function prepareGetExpenseTypes(subType) {
return subType ?
'SELECT * FROM ExpenseType WHERE parentId=?' :
Expand Down

0 comments on commit ebfe5ce

Please sign in to comment.