Skip to content

Commit

Permalink
feat(customer): support in-memory backend delegate (graycoreio#3184)
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 authored and gracetxgao committed Oct 24, 2024
1 parent 8e5c9db commit f350814
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@ import {

import { DaffCustomerAddress } from '@daffodil/customer';
import { DaffCustomerAddressFactory } from '@daffodil/customer/testing';
import { DaffInMemorySingleRouteableBackend } from '@daffodil/driver/in-memory';

import { DAFF_CUSTOMER_ADDRESS_IN_MEMORY_COLLECTION_NAME } from '../collection-names/address.const';

/**
* An in-memory service that handles customer address HTTP requests.
*/
@Injectable({
providedIn: 'root',
})
export class DaffCustomerAddressInMemoryBackendService implements InMemoryDbService {
export class DaffCustomerAddressInMemoryBackendService implements InMemoryDbService, DaffInMemorySingleRouteableBackend {
readonly collectionName = DAFF_CUSTOMER_ADDRESS_IN_MEMORY_COLLECTION_NAME;

addresses: Record<DaffCustomerAddress['id'], DaffCustomerAddress> = {};

constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import {

import { DaffCustomer } from '@daffodil/customer';
import { DaffCustomerFactory } from '@daffodil/customer/testing';
import { DaffInMemorySingleRouteableBackend } from '@daffodil/driver/in-memory';

import { DAFF_CUSTOMER_IN_MEMORY_COLLECTION_NAME } from '../collection-names/customer.const';

/**
* An in-memory service that delegates customer queries to child backends
Expand All @@ -17,7 +20,9 @@ import { DaffCustomerFactory } from '@daffodil/customer/testing';
@Injectable({
providedIn: 'root',
})
export class DaffCustomerInMemoryBackendService implements InMemoryDbService {
export class DaffCustomerInMemoryBackendService implements InMemoryDbService, DaffInMemorySingleRouteableBackend {
readonly collectionName = DAFF_CUSTOMER_IN_MEMORY_COLLECTION_NAME;

customers: Record<DaffCustomer['id'], DaffCustomer> = {};

constructor(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const DAFF_CUSTOMER_ADDRESS_IN_MEMORY_COLLECTION_NAME = 'customer-address';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const DAFF_CUSTOMER_IN_MEMORY_COLLECTION_NAME = 'customer';
17 changes: 12 additions & 5 deletions libs/customer/driver/in-memory/src/driver/address.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
provideHttpClientTesting,
} from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';
import { InMemoryBackendConfig } from 'angular-in-memory-web-api';
import { Observable } from 'rxjs';

import { DaffCustomerAddress } from '@daffodil/customer';
Expand All @@ -25,6 +26,12 @@ describe('@daffodil/customer/driver/in-memory | DaffCustomerAddressInMemoryDrive
imports: [],
providers: [
DaffCustomerAddressInMemoryDriver,
{
provide: InMemoryBackendConfig,
useValue: {
apiBase: 'api',
},
},
provideHttpClient(withInterceptorsFromDi()),
provideHttpClientTesting(),
],
Expand All @@ -50,7 +57,7 @@ describe('@daffodil/customer/driver/in-memory | DaffCustomerAddressInMemoryDrive
let result: Observable<DaffCustomerAddress[]>;

beforeEach(() => {
url = service.url;
url = service['url'];
result = service.list();
});

Expand All @@ -72,7 +79,7 @@ describe('@daffodil/customer/driver/in-memory | DaffCustomerAddressInMemoryDrive
let result: Observable<DaffCustomerAddress>;

beforeEach(() => {
url = service.url;
url = service['url'];
result = service.get(mockAddress.id);
});

Expand All @@ -94,7 +101,7 @@ describe('@daffodil/customer/driver/in-memory | DaffCustomerAddressInMemoryDrive
let result: Observable<DaffCustomerAddress[]>;

beforeEach(() => {
url = `${service.url}/${mockAddress.id}`;
url = `${service['url']}/${mockAddress.id}`;
result = service.update(mockAddress);
});

Expand All @@ -116,7 +123,7 @@ describe('@daffodil/customer/driver/in-memory | DaffCustomerAddressInMemoryDrive
let result: Observable<DaffCustomerAddress[]>;

beforeEach(() => {
url = service.url;
url = service['url'];
result = service.add(mockAddress);
});

Expand All @@ -138,7 +145,7 @@ describe('@daffodil/customer/driver/in-memory | DaffCustomerAddressInMemoryDrive
let result: Observable<DaffCustomerAddress[]>;

beforeEach(() => {
url = `${service.url}/${mockAddress.id}`;
url = `${service['url']}/${mockAddress.id}`;
result = service.delete(mockAddress.id);
});

Expand Down
16 changes: 9 additions & 7 deletions libs/customer/driver/in-memory/src/driver/address.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { InMemoryBackendConfig } from 'angular-in-memory-web-api';
import {
map,
Observable,
Expand All @@ -8,6 +9,9 @@ import {
import { DaffIdentifiable } from '@daffodil/core';
import { DaffCustomerAddress } from '@daffodil/customer';
import { DaffCustomerAddressDriverInterface } from '@daffodil/customer/driver';
import { DaffInMemoryDriverBase } from '@daffodil/driver/in-memory';

import { DAFF_CUSTOMER_ADDRESS_IN_MEMORY_COLLECTION_NAME } from '../collection-names/address.const';

/**
* The customer address in-memory driver to mock the customer address backend service.
Expand All @@ -17,15 +21,13 @@ import { DaffCustomerAddressDriverInterface } from '@daffodil/customer/driver';
@Injectable({
providedIn: 'root',
})
export class DaffCustomerAddressInMemoryDriver implements DaffCustomerAddressDriverInterface {
/**
* @docs-private
*/
url = '/api/customer-address';

export class DaffCustomerAddressInMemoryDriver extends DaffInMemoryDriverBase implements DaffCustomerAddressDriverInterface {
constructor(
private http: HttpClient,
) {}
config: InMemoryBackendConfig,
) {
super(config, DAFF_CUSTOMER_ADDRESS_IN_MEMORY_COLLECTION_NAME);
}

list(): Observable<DaffCustomerAddress[]> {
return this.http.get<DaffCustomerAddress[]>(this.url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ import {
DaffCustomerAddressDriver,
DaffCustomerDriver,
} from '@daffodil/customer/driver';
import {
DaffInMemoryBackendInterface,
provideDaffInMemoryBackends,
} from '@daffodil/driver/in-memory';

import { DaffCustomerAddressInMemoryDriver } from './address.service';
import { DaffCustomerInMemoryDriver } from './customer.service';
import { DaffCustomerAddressInMemoryBackendService } from '../backend/address.service';
import { DaffCustomerInMemoryBackendService } from '../backend/customer.service';

/**
* Provides the {@link DaffCustomerInMemoryDriver} as the {@link DaffCustomerDriver}.
Expand All @@ -28,10 +34,15 @@ export class DaffCustomerInMemoryDriverModule {
{
provide: DaffCustomerDriver,
useExisting: DaffCustomerInMemoryDriver,
},{
},
{
provide: DaffCustomerAddressDriver,
useExisting: DaffCustomerAddressInMemoryDriver,
},
provideDaffInMemoryBackends<DaffInMemoryBackendInterface>(
DaffCustomerInMemoryBackendService,
DaffCustomerAddressInMemoryBackendService,
),
],
};
}
Expand Down
15 changes: 11 additions & 4 deletions libs/customer/driver/in-memory/src/driver/customer.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
provideHttpClientTesting,
} from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';
import { InMemoryBackendConfig } from 'angular-in-memory-web-api';
import { Observable } from 'rxjs';

import { DaffCustomer } from '@daffodil/customer';
Expand All @@ -25,6 +26,12 @@ describe('@daffodil/customer/driver/in-memory | DaffCustomerInMemoryDriver', ()
imports: [],
providers: [
DaffCustomerInMemoryDriver,
{
provide: InMemoryBackendConfig,
useValue: {
apiBase: 'api',
},
},
provideHttpClient(withInterceptorsFromDi()),
provideHttpClientTesting(),
],
Expand All @@ -50,7 +57,7 @@ describe('@daffodil/customer/driver/in-memory | DaffCustomerInMemoryDriver', ()
let result: Observable<DaffCustomer>;

beforeEach(() => {
url = service.url;
url = service['url'];
result = service.get();
});

Expand All @@ -72,7 +79,7 @@ describe('@daffodil/customer/driver/in-memory | DaffCustomerInMemoryDriver', ()
let result: Observable<DaffCustomer>;

beforeEach(() => {
url = `${service.url}/customer`;
url = `${service['url']}/customer`;
result = service.update(mockResponse);
});

Expand All @@ -94,7 +101,7 @@ describe('@daffodil/customer/driver/in-memory | DaffCustomerInMemoryDriver', ()
let result: Observable<DaffCustomer>;

beforeEach(() => {
url = `${service.url}/email`;
url = `${service['url']}/email`;
result = service.changeEmail('email', 'password');
});

Expand All @@ -116,7 +123,7 @@ describe('@daffodil/customer/driver/in-memory | DaffCustomerInMemoryDriver', ()
let result: Observable<void>;

beforeEach(() => {
url = `${service.url}/password`;
url = `${service['url']}/password`;
result = service.changePassword('old', 'new');
});

Expand Down
16 changes: 9 additions & 7 deletions libs/customer/driver/in-memory/src/driver/customer.service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { InMemoryBackendConfig } from 'angular-in-memory-web-api';
import {
map,
Observable,
} from 'rxjs';

import { DaffCustomer } from '@daffodil/customer';
import { DaffCustomerDriverInterface } from '@daffodil/customer/driver';
import { DaffInMemoryDriverBase } from '@daffodil/driver/in-memory';

import { DAFF_CUSTOMER_IN_MEMORY_COLLECTION_NAME } from '../collection-names/customer.const';

/**
* The customer inmemory driver to mock the customer backend service.
Expand All @@ -16,15 +20,13 @@ import { DaffCustomerDriverInterface } from '@daffodil/customer/driver';
@Injectable({
providedIn: 'root',
})
export class DaffCustomerInMemoryDriver implements DaffCustomerDriverInterface {
/**
* @docs-private
*/
url = '/api/customer';

export class DaffCustomerInMemoryDriver extends DaffInMemoryDriverBase implements DaffCustomerDriverInterface {
constructor(
private http: HttpClient,
) {}
config: InMemoryBackendConfig,
) {
super(config, DAFF_CUSTOMER_IN_MEMORY_COLLECTION_NAME);
}

changeEmail(email: string, password: string): Observable<DaffCustomer> {
return this.http.put<DaffCustomer>(`${this.url}/email`, { email, password });
Expand Down

0 comments on commit f350814

Please sign in to comment.