Skip to content

Commit afc9d1c

Browse files
committed
Replaced aggregate with entity and methods
1 parent ec67f9c commit afc9d1c

File tree

6 files changed

+93
-98
lines changed

6 files changed

+93
-98
lines changed

samples/unwrapping_onion/src/unpeeled/ecommerce/shoppingCarts/application/mappers/customerShoppingHistoryMapper.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

samples/unwrapping_onion/src/unpeeled/ecommerce/shoppingCarts/controllers/shoppingCartController.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@ import { QueryBus } from '#core/queries';
55
import { GetShoppingCartById } from 'src/unpeeled/ecommerce/shoppingCarts/application/queries/getShoppingCartById';
66
import { assertNotEmptyString, assertPositiveNumber } from '#core/validation';
77
import { AddProductItemToShoppingCartRequest } from 'src/unpeeled/ecommerce/shoppingCarts/requests/addProductItemToShoppingCartRequest';
8-
import { ProductItem } from 'src/unpeeled/ecommerce/shoppingCarts/common/productItem';
8+
import { ProductItem } from 'src/unpeeled/ecommerce/shoppingCarts/productItems/productItem';
99
import { RemoveProductItemFromShoppingCartRequest } from 'src/unpeeled/ecommerce/shoppingCarts/requests/removeProductItemFromShoppingCartRequest.ts';
1010
import { GetCustomerShoppingHistory } from 'src/unpeeled/ecommerce/shoppingCarts/application/queries/getCustomerShoppingHistory';
11-
import { ShoppingCart } from 'src/unpeeled/ecommerce/shoppingCarts';
11+
import {
12+
addProductItem as addProductItemToShoppingCart,
13+
confirm as confirmShoppingCart,
14+
open as openShoppingCart,
15+
removeProductItem as removeProductItemFromShoppingCart,
16+
ShoppingCart,
17+
} from 'src/unpeeled/ecommerce/shoppingCarts';
1218
import { EventBus } from '#core/events';
1319
import { ShoppingCartModel } from '../models/shoppingCart';
1420
import { Collection, MongoClient } from 'mongodb';
@@ -64,7 +70,7 @@ export class ShoppingCartController {
6470
},
6571
};
6672

