Skip to content

Commit

Permalink
Connect backend for MVP
Browse files Browse the repository at this point in the history
  • Loading branch information
ParadoxZero committed Jul 28, 2024
1 parent c0f29bb commit 09d5e12
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .env.production
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
VITE_CREATE_DUMMY_DATA=false
VITE_PING_REMOTE=false
VITE_USE_LOCAL_DATA_SERVICE=true
VITE_USE_LOCAL_DATA_SERVICE=false
6 changes: 3 additions & 3 deletions api/budget_controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public async Task<IActionResult> CreateBudget([FromBody] CreateBudgetInput input
return Ok(await _userDataService.CreateBudget(input.name));
}

[HttpPost("{budget_id}/category")]
public async Task<IActionResult> AddCategoryInput(string budget_id, Category category)
[HttpPost("{budget_id}/categories")]
public async Task<IActionResult> AddCategoryInput(string budget_id, List<Category> categoryList)
{
return Ok(await _userDataService.AddCategoryToBudget(budget_id, category));
return Ok(await _userDataService.AddCategoryToBudget(budget_id, categoryList));
}


Expand Down
2 changes: 1 addition & 1 deletion api/models/budget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ public class Budget
public required List<Expense> unplannedList { get; set; }
public required TimeUnit period { get; set; }
public required List<UserAction> userActions { get; set; }
public DateTime last_updated { get; set; }
public long last_updated { get; set; }
public required List<string> authorized_users { get; set; }
}
2 changes: 1 addition & 1 deletion api/models/category.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class Category
public required string Description { get; set; }
public decimal Allocation { get; set; }
public bool IsActive { get; set; }
public DateTime LastUpdated { get; set; }
public long LastUpdated { get; set; }
public required string Currency { get; set; }
public required List<Expense> ExpenseList { get; set; }
}
Expand Down
2 changes: 1 addition & 1 deletion api/models/expense.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ public class Expense
public required string Title { get; set; }
public decimal Amount { get; set; }
public int CategoryId { get; set; }
public DateTime Timestamp { get; set; }
public long Timestamp { get; set; }
}
6 changes: 3 additions & 3 deletions api/models/recurring.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ public class Recurring
public required string Name { get; set; }
public required string Description { get; set; }
public bool IsActive { get; set; }
public DateTime LastUpdated { get; set; }
public long LastUpdated { get; set; }
public RecurringType Frequency { get; set; }
public int FrequencyUnit { get; set; } // This has different meanings based on the frequency, if monthly, it will be day of month, if weekly, it will be day of week etc.
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public long StartDate { get; set; }
public long EndDate { get; set; }
public decimal Amount { get; set; }
}
2 changes: 1 addition & 1 deletion api/models/user_action.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public enum UserActionType

public class UserAction
{
public DateTime timestamp { get; set; }
public long timestamp { get; set; }
public UserActionType type { get; set; }
public Category? category { get; set; }
public Expense? expense { get; set; }
Expand Down
21 changes: 17 additions & 4 deletions api/services/db_service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public async Task<UserData> GetUserData(string user_id)
}
}

public async Task UpdateUserData(UserData userData)
{
await _container.UpsertItemAsync(userData, new PartitionKey(userData.id));
}

public async Task<Budget> GetBudgetAsync(string budget_id)
{
return await _container.ReadItemAsync<Budget>(budget_id, new PartitionKey(budget_id));
Expand All @@ -66,7 +71,7 @@ public async Task<Budget> CreateNewBudgetAsync(string name)
authorized_users = new List<string>() { _identityService.GetUserIdentity() },
categoryList = new List<Category>(),
history_id = history.id,
last_updated = DateTime.Now,
last_updated = DateTime.UtcNow.Ticks,
period = period,
recurringList = new List<Recurring>(),
unplannedList = new List<Expense>(),
Expand All @@ -82,6 +87,7 @@ public async Task<Budget> CreateNewBudgetAsync(string name)

public async Task UpdateBudgetAsync(Budget budget)
{
budget.last_updated = DateTime.UtcNow.Ticks;
await _container.UpsertItemAsync(budget, new PartitionKey(budget.id));
}

Expand All @@ -90,15 +96,19 @@ public async Task AddExpenseAsync(string budget_id, Expense expense)
Budget budget = await GetBudgetAsync(budget_id);
Category cat = budget.categoryList?.Find(c => c.Id == expense.CategoryId) ?? throw new Exception("Category not found");
expense.Id = cat.ExpenseList.LastOrDefault()?.Id ?? 0;
expense.Timestamp = DateTime.UtcNow.Ticks;
cat.ExpenseList.Add(expense);
cat.LastUpdated = DateTime.UtcNow.Ticks;
await UpdateBudgetAsync(budget);
}

