import { Itinerary , Spot , ItineraryMap } from '../models/itinerary' ; import { Storage } from '@ionic/storage' ; import { Injectable } from '@angular/core' ; class @ Injectable ( ) export class ItineraryDAO { private collectionName : string = 'itineraries' ; public constructor ( private storage ) { } private return { Promise < ItineraryMap > } private get Storage ( ) : Promise < ItineraryMap > { return new Promise < ItineraryMap > ( ( resolve ) => { this . storage . get ( this . collectionName ) . then ( ( content ) => { if ( ! content ) resolve ( new ItineraryMap ( ) ) ; else { let data = JSON . parse ( content ) ; let map = new ItineraryMap ( ) ; let lines = Object . keys ( data . itineraries ) ; lines . forEach ( line => { let it = data . itineraries [ line ] ; let spots = [ ] ; it . spots . forEach ( sp => spots . push ( new Spot ( sp . latitude , sp . longitude , sp . returning ) ) ) ; map . save ( new Itinerary ( it . line , it . description , it . agency , spots ) ) ; } ) ; resolve ( map ) ; } } ) ; } ) ; } private param { ItineraryMap } obj return { Promise < void > } private set ( obj ) { return this . storage . set ( this . collectionName , JSON . stringify ( obj ) ) ; } param { string } line return { Promise < Itinerary > } public get ( line ) { return new Promise < Itinerary > ( resolve => this . Storage . then ( map => resolve ( map . get ( line ) ) ) ) ; } param { Itinerary } obj return { Promise < boolean > } public save ( obj ) { return new Promise < boolean > ( ( resolve , reject ) => { this . Storage . then ( ( map ) => { map . save ( obj ) ; this . set ( map ) . then ( ( ) => resolve ( true ) ) ; } ) ; } ) ; } param { Itinerary } obj return { Promise < boolean > } public remove ( obj ) { return new Promise < boolean > ( ( resolve , reject ) => { this . Storage . then ( ( map ) => { map . remove ( obj ) ; this . set ( map ) . then ( ( ) => resolve ( true ) ) ; } ) ; } ) ; } }