diff options
author | Gunther6070 <haydenhartman10@yahoo.com> | 2025-03-25 08:21:01 -0400 |
---|---|---|
committer | Gunther6070 <haydenhartman10@yahoo.com> | 2025-03-25 08:21:01 -0400 |
commit | ffbe2870d320c4127d32307f2646f39e2e284eec (patch) | |
tree | f9f46447c689c1667f04f17bfbf2e8d07ff0fcd0 /ufund-ui/src | |
parent | d31c1aec7f615646553a227c8e235d4ae2679c68 (diff) | |
parent | c15aa3daab0cf9a640945d4e634d1327fb55d2db (diff) | |
download | JellySolutions-ffbe2870d320c4127d32307f2646f39e2e284eec.tar.gz JellySolutions-ffbe2870d320c4127d32307f2646f39e2e284eec.tar.bz2 JellySolutions-ffbe2870d320c4127d32307f2646f39e2e284eec.zip |
Merge branch 'api-cleanup' of https://github.com/RIT-SWEN-261-02/team-project-2245-swen-261-02-2b into api-cleanup
Diffstat (limited to 'ufund-ui/src')
-rw-r--r-- | ufund-ui/src/app/services/users.service.ts | 35 |
1 files changed, 20 insertions, 15 deletions
diff --git a/ufund-ui/src/app/services/users.service.ts b/ufund-ui/src/app/services/users.service.ts index 8515073..6671440 100644 --- a/ufund-ui/src/app/services/users.service.ts +++ b/ufund-ui/src/app/services/users.service.ts @@ -1,6 +1,6 @@ import { Injectable } from '@angular/core'; import {HttpClient, HttpHeaders} from '@angular/common/http'; -import {BehaviorSubject, firstValueFrom, Observable} from 'rxjs'; +import {BehaviorSubject, catchError, firstValueFrom, Observable, of} from 'rxjs'; import {User} from '../models/User'; import { Need } from '../models/Need'; import { CupboardService } from './cupboard.service'; @@ -16,20 +16,20 @@ export class UsersService { private url = "http://localhost:8080/users" private authUrl = "http://localhost:8080/auth" - private httpOptions = { + private httpOptions = () => ({ headers: new HttpHeaders({ 'Content-Type': 'application/json', "jelly-api-key": this.apiKey }) - }; - private httpOptions2 = { + }); + private httpOptions2 = () => ({ headers: new HttpHeaders({ 'Content-Type': 'application/json', "jelly-api-key": this.apiKey }), responseType: "text" as "json" // don't ask me how or why this works, bc i have no clue... // see the relevant angular bug report https://github.com/angular/angular/issues/18586 - }; + }); constructor( private http: HttpClient, @@ -37,19 +37,20 @@ export class UsersService { ) {} async createUser(username:string, password:string) { - await firstValueFrom(this.http.post<User>(this.url, {username: username, password: password}, this.httpOptions)) + await firstValueFrom(this.http.post<User>(this.url, {username: username, password: password}, this.httpOptions())) } getUser(id: string): Observable<User> { - return this.http.get<User>(`${this.url}/${id}`, this.httpOptions) + return this.http.get<User>(`${this.url}/${id}`, this.httpOptions()) } updateUser(user: User): Observable<User> { - return this.http.put<User>(`${this.url}/${user.username}`, user, this.httpOptions2) // This line is causing issues as the key is not properly being passed + console.log(`${this.url}/${user.username}`, user, this.httpOptions) + return this.http.put<User>(`${this.url}/${user.username}`, user, this.httpOptions2()) // This line is causing issues as the key is not properly being passed } deleteUser(id: number): Observable<boolean> { - return this.http.delete<boolean>(`${this.url}/${id}`, this.httpOptions) + return this.http.delete<boolean>(`${this.url}/${id}`, this.httpOptions()) } getCurrentUserSubject() { @@ -61,7 +62,7 @@ export class UsersService { } async login(username: string, password: string) { - let res = this.http.post<string>(this.authUrl, {username: username, password: password}, this.httpOptions2); + let res = this.http.post<string>(this.authUrl, {username: username, password: password}, this.httpOptions2()); this.apiKey = await firstValueFrom(res); console.log("apikey: "+this.apiKey) let res2 = this.http.get<User>(`${this.url}/${username}`, { @@ -81,16 +82,20 @@ export class UsersService { }) 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.updateUser(this.getCurrentUser()!) + .pipe( + catchError((err: any, ob) => { + console.error(err); + return of(); + }) + ) + .subscribe(() => { this.refreshBasket(); - error: (err: any) => { - console.error(err); - } }); } |