Skip to content
Permalink
a080bf85e0
Switch branches/tags

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?
Go to file
 
 
Cannot retrieve contributors at this time
234 lines (195 sloc) 7.58 KB
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");
//get all Expenses ; specifies a list of all the existing expenses for the user
public async Task<List<Expense>> GetAllExpense(String usermail)
{
return (await firebase
.Child("Expenses")
.OnceAsync<Expense>()).Where(a=>a.Object.email==usermail).Select(item => new Expense
{
expenseCategory = item.Object.expenseCategory,
narration = item.Object.narration,
amount=item.Object.amount,
id=item.Object.id,
email=item.Object.email,
}).Reverse().ToList();
}
//all New Expense; adds new expenses
public async Task AddExpense(double amount, string narration, int expenseCategory, string id, String email)
{
await firebase
.Child("Expenses")
.PostAsync(new Expense() {
amount = amount,
expenseCategory= expenseCategory,
narration= narration,
id= id,
email=email,
});
}
//get Details of a particular expense
public async Task<Expense> GetExpense(string expenseID, String email)
{
var allExpense = await GetAllExpense(email);
await firebase
.Child("Expenses")
.OnceAsync<Expense>();
return allExpense.Where(a => a.id == expenseID).FirstOrDefault();
}
//Update an Expense
public async Task UpdateExpense(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,
});
}
//Deleting an Expense
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
//get all categories; specifies a list of all existing categories of expenses per user
public async Task<List<ExpenseCategory>> GetAllCategory(String useremail)
{
return (await firebase
.Child("Category")
.OnceAsync<ExpenseCategory>()).Where(a => a.Object.email == useremail).Select(item => new ExpenseCategory
{
id = item.Object.id,
name= item.Object.name,
}).ToList();
}
//add new category
public async Task AddCategory(int id, string name, string email)
{
await firebase
.Child("Category")
.PostAsync(new ExpenseCategory()
{
name=name,
id = id,
email= email
});
}
//get a new category
public async Task<ExpenseCategory> GetCategory(int categoryID, string email)
{
var allExpense = await GetAllCategory(email);
await firebase
.Child("Category")
.OnceAsync<ExpenseCategory>();
return allExpense.Where(a => a.id == categoryID).FirstOrDefault();
}
//update a new category
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,
});
}
//delete a category
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 all expenses
public async Task<double> GetTotalAmountOfExpense(string email)
{
double totalAmount=0.0;
var allExpense = await GetAllExpense(email);
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(string email)
{
List<CatergoryAmount> categoryAmount = new List<CatergoryAmount>();
var allCategories = await GetAllCategory(email);
var allExpense = await GetAllExpense(email);
foreach (var category in allCategories) // Outer loop
{
// Code that should run for each collection
double amt = 0;
foreach (var expense in allExpense) // Inner loop
{
// Code that should run for each item
if (category.id == expense.expenseCategory)
{
amt += expense.amount;
}
}
categoryAmount.Add(new CatergoryAmount
{
amount = amt,
id = category.id,
categoryName = category.name,
});
}
/* for (int i = 0; i <allCategories.Count; i++)
{
double amt = 0;
//var currentCategory = allCategories[i];
for (int j = 0; j <allExpense.Count; j++)
{
double amount = 1;
//var currentExpenses = allExpense[j];
if (allCategories[i].id == allExpense[j].expenseCategory)
{
amount += allExpense[j].amount;
Console.WriteLine(amount);
}
else
{
amount += 1;
}
amt= amount;
}
categoryAmount.Add(new CatergoryAmount
{
amount = amt,
id = allCategories[i].id,
categoryName= allCategories[i].name,
});
}*/
return categoryAmount;
}
}
}