import { Injectable } from '@angular/core' ; import { Storage } from '@ionic/storage' ; import { Database } from '@ionic/cloud-angular' ; import { Observable } from 'rxjs/Observable' ; import 'rxjs/add/operator/map' ; import 'rxjs/add/operator/catch' ; @ Injectable ( ) export class DataService { cartItems : any [ ] ; loading : any ; constructor ( public storage , public db ) { this . storage . get ( 'cart' ) . then ( ( value ) => { if ( value === null ) { this . cartItems = [ ] ; } else { this . cartItems = value ; } } ) ; this . db . connect ( ) ; } public getCategories ( ) { const categories = this . db . aggregate ( { name : "testing" , categories : this . db . collection ( 'categories' ) , parent_categories : this . db . collection ( 'parent_categories' ) } ) ; return categories . fetch ( ) ; } public getPopularProducts ( ) { const popular_products = this . db . collection ( 'popular_products' ) ; return popular_products . order ( "price" , "ascending" ) . watch ( ) ; } public getProducts ( ) { const products = this . db . collection ( 'products' ) ; return products . fetch ( ) ; } public addToCart ( item ) { this . cartItems . push ( item ) ; return this . storage . set ( 'cart' , this . cartItems ) ; } public clearCart ( ) { this . cartItems = [ ] ; return this . storage . remove ( 'cart' ) ; } public getCart ( ) { return this . storage . get ( 'cart' ) ; } }