Skip to content

Commit ec67f9c

Browse files
committed
Got rid of mappers, shoppingCartRepository and query handlers
1 parent 6a8952a commit ec67f9c

File tree

9 files changed

+144
-284
lines changed

9 files changed

+144
-284
lines changed

samples/unwrapping_onion/src/unpeeled/ecommerce/app.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { getApplication } from '#core/api';
22
import { MongoClient } from 'mongodb';
3-
import registerHandlers from './shoppingCarts/application';
43
import { getControllers } from './shoppingCarts/controllers';
54

65
const initApp = (mongo: MongoClient) => {
7-
registerHandlers(mongo);
86
return getApplication(getControllers(mongo));
97
};
108

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

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

samples/unwrapping_onion/src/unpeeled/ecommerce/shoppingCarts/application/queryHandlers/getCustomerShoppingHistoryHandler.ts

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

samples/unwrapping_onion/src/unpeeled/ecommerce/shoppingCarts/application/queryHandlers/getShoppingCartByIdQueryHandler.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import { EventBusFactory } from '#core/events';
2-
import { QueryBusFactory } from '#core/queries';
32
import { MongoClient } from 'mongodb';
43
import { ShoppingCartController } from './shoppingCartController';
54

65
export const getControllers = (mongo: MongoClient) => {
7-
return [
8-
new ShoppingCartController(mongo, EventBusFactory(), QueryBusFactory())
9-
.router,
10-
];
6+
return [new ShoppingCartController(mongo, EventBusFactory()).router];
117
};

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

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,14 @@ import {
1818
RemoveProductItemFromShoppingCart,
1919
ConfirmShoppingCart,
2020
} from '../commands';
21-
import { getCollection } from '#core/repositories';
22-
import {
23-
getShoppingCart,
24-
store,
25-
} from '../infrastructure/shoppingCartRepository';
21+
import { getById, getCollection } from '#core/repositories';
22+
import { findAllByCustomerId, getShoppingCart, store } from '../storage';
2623

2724
export class ShoppingCartController {
2825
public router = Router();
2926
private carts: Collection<ShoppingCartModel>;
3027

31-
constructor(
32-
mongo: MongoClient,
33-
private eventBus: EventBus,
34-
private queryBus: QueryBus
35-
) {
28+
constructor(mongo: MongoClient, private eventBus: EventBus) {
3629
this.carts = getCollection<ShoppingCartModel>(mongo, 'shoppingCarts');
3730
this.router.post('/customers/:customerId/shopping-carts/', this.open);
3831
this.router.post(
@@ -109,7 +102,7 @@ export class ShoppingCartController {
109102

110103
const event = aggregate.addProductItem(command.data.productItem);
111104

112-
await store(this.carts, event, aggregate);
105+
await store(this.carts, event);
113106
await this.eventBus.publish(event);
114107

115108
response.sendStatus(200);
@@ -142,7 +135,7 @@ export class ShoppingCartController {
142135

143136
const event = aggregate.removeProductItem(command.data.productItem);
144137

145-
await store(this.carts, event, aggregate);
138+
await store(this.carts, event);
146139
await this.eventBus.publish(event);
147140

148141
response.sendStatus(200);
@@ -171,7 +164,7 @@ export class ShoppingCartController {
171164

172165
const event = aggregate.confirm();
173166

174-
await store(this.carts, event, aggregate);
167+
await store(this.carts, event);
175168
await this.eventBus.publish(event);
176169

177170
response.sendStatus(200);
@@ -190,7 +183,7 @@ export class ShoppingCartController {
190183
const query = new GetShoppingCartById(
191184
assertNotEmptyString(request.params.shoppingCartId)
192185
);
193-
const result = await this.queryBus.query(query);
186+
const result = await getById(this.carts, query.shoppingCartId);
194187

195188
response.send(result);
196189
} catch (error) {
@@ -208,7 +201,7 @@ export class ShoppingCartController {
208201
const query = new GetCustomerShoppingHistory(
209202
assertNotEmptyString(request.params.customerId)
210203
);
211-
const result = await this.queryBus.query(query);
204+
const result = await findAllByCustomerId(this.carts, query.customerId);
212205

213206
response.send(result);
214207
} catch (error) {

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

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@ import {
99
export class ShoppingCart {
1010
public constructor(
1111
public id: string,
12-
public customerId: string,
1312
public status: string,
14-
public productItems: ProductItem[],
15-
public openedAt: Date,
16-
public confirmedAt: Date | undefined,
13+
public productItems: Map<string, number>,
1714
public revision: number
1815
) {}
1916

@@ -54,12 +51,9 @@ export class ShoppingCart {
5451

5552
const { productId, quantity } = productItemToRemove;
5653

57-
const currentProductItem = this.findProductItem(
58-
this.productItems,
59-
productId
60-
);
54+
const currentQuantity = this.productItems.get(productId);
6155

62-
const newQuantity = (currentProductItem?.quantity ?? 0) - quantity;
56+
const newQuantity = (currentQuantity ?? 0) - quantity;
6357

6458
if (newQuantity < 0) throw new Error('Product Item not found');
6559

@@ -85,13 +79,6 @@ export class ShoppingCart {
8579
},
8680
};
8781
}
88-
89-
private findProductItem(
90-
productItems: ProductItem[],
91-
productId: string
92-
): ProductItem | undefined {
93-
return productItems.find((pi) => pi.productId === productId);
94-
}
9582
}
9683

9784
export const ShoppingCartStatus = {

samples/unwrapping_onion/src/unpeeled/ecommerce/shoppingCarts/infrastructure/shoppingCartRepository.ts

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

0 commit comments

Comments
 (0)