aboutsummaryrefslogtreecommitdiff
path: root/ufund-ui/src/app/services/cupboard.service.ts
blob: 9232c0c123422d728edd89e595fff7555da0e84f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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'
})
export class CupboardService {

    private url = "http://localhost:8080/cupboard"

    httpOptions = () => ({
        headers: new HttpHeaders({
            'Content-Type': 'application/json',
            "jelly-api-key": this.authService.getApiKey()
        })
    });

    constructor(
        private http: HttpClient,
        private authService: AuthService
    ) {}

    createNeed(need: Need): Observable<boolean> {
        return this.http.post<boolean>(this.url, need, this.httpOptions())
    }

    getNeeds(): Observable<Need[]> {
        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())
    }

    getNeed(id: number): Observable<Need> {
        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())
    }

    deleteNeed(id: number): Observable<boolean> {
        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())
    }
}