Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | 3x 3x 3x 3x 3x 3x 3x 8x 2x 2x 2x 2x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { Injectable } from '@angular/core'; import { Observable, forkJoin, of, throwError } from 'rxjs'; import { concatMap, first, map, switchMap } from 'rxjs/operators'; import { AppFacade } from 'ish-core/facades/app.facade'; import { ApiService, unpackEnvelope } from 'ish-core/services/api/api.service'; import { WishlistData } from '../../models/wishlist/wishlist.interface'; import { WishlistMapper } from '../../models/wishlist/wishlist.mapper'; import { Wishlist, WishlistHeader } from '../../models/wishlist/wishlist.model'; @Injectable({ providedIn: 'root' }) export class WishlistService { constructor(private apiService: ApiService, private wishlistMapper: WishlistMapper, private appFacade: AppFacade) {} /** * Gets a list of wishlists for the current user. * * @returns The customer's wishlists. */ getWishlists(): Observable<Wishlist[]> { return this.appFacade.customerRestResource$.pipe( first(), concatMap(restResource => this.apiService.get(`${restResource}/-/wishlists`).pipe( unpackEnvelope<WishlistData>(), map(wishlistData => wishlistData.map(data => this.getWishlist(this.wishlistMapper.fromDataToId(data)))), switchMap(obsArray => (obsArray.length ? forkJoin(obsArray) : of([]))) ) ) ); } /** * Gets a wishlist of the given id for the current user. * * @param wishlistId The wishlist id. * @returns The wishlist. */ private getWishlist(wishlistId: string): Observable<Wishlist> { Iif (!wishlistId) { return throwError(() => new Error('getWishlist() called without wishlistId')); } return this.appFacade.customerRestResource$.pipe( first(), concatMap(restResource => this.apiService .get<WishlistData>(`${restResource}/-/wishlists/${this.apiService.encodeResourceId(wishlistId)}`) .pipe(map(wishlistData => this.wishlistMapper.fromData(wishlistData, wishlistId))) ) ); } /** * Creates a wishlists for the current user. * * @param wishlistDetails The wishlist data. * @returns The created wishlist. */ createWishlist(wishlistData: WishlistHeader): Observable<Wishlist> { return this.appFacade.customerRestResource$.pipe( first(), concatMap(restResource => this.apiService .post(`${restResource}/-/wishlists`, wishlistData) .pipe(map((response: WishlistData) => this.wishlistMapper.fromData(wishlistData, response.title))) ) ); } /** * Deletes a wishlist of the given id. * * @param wishlistId The wishlist id. * @returns The wishlist. */ deleteWishlist(wishlistId: string): Observable<void> { Iif (!wishlistId) { return throwError(() => new Error('deleteWishlist() called without wishlistId')); } return this.appFacade.customerRestResource$.pipe( first(), concatMap(restResource => this.apiService.delete<void>(`${restResource}/-/wishlists/${this.apiService.encodeResourceId(wishlistId)}`) ) ); } /** * Updates a wishlist of the given id. * * @param wishlist The wishlist to be updated. * @returns The updated wishlist. */ updateWishlist(wishlist: Wishlist): Observable<Wishlist> { return this.appFacade.customerRestResource$.pipe( first(), concatMap(restResource => this.apiService .put(`${restResource}/-/wishlists/${this.apiService.encodeResourceId(wishlist.id)}`, wishlist) .pipe(map((response: Wishlist) => this.wishlistMapper.fromUpdate(response, wishlist.id))) ) ); } /** * Adds a product to the wishlist with the given id and reloads the wishlist. * * @param wishlist Id The wishlist id. * @param sku The product sku. * @param quantity The product quantity (default = 1). * @returns The changed wishlist. */ addProductToWishlist(wishlistId: string, sku: string, quantity = 1): Observable<Wishlist> { Iif (!wishlistId) { return throwError(() => new Error('addProductToWishlist() called without wishlistId')); } Iif (!sku) { return throwError(() => new Error('addProductToWishlist() called without sku')); } return this.appFacade.customerRestResource$.pipe( first(), concatMap(restResource => this.apiService .post( `${restResource}/-/wishlists/${this.apiService.encodeResourceId( wishlistId )}/products/${this.apiService.encodeResourceId(sku)}`, { quantity } ) .pipe(concatMap(() => this.getWishlist(wishlistId))) ) ); } /** * Removes a product from the wishlist with the given id. Returns an error observable if parameters are falsy. * * @param wishlist Id The wishlist id. * @param sku The product sku. * @returns The changed wishlist. */ removeProductFromWishlist(wishlistId: string, sku: string): Observable<Wishlist> { Iif (!wishlistId) { return throwError(() => new Error('removeProductFromWishlist() called without wishlistId')); } Iif (!sku) { return throwError(() => new Error('removeProductFromWishlist() called without sku')); } return this.appFacade.customerRestResource$.pipe( first(), concatMap(restResource => this.apiService .delete( `${restResource}/-/wishlists/${this.apiService.encodeResourceId( wishlistId )}/products/${this.apiService.encodeResourceId(sku)}` ) .pipe(concatMap(() => this.getWishlist(wishlistId))) ) ); } } |