I have an in-memory DB in angular
createDb(){
let Data= [
{id:1 , ProviderCode: '9W-', ProviderName: 'Jet Airways', ProviderType: 'Domestic' },
{id:2 , ProviderCode: 'EK-', ProviderName: 'Emirates', ProviderType: 'International' },
];
return {Data};
}
and I have specified CRUD operations in separate service
public getAllAirlines(){
return this.httpClient.get(this.SERVER_URL + 'Data');
}
public getAirlines(ProviderType: String){
return this.httpClient.get(`${this.SERVER_URL + 'Data'}/${ProviderType}`);
}
public createAirlines(data: {ProviderCode: String, ProviderName: String, ProviderType: String}){
return this.httpClient.post(`${this.SERVER_URL + 'Data'}`,data );
}
public deleteAirlines(AirlineId: String){
return this.httpClient.delete(`${this.SERVER_URL + 'Data'}/${AirlineId}`);
}
public updateAirlines (data: {ProviderCode: String, ProviderName: String, ProviderType: String}) {
return this.httpClient.put(`${this.SERVER_URL + 'Data'}/${data.ProviderCode}/${data.ProviderType}`, data);
}
I have a Delete airline component, which has a form having details as ProviderCode and ProviderType to delete. To delete the record I need to retrieve the ID from the DB and I am unable to do that. Same case with update functionality. Please suggest me ways to do that.
This function:
getAllAirlines(){
return this.httpClient.get(this.SERVER_URL + 'Data');
}
Will return you an observable.
In your component you can subscribe to it and that will get you all the Airline objects with their id.
Writing the function fullout might help you with understanding:
getAllAirlines(): Observable<Airline[]>{
return this.httpClient.get<Airline[]>(this.SERVER_URL + 'Data');
}
Let me also give an example of how to get the data from this request.
In your "delete-airline.component.ts":
airlines: Airline[] = []
...
constructor(
private separateService: SeparateService
) { }
ngOnInit():void {
this.separateService.getAllAirlines().subscibe(
data => this.airlines = data
)
}
Then you can just loop over the items in your airlines array and use their id values...