diff options
Diffstat (limited to 'ufund-ui/src/app/services/users.service.ts')
-rw-r--r-- | ufund-ui/src/app/services/users.service.ts | 36 |
1 files changed, 31 insertions, 5 deletions
diff --git a/ufund-ui/src/app/services/users.service.ts b/ufund-ui/src/app/services/users.service.ts index bc31870..dba8185 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) { @@ -40,10 +44,8 @@ export class UsersService { return this.http.get<User>(`${this.url}/${id}`, this.httpOptions) } - updateUser(id: string, user: User): Observable<User> { - console.log(id, user) - console.log(this.apiKey) - return this.http.put<User>(`${this.url}/${id}`,user, this.httpOptions) + updateUser(user: User): Observable<User> { + return this.http.put<User>(`${this.url}/${user.username}`,user, this.httpOptions2) } deleteUser(id: number): Observable<boolean> { @@ -72,4 +74,28 @@ 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()!).subscribe(() => { + this.refreshBasket(); + error: (err: any) => { + console.error(err); + } + }); + } + + getBasket() { + return this.basket; + } + } |