Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
ExpenseTracker/ExpenseTracker/viewModel/FirebaseHelper.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
196 lines (156 sloc)
5.85 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using ExpenseTracker.model; | |
using Firebase.Database; | |
using Firebase.Database.Query; | |
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace ExpenseTracker.viewModel | |
{ | |
class FirebaseHelper | |
{ | |
FirebaseClient firebase = new FirebaseClient("https://expensetracker-cdaa4.firebaseio.com"); | |
public async Task<List<Expense>> GetAllExpense() | |
{ | |
return (await firebase | |
.Child("Expenses") | |
.OnceAsync<Expense>()).Select(item => new Expense | |
{ | |
expenseCategory = item.Object.expenseCategory, | |
narration = item.Object.narration, | |
amount=item.Object.amount, | |
id=item.Object.id, | |
}).Reverse().ToList(); | |
} | |
public async Task AddExpense(double amount, string narration, int expenseCategory, string id) | |
{ | |
await firebase | |
.Child("Expenses") | |
.PostAsync(new Expense() { | |
amount = amount, | |
expenseCategory= expenseCategory, | |
narration = narration, | |
id= id, | |
}); | |
} | |
public async Task<Expense> GetExpense(string expenseID) | |
{ | |
var allExpense = await GetAllExpense(); | |
await firebase | |
.Child("Expenses") | |
.OnceAsync<Expense>(); | |
return allExpense.Where(a => a.id == expenseID).FirstOrDefault(); | |
} | |
public async Task UpdatePerson(double amount, string narration, int expenseCategory, string id) | |
{ | |
var toUpdatePerson = (await firebase | |
.Child("Expenses") | |
.OnceAsync<Expense>()).Where(a => a.Object.id == id).FirstOrDefault(); | |
await firebase | |
.Child("Expenses") | |
.Child(toUpdatePerson.Key) | |
.PutAsync(new Expense() { | |
amount = amount, | |
expenseCategory = expenseCategory, | |
narration = narration, | |
}); | |
} | |
public async Task DeleteExpense(string expenseID) | |
{ | |
var toDeletePerson = (await firebase | |
.Child("Expenses") | |
.OnceAsync<Expense>()).Where(a => a.Object.id == expenseID).FirstOrDefault(); | |
await firebase.Child("Persons").Child(toDeletePerson.Key).DeleteAsync(); | |
} | |
//Category Firebase | |
public async Task<List<ExpenseCategory>> GetAllCategory() | |
{ | |
return (await firebase | |
.Child("Category") | |
.OnceAsync<ExpenseCategory>()).Select(item => new ExpenseCategory | |
{ | |
id = item.Object.id, | |
name= item.Object.name, | |
}).ToList(); | |
} | |
public async Task AddCategory(int id, string name) | |
{ | |
await firebase | |
.Child("Category") | |
.PostAsync(new ExpenseCategory() | |
{ | |
name=name, | |
id = id, | |
}); | |
} | |
public async Task<ExpenseCategory> GetCategory(int categoryID) | |
{ | |
var allExpense = await GetAllCategory(); | |
await firebase | |
.Child("Category") | |
.OnceAsync<ExpenseCategory>(); | |
return allExpense.Where(a => a.id == categoryID).FirstOrDefault(); | |
} | |
public async Task UpdateCategory(string name, int id) | |
{ | |
var toUpdatePerson = (await firebase | |
.Child("Category") | |
.OnceAsync<ExpenseCategory>()).Where(a => a.Object.id == id).FirstOrDefault(); | |
await firebase | |
.Child("Category") | |
.Child(toUpdatePerson.Key) | |
.PutAsync(new ExpenseCategory() | |
{ | |
id=id,name=name, | |
}); | |
} | |
public async Task DeleteCategoory(int categoryID) | |
{ | |
var toDeletePerson = (await firebase | |
.Child("Category") | |
.OnceAsync<ExpenseCategory>()).Where(a => a.Object.id == categoryID).FirstOrDefault(); | |
await firebase.Child("Category").Child(toDeletePerson.Key).DeleteAsync(); | |
} | |
//Queries | |
//getting total amount of expenses | |
public async Task<double> GetTotalAmountOfExpense() | |
{ | |
double totalAmount=0.0; | |
var allExpense = await GetAllExpense(); | |
for (int i= 0; i< allExpense.Count;i++) | |
{ | |
totalAmount += allExpense[i].amount; | |
} | |
return totalAmount; | |
} | |
//get total amount spent on each category | |
public async Task<List<CatergoryAmount>> GetTotalAmountOfExpensePerCategory() | |
{ | |
List<CatergoryAmount> categoryAmount = new List<CatergoryAmount>(); | |
var allCategories = await GetAllCategory(); | |
var allExpense = await GetAllExpense(); | |
for (int i = 0; i <= allCategories.Count; i++) | |
{ | |
double amount = 0; | |
var currentCategory = allCategories[i]; | |
for (int j = 0; j <= allExpense.Count; j++) | |
{ | |
var currentExpenses = allExpense[j]; | |
if (currentCategory.id==currentExpenses.expenseCategory) | |
{ | |
amount += currentExpenses.amount; | |
} | |
} | |
categoryAmount.Add(new CatergoryAmount | |
{ | |
amount = amount, | |
id = currentCategory.id, | |
categoryName=currentCategory.name, | |
}); | |
} | |
return categoryAmount; | |
} | |
} | |
} | |