Skip to content

Commit

Permalink
feat(product): support in-memory backend delegate (#3193)
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 authored Oct 10, 2024
1 parent a8360df commit 0fa5e12
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 10 deletions.
9 changes: 7 additions & 2 deletions libs/product/driver/in-memory/src/backend/product.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import {
STATUS,
} from 'angular-in-memory-web-api';

import { DaffInMemorySingleRouteableBackend } from '@daffodil/driver/in-memory';
import { DaffProduct } from '@daffodil/product';
import {
DaffProductImageFactory,
DaffProductExtensionFactory,
} from '@daffodil/product/testing';

import { DAFF_PRODUCT_IN_MEMORY_COLLECTION_NAME } from '../collection-name.const';

/**
* An in-memory service that stubs out the backend services for getting products.
*
Expand All @@ -22,7 +25,9 @@ import {
@Injectable({
providedIn: 'root',
})
export class DaffInMemoryBackendProductService implements InMemoryDbService {
export class DaffInMemoryBackendProductService implements InMemoryDbService, DaffInMemorySingleRouteableBackend {
readonly collectionName = DAFF_PRODUCT_IN_MEMORY_COLLECTION_NAME;

protected _products: DaffProduct[] = [];

/**
Expand Down Expand Up @@ -105,7 +110,7 @@ export class DaffInMemoryBackendProductService implements InMemoryDbService {
* @returns An http response object
*/
get(reqInfo: any) {
if(reqInfo.id === 'best-sellers') {
if (reqInfo.id === 'best-sellers') {
return reqInfo.utils.createResponse$(() => ({
body: this._products.slice(0,4),
status: STATUS.OK,
Expand Down
1 change: 1 addition & 0 deletions libs/product/driver/in-memory/src/collection-name.const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const DAFF_PRODUCT_IN_MEMORY_COLLECTION_NAME = 'products';
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ModuleWithProviders,
} from '@angular/core';

import { provideDaffInMemoryBackends } from '@daffodil/driver/in-memory';
import { DaffProductDriver } from '@daffodil/product/driver';
import {
DaffDefaultProductFactory,
Expand All @@ -12,6 +13,7 @@ import {
} from '@daffodil/product/testing';

import { DaffInMemoryProductService } from './product.service';
import { DaffInMemoryBackendProductService } from '../backend/product.service';

/**
* Module for providing the DaffInMemoryProductService driver to your application.
Expand All @@ -32,6 +34,7 @@ export class DaffProductInMemoryDriverModule {
useExisting: DaffInMemoryProductService,
},
daffProvideProductExtraFactoryTypes(DaffDefaultProductFactory),
provideDaffInMemoryBackends(DaffInMemoryBackendProductService),
],
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import {
provideHttpClientTesting,
} from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';
import { InMemoryBackendConfig } from 'angular-in-memory-web-api';

import { DaffProductDriverResponse } from '@daffodil/product/driver';
import { DaffProductFactory } from '@daffodil/product/testing';

import { DaffInMemoryProductService } from './product.service';

describe('Driver | InMemory | Product | ProductService', () => {
describe('@daffodil/product/driver/in-memory | ProductService', () => {
let productService;
let httpMock: HttpTestingController;
let productFactory: DaffProductFactory;
Expand All @@ -23,6 +24,12 @@ describe('Driver | InMemory | Product | ProductService', () => {
imports: [],
providers: [
DaffInMemoryProductService,
{
provide: InMemoryBackendConfig,
useValue: {
apiBase: 'api',
},
},
provideHttpClient(withInterceptorsFromDi()),
provideHttpClientTesting(),
],
Expand Down
15 changes: 8 additions & 7 deletions libs/product/driver/in-memory/src/driver/product.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ import {
Inject,
Injectable,
} from '@angular/core';
import { InMemoryBackendConfig } from 'angular-in-memory-web-api';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

import { DaffInMemoryDriverBase } from '@daffodil/driver/in-memory';
import { DaffProduct } from '@daffodil/product';
import {
DaffProductDriverResponse,
DaffProductServiceInterface,
} from '@daffodil/product/driver';

import { DAFF_PRODUCT_IN_MEMORY_COLLECTION_NAME } from '../collection-name.const';
import { DAFF_PRODUCT_IN_MEMORY_PRODUCT_RESPONSE_TRANSFORM } from '../injection-tokens/public_api';
import { DaffInMemoryProductResponseTransform } from '../interfaces/public_api';

Expand All @@ -24,16 +27,14 @@ import { DaffInMemoryProductResponseTransform } from '../interfaces/public_api';
@Injectable({
providedIn: 'root',
})
export class DaffInMemoryProductService implements DaffProductServiceInterface {
/**
* @docs-private
*/
url = '/api/products';

export class DaffInMemoryProductService extends DaffInMemoryDriverBase implements DaffProductServiceInterface {
constructor(
private http: HttpClient,
config: InMemoryBackendConfig,
@Inject(DAFF_PRODUCT_IN_MEMORY_PRODUCT_RESPONSE_TRANSFORM) private transform: DaffInMemoryProductResponseTransform,
) {}
) {
super(config, DAFF_PRODUCT_IN_MEMORY_COLLECTION_NAME);
}

getAll(): Observable<DaffProduct[]> {
return this.http.get<DaffProduct[]>(this.url);
Expand Down

0 comments on commit 0fa5e12

Please sign in to comment.