This file contains hidden or 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
| describe('CheckoutService', () => { | |
| test('checkout', async () => { | |
| const customerRepository: ICustomerRepository = { | |
| findOne: jest.fn(() => { | |
| const customer: Customer = { | |
| memberType: 'VIP', | |
| }; | |
| return customer; | |
| }), | |
| }; |
This file contains hidden or 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
| describe('CheckoutService', () => { | |
| test('checkout', async () => { | |
| const customerRepository: ICustomerRepository = { | |
| async findOne(customerId: string) { | |
| expect(customerId).toEqual('customerId'); | |
| const customer: Customer = { | |
| memberType: 'VIP', | |
| }; | |
| return customer; | |
| }, |
This file contains hidden or 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
| export class CheckoutService { | |
| ... | |
| async checkout(customerId: string) { | |
| const customer = await this.customerRepository.findOne(customerId); | |
| if (customer === null) { | |
| throw new Error('Customer not found'); | |
| } | |
| const shoppingItems = await this.shoppingItemRepository.findMany({ |
This file contains hidden or 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
| router.post('/customer/:customerId/checkout', async (req, res) => { | |
| const { customerId } = req.params; | |
| const checkoutService = new CheckoutService( | |
| Customer, | |
| ShoppingItem, | |
| StockItem, | |
| CreditCardService, | |
| ); | |
| const { | |
| totalPrice, |
This file contains hidden or 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
| class CheckoutService { | |
| constructor( | |
| private readonly customerRepository: ICustomerRepository, | |
| private readonly shoppingItemRepository: IShoppingItemRepository, | |
| private readonly stockItemRepository: IStockItemRepository, | |
| private readonly creditCardService: ICreditCardService, | |
| ) {} | |
| async checkout(customerId: string) { | |
| const customer = await this.customerRepository.findOne(customerId); |
This file contains hidden or 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
| class CheckoutService { | |
| customerRepository: ICustomerRepository; | |
| shoppingItemRepository: IShoppingItemRepository; | |
| stockItemRepository: IStockItemRepository; | |
| creditCardService: ICreditCardService; | |
| constructor( | |
| customerRepository: ICustomerRepository, | |
| shoppingItemRepository: IShoppingItemRepository, | |
| stockItemRepository: IStockItemRepository, |
This file contains hidden or 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
| interface ICustomerRepository { | |
| findOne(customerId: string): Promise<Customer>; | |
| } | |
| interface IShoppingItemRepository { | |
| findMany(where: any): Promise<ShoppingItem[]>; | |
| } | |
| interface IStockItemRepository { | |
| findMany(where: any): Promise<StockItem[]>; | |
| update(where: any, value: any): Promise<void>; | |
| } |
This file contains hidden or 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
| class CheckoutService { | |
| customerRepository: any; | |
| shoppingItemRepository: any; | |
| stockItemRepository: any; | |
| creditCardService: any; | |
| async checkout(customerId: string) { | |
| const customer = await this.customerRepository.findOne(customerId); | |
| if (customer === null) { | |
| throw new Error('Customer not found'); |
This file contains hidden or 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
| class CheckoutService { | |
| async checkout(customerId: string) { | |
| const customer = await Customer.findOne(customerId); | |
| if (customer === null) { | |
| throw new Error('Customer not found'); | |
| } | |
| const shoppingItems = await ShoppingItem.findMany( | |
| { customerId } | |
| ); | |
| const itemIds = shoppingItems.map(item => item.itemId); |
This file contains hidden or 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
| class CheckoutService { | |
| async checkout(customerId: string) { | |
| const customer = await Customer.findOne(customerId); | |
| if (customer === null) { | |
| throw new Error('Customer not found'); | |
| } | |
| const shoppingItems = await ShoppingItem.findMany( | |
| { customerId } | |
| ); | |
| const itemIds = shoppingItems.map(item => item.itemId); |
NewerOlder