@@ -87,11 +87,6 @@ export class PlacesService {
8787 { image } ,
8888 { responseType : 'text' }
8989 )
90- . pipe (
91- map ( ( url ) => {
92- return url ;
93- } )
94- ) ;
9590 }
9691
9792 addplaces (
@@ -103,53 +98,52 @@ export class PlacesService {
10398 imageUrl : string
10499 ) {
105100 let generatedId : string ;
106- const newPlace = new Place (
107- Math . random ( ) . toString ( ) ,
108- title ,
109- description ,
110- imageUrl ,
111- price ,
112- dateFrom ,
113- dateTo ,
114- this . authService . userId
101+ let newPlace : Place ;
102+ return this . authService . userId . pipe (
103+ take ( 1 ) ,
104+ switchMap ( ( userId ) => {
105+ if ( ! userId ) {
106+ throw new Error ( 'No User found' ) ;
107+ }
108+ newPlace = new Place (
109+ Math . random ( ) . toString ( ) ,
110+ title ,
111+ description ,
112+ imageUrl ,
113+ price ,
114+ dateFrom ,
115+ dateTo ,
116+ userId
117+ ) ;
118+ return this . http . post < { name : string } > (
119+ 'https://ionic-angular-backend-66c35-default-rtdb.asia-southeast1.firebasedatabase.app/offered-places.json' ,
120+ { ...newPlace , id : null }
121+ ) ;
122+ } ) ,
123+ switchMap ( ( resData ) => {
124+ generatedId = resData . name ;
125+ return this . places ;
126+ } ) ,
127+ take ( 1 ) ,
128+ tap ( ( places ) => {
129+ newPlace . id = generatedId ;
130+ this . _places . next ( places . concat ( newPlace ) ) ;
131+ } )
115132 ) ;
116- return this . http
117- . post < { name : string } > (
118- 'https://ionic-angular-backend-66c35-default-rtdb.asia-southeast1.firebasedatabase.app/offered-places.json' ,
119- { ...newPlace , id : null }
120- )
121- . pipe (
122- switchMap ( ( resData ) => {
123- generatedId = resData . name ;
124- return this . places ;
125- } ) ,
126- take ( 1 ) ,
127- tap ( ( places ) => {
128- newPlace . id = generatedId ;
129- this . _places . next ( places . concat ( newPlace ) ) ;
130- } )
131- ) ;
132- // return this._places.pipe(
133- // take(1),
134- // delay(1000),
135- // tap((places) => {
136- // this._places.next(places.concat(newPlace));
137- // })
138- // );
139133 }
140134
141135 updatePlace ( placeId : string , title : string , description : string ) {
142136 let updatedPlaces : Place [ ] ;
143137 return this . places . pipe (
144138 take ( 1 ) ,
145139 switchMap ( ( places ) => {
146- if ( ! places || places . length <= 0 ) {
140+ if ( ! places || places . length <= 0 ) {
147141 return this . fetchPlaces ( ) ;
148142 } else {
149143 return of ( places ) ;
150144 }
151145 } ) ,
152- switchMap ( places => {
146+ switchMap ( ( places ) => {
153147 const updatedPlaceIndex = places . findIndex ( ( pl ) => pl . id === placeId ) ;
154148 updatedPlaces = [ ...places ] ;
155149 const oldPlace = updatedPlaces [ updatedPlaceIndex ] ;
0 commit comments