Skip to content
Permalink
bfcf901c2b
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
231 lines (192 sloc) 7.15 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
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();
}
//all New Expense
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,
});
}
//get Details of a particular expense
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();
}
//Update and 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
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();
}
//add new category
public async Task AddCategory(int id, string name)
{
await firebase
.Child("Category")
.PostAsync(new ExpenseCategory()
{
name=name,
id = id,
});
}
//get a new category
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();
}
//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 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();
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;
}
}
}