-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Completed documentation for the WebApi definition
- Loading branch information
1 parent
bca1ace
commit 4394de0
Showing
6 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
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
62 changes: 62 additions & 0 deletions
62
docs/snippets/gettingStarted/webApi/shoppingCartEndpointWithOn.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,62 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import { | ||
CommandHandler, | ||
assertNotEmptyString, | ||
assertPositiveNumber, | ||
getInMemoryEventStore, | ||
} from '@event-driven-io/emmett'; | ||
import { NoContent, on } from '@event-driven-io/emmett-expressjs'; | ||
import { Router, type Request } from 'express'; | ||
import { addProductItem } from '../businessLogic'; | ||
import type { AddProductItemToShoppingCart } from '../commands'; | ||
import { evolve, getInitialState } from '../shoppingCart'; | ||
import { getShoppingCartId } from './simpleApi'; | ||
|
||
const handle = CommandHandler(evolve, getInitialState); | ||
const router: Router = Router(); | ||
|
||
const getUnitPrice = (_productId: string) => { | ||
return Promise.resolve(100); | ||
}; | ||
|
||
const eventStore = getInMemoryEventStore(); | ||
|
||
type AddProductItemRequest = Request< | ||
Partial<{ clientId: string; shoppingCartId: string }>, | ||
unknown, | ||
Partial<{ productId: number; quantity: number }> | ||
>; | ||
|
||
// #region getting-started-on-router | ||
router.post( | ||
'/clients/:clientId/shopping-carts/current/product-items', | ||
on(async (request: AddProductItemRequest) => { | ||
// 1. Translate request params to the command | ||
const shoppingCartId = getShoppingCartId( | ||
assertNotEmptyString(request.params.clientId), | ||
); | ||
const productId = assertNotEmptyString(request.body.productId); | ||
|
||
const command: AddProductItemToShoppingCart = { | ||
type: 'AddProductItemToShoppingCart', | ||
data: { | ||
shoppingCartId, | ||
productItem: { | ||
productId, | ||
quantity: assertPositiveNumber(request.body.quantity), | ||
unitPrice: await getUnitPrice(productId), | ||
}, | ||
}, | ||
}; | ||
|
||
// 2. Handle command | ||
await handle(eventStore, shoppingCartId, (state) => | ||
addProductItem(command, state), | ||
); | ||
|
||
// 3. Return response status | ||
return NoContent(); | ||
}), | ||
); | ||
|
||
// #endregion getting-started-on-router |
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 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