-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from blockydevs/switch-to-adapter-architecture
switch to adapter architecture pattern
- Loading branch information
Showing
23 changed files
with
137 additions
and
200 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,25 @@ | ||
import { Injectable, NotFoundException } from '@nestjs/common'; | ||
import { HttpService } from '@nestjs/axios'; | ||
import { Inject, Injectable, NotFoundException } from '@nestjs/common'; | ||
import { Product } from './product.interface'; | ||
import { firstValueFrom } from 'rxjs'; | ||
import { StoreManagerPort } from '../store-manager/store-manager.port'; | ||
|
||
@Injectable() | ||
export class ProductService { | ||
constructor(private readonly httpService: HttpService) {} | ||
constructor( | ||
@Inject(StoreManagerPort) | ||
private readonly storeManagerPort: StoreManagerPort, | ||
) {} | ||
|
||
public async getProductData(productId: number): Promise<Product> { | ||
const { data } = await firstValueFrom( | ||
this.httpService.get(`https://fakestoreapi.com/products/${productId}`), | ||
const productData = await this.storeManagerPort.getProductData( | ||
productId.toString(), | ||
); | ||
|
||
if (!data) { | ||
if (!productData) { | ||
console.log(`Product ${productId} not found`); | ||
throw new NotFoundException( | ||
`Product with id ${productId} not found in the external API.`, | ||
); | ||
} | ||
|
||
return data; | ||
return productData; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/store-manager/fake-api/fake-api-store-manager.adapter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { StoreManagerPort } from '../store-manager.port'; | ||
import { firstValueFrom } from 'rxjs'; | ||
import { Cart } from '../../cart/cart.interface'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { HttpService } from '@nestjs/axios'; | ||
import { Product } from '../../product/product.interface'; | ||
|
||
@Injectable() | ||
export class FakeApiStoreManagerAdapter implements StoreManagerPort { | ||
constructor(private readonly httpService: HttpService) {} | ||
|
||
public async getCartData(cartId: string): Promise<Cart> { | ||
const { data } = await firstValueFrom( | ||
this.httpService.get(`https://fakestoreapi.com/carts/${cartId}`), | ||
); | ||
|
||
return data as Cart; | ||
} | ||
|
||
public async getProductData(productId: string): Promise<Product> { | ||
const { data } = await firstValueFrom( | ||
this.httpService.get(`https://fakestoreapi.com/products/${productId}`), | ||
); | ||
return data; | ||
} | ||
} |
Oops, something went wrong.