From 9a274591acde24b282e31cdf8b170b35102d3099 Mon Sep 17 00:00:00 2001 From: William Brawner Date: Mon, 16 Aug 2021 17:22:58 -0600 Subject: [PATCH] Fixes for new Ktor API --- src/app/app.component.ts | 5 +++-- .../add-edit-budget.component.ts | 2 +- .../budget-details.component.html | 4 ++-- .../new-category/new-category.component.ts | 3 ++- src/app/shared/twigs.http.service.ts | 19 +++++++++++-------- src/app/shared/twigs.local.service.ts | 2 +- src/app/shared/twigs.service.ts | 2 +- src/app/users/user.ts | 6 ++++-- src/environments/environment.prod.ts | 2 +- src/environments/environment.ts | 2 +- 10 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/app/app.component.ts b/src/app/app.component.ts index ab2a175..cb29f19 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -41,12 +41,13 @@ export class AppComponent implements OnInit { '/register' ] let auth = this.storage.getItem('Authorization'); + let userId = this.storage.getItem('userId'); let savedUser = JSON.parse(this.storage.getItem('user')) as User; - if (auth && auth.length == 255) { + if (auth && auth.length == 255 && userId) { if (savedUser) { this.user.next(savedUser); } - this.twigsService.getProfile().subscribe(fetchedUser => { + this.twigsService.getProfile(userId).subscribe(fetchedUser => { this.storage.setItem('user', JSON.stringify(fetchedUser)); this.user.next(fetchedUser); if (unauthenticatedRoutes.indexOf(this.location.path()) != -1) { diff --git a/src/app/budgets/add-edit-budget/add-edit-budget.component.ts b/src/app/budgets/add-edit-budget/add-edit-budget.component.ts index 6c63317..957e5ab 100644 --- a/src/app/budgets/add-edit-budget/add-edit-budget.component.ts +++ b/src/app/budgets/add-edit-budget/add-edit-budget.component.ts @@ -23,7 +23,7 @@ export class AddEditBudgetComponent { ) { this.app.setTitle(this.title) this.app.setBackEnabled(true); - this.users = [new UserPermission(this.app.user.value, Permission.OWNER)]; + this.users = [new UserPermission(this.app.user.value.id, Permission.OWNER)]; } save(): void { diff --git a/src/app/budgets/budget-details/budget-details.component.html b/src/app/budgets/budget-details/budget-details.component.html index fcbd8fa..86854b8 100644 --- a/src/app/budgets/budget-details/budget-details.component.html +++ b/src/app/budgets/budget-details/budget-details.component.html @@ -27,9 +27,9 @@

Expenses

- Add Category + Add Category
- + add

Add categories to gain more insights into your expenses.

diff --git a/src/app/categories/new-category/new-category.component.ts b/src/app/categories/new-category/new-category.component.ts index 515a30c..2885233 100644 --- a/src/app/categories/new-category/new-category.component.ts +++ b/src/app/categories/new-category/new-category.component.ts @@ -17,7 +17,8 @@ export class NewCategoryComponent implements OnInit { ) { } ngOnInit() { - this.budgetId = this.route.snapshot.paramMap.get('budgetId'); + this.budgetId = this.route.snapshot.queryParamMap.get('budgetId'); + console.log(`Creating category for budget ${this.budgetId}`) this.category = new Category(); // TODO: Set random color for category, improve color picker // this.category.color = diff --git a/src/app/shared/twigs.http.service.ts b/src/app/shared/twigs.http.service.ts index 2a8a6a6..7e0d475 100644 --- a/src/app/shared/twigs.http.service.ts +++ b/src/app/shared/twigs.http.service.ts @@ -36,7 +36,8 @@ export class TwigsHttpService implements TwigsService { auth => { // TODO: Use token expiration to determine cookie expiration this.storage.setItem('Authorization', auth.token); - this.getProfile().subscribe(user => emitter.next(user), error => emitter.error(error)); + this.storage.setItem('userId', auth.userId); + this.getProfile(auth.userId).subscribe(user => emitter.next(user), error => emitter.error(error)); }, error => emitter.error(error) ); @@ -49,12 +50,13 @@ export class TwigsHttpService implements TwigsService { 'email': email, 'password': password }; - return this.http.post(this.apiUrl + '/users', params, this.options); + return this.http.post(this.apiUrl + '/users/register', params, this.options); } logout(): Observable { return new Observable(emitter => { this.storage.removeItem('Authorization'); + this.storage.removeItem('userId'); emitter.next(); emitter.complete(); }) @@ -72,7 +74,7 @@ export class TwigsHttpService implements TwigsService { } getBudgetBalance(id: string): Observable { - return this.http.get(`${this.apiUrl}/budgets/${id}/balance`, this.options) + return this.http.get(`${this.apiUrl}/transactions/sum?budgetId=${id}`, this.options) .pipe(map(obj => obj.balance)); } @@ -116,7 +118,7 @@ export class TwigsHttpService implements TwigsService { 'description': description, 'users': users.map(userPermission => { return { - user: userPermission.user.id, + user: userPermission.user, permission: Permission[userPermission.permission] }; }) @@ -138,7 +140,7 @@ export class TwigsHttpService implements TwigsService { 'description': budget.description, 'users': budget.users.map(userPermission => { return { - user: userPermission.user.id, + user: userPermission.user, permission: Permission[userPermission.permission] }; }) @@ -176,6 +178,7 @@ export class TwigsHttpService implements TwigsService { const params = { params: new HttpParams() .set('budgetIds', `${budgetId}`) + .set('archived', false) }; return this.http.get(`${this.apiUrl}/categories`, Object.assign(params, this.options)); } @@ -185,7 +188,7 @@ export class TwigsHttpService implements TwigsService { } getCategoryBalance(id: string): Observable { - return this.http.get(`${this.apiUrl}/categories/${id}/balance`, this.options) + return this.http.get(`${this.apiUrl}/transactions/sum?categoryId=${id}`, this.options) .pipe(map(obj => obj.balance)); } @@ -280,8 +283,8 @@ export class TwigsHttpService implements TwigsService { } // Users - getProfile(): Observable { - return this.http.get(`${this.apiUrl}/users/me`, this.options); + getProfile(id: string): Observable { + return this.http.get(`${this.apiUrl}/users/${id}`, this.options); } getUsersByUsername(username: string): Observable { diff --git a/src/app/shared/twigs.local.service.ts b/src/app/shared/twigs.local.service.ts index 1236caa..2fdd913 100644 --- a/src/app/shared/twigs.local.service.ts +++ b/src/app/shared/twigs.local.service.ts @@ -313,7 +313,7 @@ export class TwigsLocalService implements TwigsService { } // Users - getProfile(): Observable { + getProfile(id: string): Observable { return new Observable(subscriber => { subscriber.error("Not yet implemented") }); diff --git a/src/app/shared/twigs.service.ts b/src/app/shared/twigs.service.ts index 1fc18ec..68d182c 100644 --- a/src/app/shared/twigs.service.ts +++ b/src/app/shared/twigs.service.ts @@ -54,7 +54,7 @@ export interface TwigsService { updateTransaction(id: string, changes: object): Observable; deleteTransaction(id: string): Observable; - getProfile(): Observable; + getProfile(id: string): Observable; getUsersByUsername(username: string): Observable; } diff --git a/src/app/users/user.ts b/src/app/users/user.ts index 6fa45f3..12f9034 100644 --- a/src/app/users/user.ts +++ b/src/app/users/user.ts @@ -13,15 +13,16 @@ export class User { } export class AuthToken { + userId: string; token: string; expiration: Date; } export class UserPermission { - user: User; + user: string; permission: Permission; - constructor(user: User, permission: Permission) { + constructor(user: string, permission: Permission) { this.user = user; this.permission = permission; } @@ -30,5 +31,6 @@ export class UserPermission { export enum Permission { READ, WRITE, + MANAGE, OWNER } \ No newline at end of file diff --git a/src/environments/environment.prod.ts b/src/environments/environment.prod.ts index 4a81d6f..932df64 100644 --- a/src/environments/environment.prod.ts +++ b/src/environments/environment.prod.ts @@ -1,4 +1,4 @@ export const environment = { production: true, - apiUrl: 'https://api.twigs.brawner.dev' + apiUrl: '/api' }; diff --git a/src/environments/environment.ts b/src/environments/environment.ts index 8e94ace..92b443e 100644 --- a/src/environments/environment.ts +++ b/src/environments/environment.ts @@ -4,7 +4,7 @@ export const environment = { production: false, - apiUrl: 'http://localhost:8080' + apiUrl: 'http://localhost:8080/api' }; /*