diff options
| author | benal01 <bja4245@rit.edu> | 2025-04-01 09:34:36 -0400 | 
|---|---|---|
| committer | benal01 <bja4245@rit.edu> | 2025-04-01 09:34:36 -0400 | 
| commit | 7ed26c5ee7171a502f6f8527fc55de2bb77eab3b (patch) | |
| tree | 2046e58c146097aac21c9e352771420c31df6589 /ufund-ui/src/app/services | |
| parent | ef46ddd082bb91d0262363536d46fe3eb4da47be (diff) | |
| parent | d8330f1ac85b26d08ca4df5ce3875078d7b4f47f (diff) | |
| download | JellySolutions-7ed26c5ee7171a502f6f8527fc55de2bb77eab3b.tar.gz JellySolutions-7ed26c5ee7171a502f6f8527fc55de2bb77eab3b.tar.bz2 JellySolutions-7ed26c5ee7171a502f6f8527fc55de2bb77eab3b.zip  | |
Merge branch 'main' of https://github.com/RIT-SWEN-261-02/team-project-2245-swen-261-02-2b-jellysolutions
Diffstat (limited to 'ufund-ui/src/app/services')
| -rw-r--r-- | ufund-ui/src/app/services/auth.service.ts | 67 | ||||
| -rw-r--r-- | ufund-ui/src/app/services/cupboard.service.ts | 30 | ||||
| -rw-r--r-- | ufund-ui/src/app/services/toasts.service.ts | 27 | ||||
| -rw-r--r-- | ufund-ui/src/app/services/users.service.ts | 73 | 
4 files changed, 137 insertions, 60 deletions
diff --git a/ufund-ui/src/app/services/auth.service.ts b/ufund-ui/src/app/services/auth.service.ts new file mode 100644 index 0000000..b75c931 --- /dev/null +++ b/ufund-ui/src/app/services/auth.service.ts @@ -0,0 +1,67 @@ +import {Injectable, Injector} from '@angular/core'; +import {BehaviorSubject, firstValueFrom} from 'rxjs'; +import {User} from '../models/User'; +import {HttpClient, HttpHeaders} from '@angular/common/http'; +import {UsersService} from './users.service'; + +@Injectable({ +    providedIn: 'root' +}) +export class AuthService { + +    private authUrl = "http://localhost:8080/auth" +    private userUrl = "http://localhost:8080/users" + +    private currentUser : BehaviorSubject<User | null> = new BehaviorSubject<User | null>(null); +    private apiKey: string = ""; + +    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, +        // private userService: UsersService +        private injector: Injector +    ) {} + +    async login(username: string, password: string) { +        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.userUrl}/${username}`, { +            headers: new HttpHeaders({ +                'Content-Type': 'application/json', +                "jelly-api-key": this.apiKey +            }) +        }) +        let currentU = await firstValueFrom(res2); +        this.currentUser.next(currentU); +        // this.currentUser.subscribe(r => console.log("currentUser: "+r.username)) +    } + +    async restoreLogin(username: string, key: string) { + +        const userService = this.injector.get(UsersService); +        this.apiKey = key; +        this.currentUser.next(await firstValueFrom(userService.getUser(username))) +    } + +    getCurrentUserSubject() { +        return this.currentUser; +    } + +    getCurrentUser() { +        return this.currentUser.getValue() +    } + +    getApiKey() { +        return this.apiKey; +    } + +} diff --git a/ufund-ui/src/app/services/cupboard.service.ts b/ufund-ui/src/app/services/cupboard.service.ts index 9e14106..9232c0c 100644 --- a/ufund-ui/src/app/services/cupboard.service.ts +++ b/ufund-ui/src/app/services/cupboard.service.ts @@ -2,6 +2,7 @@ import {Injectable} from '@angular/core';  import {HttpClient, HttpHeaders} from '@angular/common/http';  import {Need} from '../models/Need';  import {Observable} from 'rxjs'; +import {AuthService} from './auth.service';  @Injectable({      providedIn: 'root' @@ -9,35 +10,44 @@ import {Observable} from 'rxjs';  export class CupboardService {      private url = "http://localhost:8080/cupboard" -    private httpOptions = { -        headers: new HttpHeaders({'Content-Type': 'application/json'}) -    }; + +    httpOptions = () => ({ +        headers: new HttpHeaders({ +            'Content-Type': 'application/json', +            "jelly-api-key": this.authService.getApiKey() +        }) +    });      constructor( -        private http: HttpClient +        private http: HttpClient, +        private authService: AuthService      ) {}      createNeed(need: Need): Observable<boolean> { -        return this.http.post<boolean>(this.url, need, this.httpOptions) +        return this.http.post<boolean>(this.url, need, this.httpOptions())      }      getNeeds(): Observable<Need[]> { -        return this.http.get<Need[]>(this.url, this.httpOptions) +        return this.http.get<Need[]>(this.url, this.httpOptions())      }      searchNeeds(name: String): Observable<Need[]> { -        return this.http.get<Need[]>(`${this.url}/?name=${name}`, this.httpOptions) +        return this.http.get<Need[]>(`${this.url}/?name=${name}`, this.httpOptions())      }      getNeed(id: number): Observable<Need> { -        return this.http.get<Need>(`${this.url}/${id}`, this.httpOptions) +        return this.http.get<Need>(`${this.url}/${id}`, this.httpOptions())      }      updateNeed(id: number, data: Need): Observable<boolean> { -        return this.http.put<boolean>(`${this.url}/${id}`, data, this.httpOptions) +        return this.http.put<boolean>(`${this.url}/${id}`, data, this.httpOptions())      }      deleteNeed(id: number): Observable<boolean> { -        return this.http.delete<boolean>(`${this.url}/${id}`, this.httpOptions) +        return this.http.delete<boolean>(`${this.url}/${id}`, this.httpOptions()) +    } + +    checkoutNeed(id: number, quantity: number) { +        return this.http.put(`${this.url}/checkout`, {needID: id, amount: quantity}, this.httpOptions())      }  } diff --git a/ufund-ui/src/app/services/toasts.service.ts b/ufund-ui/src/app/services/toasts.service.ts new file mode 100644 index 0000000..4fd024e --- /dev/null +++ b/ufund-ui/src/app/services/toasts.service.ts @@ -0,0 +1,27 @@ +import {Injectable, ViewContainerRef} from '@angular/core'; +import {ToastComponent} from '../components/toast/toast.component'; + +export enum ToastType { +    INFO, +    WARNING, +    ERROR +} + +@Injectable({ +    providedIn: 'root' +}) +export class ToastsService { + +    private vcr?: ViewContainerRef + +    sendToast(type: ToastType, message: string, action?: {label: string, onAction: () => void}) { +        let compRef = this.vcr?.createComponent(ToastComponent)! +        compRef.setInput("message", message) +        compRef.setInput("type", type) +        compRef.setInput("action", action) +    } + +    setRootViewContainerRef(vcr: ViewContainerRef) { +        this.vcr = vcr +    } +} diff --git a/ufund-ui/src/app/services/users.service.ts b/ufund-ui/src/app/services/users.service.ts index dba8185..4080ebf 100644 --- a/ufund-ui/src/app/services/users.service.ts +++ b/ufund-ui/src/app/services/users.service.ts @@ -1,96 +1,69 @@  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'; +import {AuthService} from './auth.service';  @Injectable({    providedIn: 'root'  })  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" -    private httpOptions = { + +    httpOptions = () => ({          headers: new HttpHeaders({              'Content-Type': 'application/json', -            "jelly-api-key": this.apiKey +            "jelly-api-key": this.authService.getApiKey()          }) -    }; -    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,          private cupboardService: CupboardService, +        private authService: AuthService      ) {}      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) +        console.log(`${this.url}/${user.username}`, user, this.httpOptions) +        return this.http.put<User>(`${this.url}/${user.username}`, user, this.httpOptions())      }      deleteUser(id: number): Observable<boolean> { -        return this.http.delete<boolean>(`${this.url}/${id}`, this.httpOptions) -    } - -    getCurrentUserSubject() { -        return this.currentUser; -    } - -    getCurrentUser() { -        return this.currentUser.getValue() -    } - -    async login(username: string, password: string) { -        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}`, { -            headers: new HttpHeaders({ -                'Content-Type': 'application/json', -                "jelly-api-key": this.apiKey -            }) -        }) -        let currentU = await firstValueFrom(res2); -        this.currentUser.next(currentU); -        // this.currentUser.subscribe(r => console.log("currentUser: "+r.username)) +        return this.http.delete<boolean>(`${this.url}/${id}`, this.httpOptions())      }      refreshBasket() { -        let promiseArr = this.getCurrentUser()!.basket.map(async needID => { +        let promiseArr = this.authService.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.authService.getCurrentUser()!.basket = newArr.map(need => need.id); +        this.updateUser(this.authService.getCurrentUser()!) +            .pipe( +                catchError((err: any, _) => { +                    console.error(err); +                    return of(); +                }) +            ) +            .subscribe(() => {              this.refreshBasket(); -            error: (err: any) => { -              console.error(err); -            }            });      }  | 
