import { HttpClient, HttpHeaders } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { switchMap } from 'rxjs'; import { environment } from '../../../environments/environment'; import { Pin } from '../../model/Pin'; import { AutocompleteService } from '../auto-complete/auto-complete.service'; // import { ImageService } from '../image/image.service'; @Injectable({ providedIn: 'root', }) export class PinService { allPins: Pin[] = []; filteredPins: Pin[] = []; private apiURL = environment.apiURL; constructor( private http: HttpClient, private autoCompleteService: AutocompleteService // private imageService: ImageService ) {} getPins(): any { const url = `${this.apiURL}/pins`; const headers = new HttpHeaders({ 'Content-Type': 'application/json', Authorization: 'Bearer ' + localStorage.getItem('auth_token'), }); return this.http.get(url, { headers }); } addPin(pin: { title: string; description: string; location: string; files: string[]; date: string; }) { const url = `${this.apiURL}/pin/add`; const headers = new HttpHeaders({ 'Content-Type': 'application/json', Authorization: 'Bearer ' + localStorage.getItem('auth_token'), }); return this.autoCompleteService.getAdressCoordinates(pin.location).pipe( switchMap((response: any) => { const coords: [string, string] = [response[0].lat, response[0].lon]; return this.http.post( url, { title: pin.title, description: pin.description, location: coords, files: pin.files, user_id: '', date: pin.date, }, { headers } ); }) ); } updatePin( id: string, pin: { title: string; description: string; location: string; files: any[]; user_id: string; date: string; } ) { const url = `${this.apiURL}/pin/${id}`; const headers = new HttpHeaders({ 'Content-Type': 'application/json', Authorization: 'Bearer ' + localStorage.getItem('auth_token'), }); // Obtenir les coordonnées GPS à partir de l'adresse return this.autoCompleteService.getAdressCoordinates(pin.location).pipe( switchMap((response: any) => { const coords: [string, string] = [response[0].lat, response[0].lon]; return this.http.patch( url, { title: pin.title, description: pin.description, location: coords, files: pin.files, user_id: pin.user_id, date: pin.date, }, { headers } ); }) ); } deletePin(id: string) { const url = `${this.apiURL}/pin/${id}`; const headers = new HttpHeaders({ 'Content-Type': 'application/json', Authorization: 'Bearer ' + localStorage.getItem('auth_token'), }); return this.http.delete(url, { headers }); } }