67-
const event = ShoppingCart.open(
73+
const event = openShoppingCart(
6874
command.data.shoppingCartId,
6975
command.data.customerId
7076
);
@@ -95,12 +101,15 @@ export class ShoppingCartController {
95101
),
96102
},
97103
};
98-
const aggregate = await getShoppingCart(
104+
const cart = await getShoppingCart(
99105
this.carts,
100106
command.data.shoppingCartId
101107
);
102108

103-
const event = aggregate.addProductItem(command.data.productItem);
109+
const event = addProductItemToShoppingCart(
110+
cart,
111+
command.data.productItem
112+
);
104113

105114
await store(this.carts, event);
106115
await this.eventBus.publish(event);
@@ -128,12 +137,15 @@ export class ShoppingCartController {
128137
),
129138
},
130139
};
131-
const aggregate = await getShoppingCart(
140+
const cart = await getShoppingCart(
132141
this.carts,
133142
command.data.shoppingCartId
134143
);
135144

136-
const event = aggregate.removeProductItem(command.data.productItem);
145+
const event = removeProductItemFromShoppingCart(
146+
cart,
147+
command.data.productItem
148+
);
137149

138150
await store(this.carts, event);
139151
await this.eventBus.publish(event);
@@ -157,12 +169,12 @@ export class ShoppingCartController {
157169
shoppingCartId: assertNotEmptyString(request.params.shoppingCartId),
158170
},
159171
};
160-
const aggregate = await getShoppingCart(
172+
const cart = await getShoppingCart(
161173
this.carts,
162174
command.data.shoppingCartId
163175
);
164176

165-
const event = aggregate.confirm();
177+
const event = confirmShoppingCart(cart);
166178

167179
await store(this.carts, event);
168180
await this.eventBus.publish(event);

samples/unwrapping_onion/src/unpeeled/ecommerce/shoppingCarts/index.ts

Lines changed: 63 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,85 @@
1-
import { ProductItem } from 'src/unpeeled/ecommerce/shoppingCarts/common/productItem';
1+
import { ProductItem } from 'src/unpeeled/ecommerce/shoppingCarts/productItems/productItem';
22
import {
33
ProductItemAddedToShoppingCart,
44
ProductItemRemovedFromShoppingCart,
55
ShoppingCartConfirmed,
66
ShoppingCartOpened,
77
} from './events';
88

9-
export class ShoppingCart {
10-
public constructor(
11-
public id: string,
12-
public status: string,
13-
public productItems: Map<string, number>,
14-
public revision: number
15-
) {}
9+
export type ShoppingCart = Readonly<{
10+
id: string;
11+
status: string;
12+
productItems: Map<string, number>;
13+
revision: number;
14+
}>;
1615

17-
public static open(id: string, customerId: string): ShoppingCartOpened {
18-
return {
19-
type: 'shopping-cart-opened',
20-
data: {
21-
shoppingCartId: id,
22-
customerId,
23-
openedAt: new Date(),
24-
},
25-
};
16+
export const open = (id: string, customerId: string): ShoppingCartOpened => {
17+
return {
18+
type: 'shopping-cart-opened',
19+
data: {
20+
shoppingCartId: id,
21+
customerId,
22+
openedAt: new Date(),
23+
},
24+
};
25+
};
26+
27+
export const addProductItem = (
28+
cart: ShoppingCart,
29+
newProductItem: ProductItem
30+
): ProductItemAddedToShoppingCart => {
31+
if (cart.status !== ShoppingCartStatus.Opened) {
32+
throw Error('Cannot add product to not opened shopping cart');
2633
}
2734

28-
public addProductItem(
29-
newProductItem: ProductItem
30-
): ProductItemAddedToShoppingCart {
31-
if (this.status !== ShoppingCartStatus.Opened) {
32-
throw Error('Cannot add product to not opened shopping cart');
33-
}
35+
return {
36+
type: 'product-item-added-to-shopping-cart',
37+
data: {
38+
shoppingCartId: cart.id,
39+
productItem: newProductItem,
40+
addedAt: new Date(),
41+
},
42+
};
43+
};
3444

35-
return {
36-
type: 'product-item-added-to-shopping-cart',
37-
data: {
38-
shoppingCartId: this.id,
39-
productItem: newProductItem,
40-
addedAt: new Date(),
41-
},
42-
};
45+
export const removeProductItem = (
46+
cart: ShoppingCart,
47+
productItemToRemove: ProductItem
48+
): ProductItemRemovedFromShoppingCart => {
49+
if (cart.status !== ShoppingCartStatus.Opened) {
50+
throw Error('Cannot remove product from not opened shopping cart');
4351
}
4452

45-
public removeProductItem(
46-
productItemToRemove: ProductItem
47-
): ProductItemRemovedFromShoppingCart {
48-
if (this.status !== ShoppingCartStatus.Opened) {
49-
throw Error('Cannot remove product from not opened shopping cart');
50-
}
51-
52-
const { productId, quantity } = productItemToRemove;
53+
const { productId, quantity } = productItemToRemove;
5354

54-
const currentQuantity = this.productItems.get(productId);
55+
const currentQuantity = cart.productItems.get(productId);
5556

56-
const newQuantity = (currentQuantity ?? 0) - quantity;
57+
const newQuantity = (currentQuantity ?? 0) - quantity;
5758

58-
if (newQuantity < 0) throw new Error('Product Item not found');
59+
if (newQuantity < 0) throw new Error('Product Item not found');
5960

60-
return {
61-
type: 'product-item-removed-from-shopping-cart',
62-
data: {
63-
shoppingCartId: this.id,
64-
productItem: productItemToRemove,
65-
removedAt: new Date(),
66-
},
67-
};
68-
}
61+
return {
62+
type: 'product-item-removed-from-shopping-cart',
63+
data: {
64+
shoppingCartId: cart.id,
65+
productItem: productItemToRemove,
66+
removedAt: new Date(),
67+
},
68+
};
69+
};
6970

70-
public confirm(): ShoppingCartConfirmed {
71-
if (this.status !== ShoppingCartStatus.Opened) {
72-
throw Error('Cannot confirm to not opened shopping cart');
73-
}
74-
return {
75-
type: 'shopping-cart-confirmed',
76-
data: {
77-
shoppingCartId: this.id,
78-
confirmedAt: new Date(),
79-
},
80-
};
71+
export const confirm = (cart: ShoppingCart): ShoppingCartConfirmed => {
72+
if (cart.status !== ShoppingCartStatus.Opened) {
73+
throw Error('Cannot confirm to not opened shopping cart');
8174
}
82-
}
75+
return {
76+
type: 'shopping-cart-confirmed',
77+
data: {
78+
shoppingCartId: cart.id,
79+
confirmedAt: new Date(),
80+
},
81+
};
82+
};
8383

8484
export const ShoppingCartStatus = {
8585
Opened: 'Opened',

samples/unwrapping_onion/src/unpeeled/ecommerce/shoppingCarts/models/shoppingCart.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ObjectId } from 'mongodb';
2-
import { ProductItem } from 'src/unpeeled/ecommerce/shoppingCarts/common/productItem';
2+
import { ProductItem } from 'src/unpeeled/ecommerce/shoppingCarts/productItems/productItem';
33

44
export class ShoppingCartModel {
55
constructor(

samples/unwrapping_onion/src/unpeeled/ecommerce/shoppingCarts/storage/index.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ export const getShoppingCart = async (
1111
): Promise<ShoppingCart> => {
1212
const model = await getById(carts, id);
1313

14-
return new ShoppingCart(
15-
model._id.toHexString(),
16-
model.status,
17-
new Map(model.productItems.map((p) => [p.productId, p.quantity])),
18-
model.revision
19-
);
14+
return {
15+
id: model._id.toHexString(),
16+
status: model.status,
17+
productItems: new Map(
18+
model.productItems.map((p) => [p.productId, p.quantity])
19+
),
20+
revision: model.revision,
21+
};
2022
};
2123

2224
export const findAllByCustomerId = async (

0 commit comments

Comments
 (0)