| @@ -39,10 +39,6 @@ export class OutletDetailsPage implements OnInit { | |||||
| this.outlet_details = this.mall_details.outlets.find((outlet) => outlet.id === outlet_id); | this.outlet_details = this.mall_details.outlets.find((outlet) => outlet.id === outlet_id); | ||||
| this.temp_outlet_details = JSON.parse(JSON.stringify(this.outlet_details)); | this.temp_outlet_details = JSON.parse(JSON.stringify(this.outlet_details)); | ||||
| }); | }); | ||||
| this.mallService.getCartItems().then((data: Array<CartItem>) => { | |||||
| this.cart = data; | |||||
| }); | |||||
| } | } | ||||
| back() { | back() { | ||||
| @@ -1,25 +1,39 @@ | |||||
| import { Injectable } from '@angular/core'; | import { Injectable } from '@angular/core'; | ||||
| import { MALLS } from '../mocks/malls'; | import { MALLS } from '../mocks/malls'; | ||||
| import CartItem from '../models/cart-item'; | |||||
| import Mall, { IMall } from '../models/mall'; | import Mall, { IMall } from '../models/mall'; | ||||
| import { OfferService } from './offer.service'; | |||||
| import { OutletService } from './outlet.service'; | |||||
| @Injectable({ | @Injectable({ | ||||
| providedIn: 'root' | providedIn: 'root' | ||||
| }) | }) | ||||
| export class MallService { | export class MallService { | ||||
| malls: Array<IMall>; | malls: Array<IMall>; | ||||
| cart: Array<CartItem> = []; | |||||
| constructor() { | |||||
| this.malls = MALLS; | |||||
| constructor( | |||||
| private offerService: OfferService, | |||||
| private outletService: OutletService | |||||
| ) { | |||||
| this.fetchMalls(); | |||||
| } | } | ||||
| public async getAllMalls() { | |||||
| return this.malls; | |||||
| private async getDenormalizedMall(mall: Mall) { | |||||
| const offers = await Promise.all(mall.offers.map(offer_id => this.offerService.getOfferByID(offer_id))); | |||||
| const outlets = await Promise.all(mall.outlets.map(outlet_id => this.outletService.getOutletByID(outlet_id))); | |||||
| return { | |||||
| ...mall, | |||||
| offers, | |||||
| outlets, | |||||
| }; | |||||
| } | } | ||||
| public async getCartItems() { | |||||
| return this.cart; | |||||
| private async fetchMalls() { | |||||
| this.malls = await Promise.all(MALLS.map(this.getDenormalizedMall)); | |||||
| } | |||||
| public async getAllMalls() { | |||||
| return this.malls; | |||||
| } | } | ||||
| public async getMallByID(id: string) { | public async getMallByID(id: string) { | ||||
| @@ -1,10 +1,16 @@ | |||||
| import { Injectable } from '@angular/core'; | import { Injectable } from '@angular/core'; | ||||
| import { OUTLETS } from '../mocks/outlets'; | |||||
| import { MENU_ITEMS_1, MENU_ITEMS_2 } from '../mocks/menu-items'; | |||||
| import MenuItem from '../models/menu-item'; | |||||
| @Injectable({ | @Injectable({ | ||||
| providedIn: 'root' | providedIn: 'root' | ||||
| }) | }) | ||||
| export class MenuItemService { | export class MenuItemService { | ||||
| menu_items: Array<MenuItem> = MENU_ITEMS_1 && MENU_ITEMS_2; | |||||
| constructor() { } | constructor() { } | ||||
| public async getMenuItemByID(id: string) { | |||||
| return this.menu_items.find((menu_item) => menu_item.id === id); | |||||
| } | |||||
| } | } | ||||
| @@ -2,8 +2,7 @@ import { Injectable } from '@angular/core'; | |||||
| import Outlet, { IOutlet } from '../models/outlet'; | import Outlet, { IOutlet } from '../models/outlet'; | ||||
| import { OUTLETS } from '../mocks/outlets'; | import { OUTLETS } from '../mocks/outlets'; | ||||
| import { OfferService } from './offer.service'; | import { OfferService } from './offer.service'; | ||||
| import Offer from '../models/offer'; | |||||
| import MenuItem from '../models/menu-item'; | |||||
| import { MenuItemService } from './menu-item.service'; | |||||
| @Injectable({ | @Injectable({ | ||||
| providedIn: 'root' | providedIn: 'root' | ||||
| @@ -12,14 +11,15 @@ export class OutletService { | |||||
| outlets: Array<IOutlet> | outlets: Array<IOutlet> | ||||
| constructor( | constructor( | ||||
| private offerService: OfferService | |||||
| private offerService: OfferService, | |||||
| private menuItemService: MenuItemService | |||||
| ) { | ) { | ||||
| this.fetchOutlets(); | this.fetchOutlets(); | ||||
| } | } | ||||
| private async getDenormalizedOutlet(outlet: Outlet) { | private async getDenormalizedOutlet(outlet: Outlet) { | ||||
| const offers = await Promise.all(outlet.offers.map(offer_id => this.offerService.getOfferByID(offer_id))); | const offers = await Promise.all(outlet.offers.map(offer_id => this.offerService.getOfferByID(offer_id))); | ||||
| const menu_items: MenuItem[] = []; | |||||
| const menu_items = await Promise.all(outlet.menu_items.map(menu_item_id => this.menuItemService.getMenuItemByID(menu_item_id))); | |||||
| return { | return { | ||||
| ...outlet, | ...outlet, | ||||