public async Task UpdateExpenseAsync(string budget_id, Expense expense)
{
Budget budget = await GetBudgetAsync(budget_id);
expense.Timestamp = DateTime.UtcNow.Ticks;
Category category = budget.categoryList?.Find(c => c.Id == expense.CategoryId) ?? throw new Exception("Category not found");
category.ExpenseList[category.ExpenseList.FindIndex(e => e.Id == expense.Id)] = expense;
category.LastUpdated = DateTime.UtcNow.Ticks;
await UpdateBudgetAsync(budget);
}

Expand All @@ -107,21 +117,24 @@ public async Task DeleteExpense(string budget_id, Expense expense)
Budget budget = await GetBudgetAsync(budget_id);
Category category = budget.categoryList?.Find(c => c.Id == expense.CategoryId) ?? throw new Exception("Category not found");
category.ExpenseList.RemoveAll(e => e.Id == expense.Id);
category.LastUpdated = DateTime.UtcNow.Ticks;
await UpdateBudgetAsync(budget);
}

public async Task AddCategoryAsync(string budget_id, Category category)
public async Task AddCategoryAsync(string budget_id, List<Category> categoryList)
{
Budget budget = await GetBudgetAsync(budget_id);
category.Id = (budget.categoryList.LastOrDefault()?.Id ?? 0) + 1;
budget.categoryList.Add(category);
int last_id = (budget.categoryList.LastOrDefault()?.Id ?? 0) + 1;
categoryList.ForEach(c => { c.Id = last_id++; c.LastUpdated = DateTime.UtcNow.Ticks; });
budget.categoryList.AddRange(categoryList);
await UpdateBudgetAsync(budget);
}

public async Task UpdateCategoryAsync(string budget_id, Category category)
{
Budget budget = await GetBudgetAsync(budget_id);
budget.categoryList[budget.categoryList.FindIndex(c => c.Id == category.Id)] = category;
category.LastUpdated = DateTime.UtcNow.Ticks;
await UpdateBudgetAsync(budget);
}

Expand Down
10 changes: 7 additions & 3 deletions api/services/user_data_service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ public async Task<List<Budget>> FetchAssociatedBudgets()

public async Task<Budget> CreateBudget(string name)
{
return await _dbService.CreateNewBudgetAsync(name);
var budget = await _dbService.CreateNewBudgetAsync(name);
var user_data = await _dbService.GetUserData(_identityService.GetUserIdentity());
user_data.BudgetIds.Add(budget.id);
await _dbService.UpdateUserData(user_data);
return budget;
}

public async Task<Budget> AddExpenseToBudget(string budget_id, Expense expense)
Expand All @@ -37,9 +41,9 @@ public async Task<Budget> AddExpenseToBudget(string budget_id, Expense expense)
return await _dbService.GetBudgetAsync(budget_id);
}

public async Task<Budget> AddCategoryToBudget(string budget_id, Category category)
public async Task<Budget> AddCategoryToBudget(string budget_id, List<Category> categoryList)
{
await _dbService.AddCategoryAsync(budget_id, category);
await _dbService.AddCategoryAsync(budget_id, categoryList);
return await _dbService.GetBudgetAsync(budget_id);
}
}
16 changes: 13 additions & 3 deletions ui/services/data_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class RemoteDataService implements DataService {
BASE_URL: string;

constructor() {
this.BASE_URL = window.location.hostname;
this.BASE_URL = "";
console.log(`Using remote data service at ${this.BASE_URL}`);
}

createBudget(name: string): Promise<Budget> {
Expand All @@ -43,6 +44,7 @@ class RemoteDataService implements DataService {
},
}).then((response) => response.json() as Promise<Budget>);
}

getBudget(): Promise<Budget[]> {
const endpoint: string = `${this.BASE_URL}/api/Budget`;
return fetch(endpoint, { method: 'GET' }).then((response) => response.json() as Promise<Budget[]>);
Expand All @@ -51,6 +53,7 @@ class RemoteDataService implements DataService {
getHistory(): Promise<BudgetHistory> {
throw new Error("Not implemented");
}

updateCategory(budger_id: string, category: Category): Promise<Budget> {
const endpoint: string = `${this.BASE_URL}/api/Budget/${budger_id}`;
return fetch(endpoint, {
Expand All @@ -73,8 +76,15 @@ class RemoteDataService implements DataService {
}).then((response) => response.json() as Promise<Budget>);
}

updateExpense(_budget_id: string, _expense: Expense): Promise<Budget> {
throw new Error("Not implemented");
updateExpense(_budget_id: string, expense: Expense): Promise<Budget> {
const endpoint: string = `${this.BASE_URL}/api/Budget/${_budget_id}/expense`;
return fetch(endpoint, {
method: 'POST',
body: JSON.stringify(expense),
headers: {
'Content-Type': 'application/json'
},
}).then((response) => response.json() as Promise<Budget>);
}

deleteExpense(_budget_id: string, _expenseId: number): Promise<Budget> {
Expand Down

0 comments on commit 09d5e12

Please sign in to comment.