aboutsummaryrefslogtreecommitdiff
path: root/ufund-ui/src/app/services/users.service.ts
diff options
context:
space:
mode:
Diffstat (limited to 'ufund-ui/src/app/services/users.service.ts')
-rw-r--r--ufund-ui/src/app/services/users.service.ts20
1 files changed, 19 insertions, 1 deletions
diff --git a/ufund-ui/src/app/services/users.service.ts b/ufund-ui/src/app/services/users.service.ts
index 2a95202..a0d316c 100644
--- a/ufund-ui/src/app/services/users.service.ts
+++ b/ufund-ui/src/app/services/users.service.ts
@@ -2,6 +2,8 @@ import { Injectable } from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {BehaviorSubject, firstValueFrom, Observable} from 'rxjs';
import {User} from '../models/User';
+import { Need } from '../models/Need';
+import { CupboardService } from './cupboard.service';
@Injectable({
providedIn: 'root'
@@ -10,6 +12,7 @@ export class UsersService {
private currentUser : BehaviorSubject<User | null> = new BehaviorSubject<User | null>(null);
private apiKey: string = "";
+ private basket = new BehaviorSubject<Need[]>([]);
private url = "http://localhost:8080/users"
private authUrl = "http://localhost:8080/auth"
@@ -29,7 +32,8 @@ export class UsersService {
};
constructor(
- private http: HttpClient
+ private http: HttpClient,
+ private cupboardService: CupboardService,
) {}
async createUser(username:string, password:string) {
@@ -73,4 +77,18 @@ export class UsersService {
this.currentUser.next(currentU);
// this.currentUser.subscribe(r => console.log("currentUser: "+r.username))
}
+
+ refreshBasket() {
+ let promiseArr = this.getCurrentUser()!.basket.map(async needID => {
+ return await firstValueFrom(this.cupboardService.getNeed(needID));
+ })
+ Promise.all(promiseArr).then(r => this.basket.next(r));
+ }
+
+ removeNeed(id: number) {
+ let newArr = this.basket.getValue().filter(v => v.id != id);
+ this.basket.next(newArr);
+ this.getCurrentUser()!.basket = newArr.map(need => need.id);
+ this.updateUser(this.getCurrentUser()!);
+ }
}