import { HttpClient, HttpHeaders } from '@angular/common/http'; import { Injectable } from '@angular/core'; @Injectable({ providedIn: 'root' }) export class ApiService { host = 'http://localhost:8000/api/'; constructor(private http: HttpClient) { } getEmployees() { let endpoint = 'employees'; let url = this.host + endpoint; return this.http.get(url); } addEmployee(employee: any) { let jsonUserData: any = localStorage.getItem('userData'); let userData = JSON.parse(jsonUserData); let httpHeaders = new HttpHeaders() .set('Authorization', `Bearer ${userData.token}`); const httpOptions = { headers: httpHeaders } let endpoint = 'employees'; let url = this.host + endpoint; return this.http.post(url, employee, httpOptions); } delEmployee(id: number) { let jsonUserData: any = localStorage.getItem('userData'); let userData = JSON.parse(jsonUserData); let httpHeaders = new HttpHeaders() .set('Authorization', `Bearer ${userData.token}`); const httpOptions = { headers: httpHeaders } let endpoint = 'employees'; let url = this.host + endpoint + '/' + id; return this.http.delete(url, httpOptions); } updateEmployee(employee: any) { let jsonUserData: any = localStorage.getItem('userData'); let userData = JSON.parse(jsonUserData); let httpHeaders = new HttpHeaders() .set('Authorization', `Bearer ${userData.token}`); const httpOptions = { headers: httpHeaders } let endpoint = 'employees'; let url = this.host + endpoint + '/' + employee.id; return this.http.put(url, employee, httpOptions); } }