1- import { HttpClient } from '@angular/common/http' ;
1+ import { HttpClient , HttpRequest } from '@angular/common/http' ;
22import { Injectable } from '@angular/core' ;
3- import { Observable } from 'rxjs' ;
3+ import { Offre } from 'app/model/offre' ;
4+ import { catchError , Observable , throwError } from 'rxjs' ;
45
56@Injectable ( {
67 providedIn : 'root'
@@ -9,21 +10,38 @@ export class OffreService {
910
1011 private BASE_URL = "http://localhost:8080/offres"
1112
12- constructor ( private httpClient :HttpClient ) { }
13-
14- public findAll ( ) : Observable < any >
15- { return this . httpClient . get ( this . BASE_URL ) ; }
16-
17- public save ( offre :any ) : Observable < any >
18- { return this . httpClient . post ( this . BASE_URL , offre ) ; }
13+ constructor ( private httpClient : HttpClient ) { }
14+
15+ public findAll ( ) : Observable < any > { return this . httpClient . get ( this . BASE_URL ) ; }
16+
17+ public save ( image : File , offre : Offre ) : Observable < Object > {
18+ const formData = new FormData ( ) ;
19+ formData . append ( 'adresseFront' , offre . adresseOffre ) ;
20+ formData . append ( 'villeFront' , offre . ville ) ;
21+ formData . append ( 'prixFront' , offre . prixOffre . toString ( ) ) ;
22+ formData . append ( 'surfaceFront' , offre . surfaceOffre . toString ( ) ) ;
23+ formData . append ( 'descriptionFront' , offre . description ) ;
24+ formData . append ( 'imageFront' , image ) ;
25+ formData . append ( 'disponibiliteFront' , offre . disponibiliteOffre . toString ( ) ) ;
26+ formData . append ( 'orientationFront' , offre . orientationOffre ) ;
27+ formData . append ( 'etatFront' , offre . etatOffre . toString ( ) ) ;
28+ formData . append ( 'typeFront' , offre . typeOffre ) ;
29+ const requestHttp = new HttpRequest ( 'POST' , this . BASE_URL , formData , {
30+ reportProgress : true , responseType : 'json'
31+ } ) ;
32+ return this . httpClient . request ( requestHttp ) . pipe (
33+ catchError ( ( error ) => {
34+ console . error ( error ) ;
35+ return throwError ( error ) ;
36+ } )
37+ ) ;
38+ }
1939
20- public delete ( id :number ) : Observable < any >
21- { return this . httpClient . delete ( this . BASE_URL + "/" + id ) ; }
40+ public delete ( id : number ) : Observable < any > { return this . httpClient . delete ( this . BASE_URL + "/" + id ) ; }
2241
23- public findOne ( id :number ) : Observable < any >
24- { return this . httpClient . get ( this . BASE_URL + "/" + id ) ; }
42+ public findOne ( id : number ) : Observable < any > { return this . httpClient . get ( this . BASE_URL + "/" + id ) ; }
2543
26- public update ( offre :any ) :Observable < any > {
44+ public update ( offre : any ) : Observable < any > {
2745 var offreJSON = JSON . parse ( offre ) ;
2846 return this . httpClient . put ( this . BASE_URL + "/" + offreJSON . idoffre , offreJSON ) ;
2947 }
0 commit comments