-
-
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.
Added description of the Web API definition
- Loading branch information
1 parent
c7d87ec
commit bca1ace
Showing
7 changed files
with
286 additions
and
109 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import { shoppingCartApi } from './simpleApi'; | ||
|
||
const getUnitPrice = (_productId: string) => { | ||
return Promise.resolve(100); | ||
}; | ||
|
||
// #region getting-started-api-setup | ||
import { getInMemoryEventStore } from '@event-driven-io/emmett'; | ||
|
||
const eventStore = getInMemoryEventStore(); | ||
|
||
const shoppingCarts = shoppingCartApi(eventStore); | ||
const shoppingCarts = shoppingCartApi(eventStore, getUnitPrice); | ||
// #endregion getting-started-api-setup |
76 changes: 76 additions & 0 deletions
76
docs/snippets/gettingStarted/webApi/shoppingCartApiSetup.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,76 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import { | ||
CommandHandler, | ||
assertNotEmptyString, | ||
assertPositiveNumber, | ||
getInMemoryEventStore, | ||
type EventStore, | ||
} from '@event-driven-io/emmett'; | ||
import { NoContent, on } from '@event-driven-io/emmett-expressjs'; | ||
import { addProductItem } from '../businessLogic'; | ||
import type { AddProductItemToShoppingCart } from '../commands'; | ||
import { getShoppingCartId } from './simpleApi'; | ||
|
||
// #region getting-started-api-setup | ||
import { type WebApiSetup } from '@event-driven-io/emmett-expressjs'; | ||
import { Router } from 'express'; | ||
import { evolve, getInitialState } from '../shoppingCart'; | ||
|
||
// Let's setup the command handler, we'll use it in endpoints | ||
const handle = CommandHandler(evolve, getInitialState); | ||
|
||
export const shoppingCartApi = | ||
( | ||
// external dependencies | ||
eventStore: EventStore, | ||
getUnitPrice: (productId: string) => Promise<number>, | ||
): WebApiSetup => | ||
(router: Router): void => { | ||
// We'll setup routes here | ||
}; | ||
|
||
// #endregion getting-started-api-setup | ||
|
||
const router: Router = Router(); | ||
|
||
const getUnitPrice = (_productId: string) => { | ||
return Promise.resolve(100); | ||
}; | ||
|
||
const eventStore = getInMemoryEventStore(); | ||
|
||
import { type Request } from 'express'; | ||
|
||
type AddProductItemRequest = Request< | ||
Partial<{ clientId: string; shoppingCartId: string }>, | ||
unknown, | ||
Partial<{ productId: number; quantity: number }> | ||
>; | ||
|
||
router.post( | ||
'/clients/:clientId/shopping-carts/current/product-items', | ||
on(async (request: AddProductItemRequest) => { | ||
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), | ||
}, | ||
}, | ||
}; | ||
|
||
await handle(eventStore, shoppingCartId, (state) => | ||
addProductItem(command, state), | ||
); | ||
|
||
return NoContent(); | ||
}), | ||
); |
61 changes: 61 additions & 0 deletions
61
docs/snippets/gettingStarted/webApi/shoppingCartEndpointVanilla.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,61 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import { | ||
assertNotEmptyString, | ||
assertPositiveNumber, | ||
getInMemoryEventStore, | ||
} from '@event-driven-io/emmett'; | ||
import { addProductItem } from '../businessLogic'; | ||
import type { AddProductItemToShoppingCart } from '../commands'; | ||
import { getShoppingCartId, handle } from './simpleApi'; | ||
|
||
import { Router } from 'express'; | ||
|
||
const router: Router = Router(); | ||
|
||
const getUnitPrice = (_productId: string) => { | ||
return Promise.resolve(100); | ||
}; | ||
|
||
const eventStore = getInMemoryEventStore(); | ||
|
||
// #region getting-started-vanilla-router | ||
import type { Request, Response } from 'express'; | ||
|
||
type AddProductItemRequest = Request< | ||
Partial<{ clientId: string; shoppingCartId: string }>, | ||
unknown, | ||
Partial<{ productId: number; quantity: number }> | ||
>; | ||
|
||
router.post( | ||
'/clients/:clientId/shopping-carts/current/product-items', | ||
async (request: AddProductItemRequest, response: Response) => { | ||
// 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. Send response status | ||
response.sendStatus(204); | ||
}, | ||
); | ||
|
||
// #endregion getting-started-vanilla-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
Oops, something went wrong.