Skip to content

Commit a85224a

Browse files
committed
refactoring: refactor
1 parent b6a416a commit a85224a

31 files changed

+421
-773
lines changed

env/development.env

100644100755
+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
NODE_ENV=development
33

44
# Server
5-
PORT=3000
5+
PORT=7000
66
HOST=localhost

src/adapters/gateways/base-gateway.ts

+57-12
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,66 @@
1-
import { UnitOfWork, Entity } from '@entities';
1+
import { Entity, UniqueEntityID } from '@entities';
2+
import MapperRegistry from '../../entities/mapper-registry';
3+
import { UnitOfWork } from './unit-of-work';
24

3-
export default interface Repository<T> {
4-
remove(t: T): Promise<void>;
5-
removeCollection(t: T): Promise<void>;
6-
save(t: T): Promise<void>;
7-
saveCollection(t: Array<T>): Promise<void>
5+
export interface Gateway {
6+
startTransaction(): void;
7+
abstractFindAll(entityName: string, criteria: any): Promise<Entity<any>[]>;
8+
abstractFind(entityName: string, id: UniqueEntityID): Promise<Entity<any>>;
9+
endTransaction(): void;
10+
remove(e: Entity<any>): Promise<void>;
11+
removeCollection(entities: Entity<any>[]): Promise<void>;
12+
save(e: Entity<any>): Promise<void>;
13+
saveCollection(entities: Entity<any>[]): Promise<void>
814
}
915

10-
export class BaseGateway {
16+
export class BaseGateway implements Gateway {
1117
private uow: UnitOfWork | undefined;
1218

1319
public startTransaction() {
1420
this.uow = new UnitOfWork();
1521
}
1622

17-
public save(e: Entity<any>) {
23+
public async abstractFind(entityName: string, id: UniqueEntityID): Promise<Entity<any>> {
24+
let entity = this.uow.load(entityName, id);
25+
26+
if(!entity) {
27+
entity = await MapperRegistry.getEntiyMapper(entityName).find({id: id.toValue()});
28+
}
29+
30+
if(!entity) {
31+
return null;
32+
}
33+
34+
this.uow.registerClean(entity);
35+
return entity;
36+
}
37+
38+
public async abstractFindAll(entityName: string, criteria: any): Promise<Entity<any>[]> {
39+
const reload = (entity: Entity<any>) => {
40+
if (!this.uow)
41+
entity;
42+
43+
const loaded = this.uow.load(entityName, entity.id);
44+
45+
if (!loaded) {
46+
this.uow.registerClean(loaded);
47+
return entity;
48+
}
49+
50+
return loaded;
51+
}
52+
53+
let entities = await MapperRegistry.getEntiyMapper(entityName).findAll(criteria);
54+
55+
for (let i = 0; i < entities.length; i++) {
56+
entities[i] = reload(entities[i]);
57+
}
58+
59+
return entities;
60+
}
61+
62+
63+
public async save(e: Entity<any>) {
1864
if (this.uow.isNew(e) || this.uow.isClean(e)) {
1965
this.uow.registerDirty(e);
2066
return;
@@ -23,17 +69,17 @@ export class BaseGateway {
2369
this.uow.registerNew(e);
2470
}
2571

26-
public saveCollection(entities: Entity<any>[]) {
72+
public async saveCollection(entities: Entity<any>[]) {
2773
for (const e of entities) {
2874
this.save(e);
2975
}
3076
}
3177

32-
public remove(e: Entity<any>) {
78+
public async remove(e: Entity<any>) {
3379
this.uow.registerRemoved(e);
3480
}
3581

36-
public removeCollection(entities: Entity<any>[]) {
82+
public async removeCollection(entities: Entity<any>[]) {
3783
for (const e of entities) {
3884
this.remove(e);
3985
}
@@ -43,5 +89,4 @@ export class BaseGateway {
4389
await this.uow.commit();
4490
this.uow = undefined;
4591
}
46-
4792
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Customer, UniqueEntityID } from '@entities';
2+
import GatewayDecorator from './gateway-decorator';
3+
4+
export default class CustomerDecorator extends GatewayDecorator {
5+
public async findCustomerById(customerId: UniqueEntityID): Promise<Customer> {
6+
const customer = await this.abstractFind('Customer', customerId);
7+
return customer as Customer;
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
import { UniqueEntityID, Entity } from '@entities';
3+
import { Gateway } from '../base-gateway';
4+
5+
export default class GatewayDecorator implements Gateway {
6+
protected _baseGateway: Gateway;
7+
8+
constructor(gateway: Gateway) {
9+
this._baseGateway = gateway;
10+
}
11+
12+
public async abstractFindAll(entity: string, criteria: any): Promise<Entity<any>[]> {
13+
return this._baseGateway.abstractFindAll(entity, criteria);
14+
}
15+
16+
public async abstractFind(entity: string, id: UniqueEntityID) {
17+
return this._baseGateway.abstractFind(entity, id);
18+
}
19+
20+
public startTransaction() {
21+
this._baseGateway.startTransaction();
22+
}
23+
24+
public async save(e: Entity<any>) {
25+
this._baseGateway.save(e);
26+
}
27+
28+
public async saveCollection(entities: Entity<any>[]) {
29+
this._baseGateway.saveCollection(entities);
30+
}
31+
32+
public async remove(e: Entity<any>) {
33+
this._baseGateway.remove(e);
34+
}
35+
36+
public async removeCollection(entities: Entity<any>[]) {
37+
this._baseGateway.removeCollection(entities);
38+
}
39+
40+
public async endTransaction() {
41+
this._baseGateway.endTransaction();
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Order, UniqueEntityID } from '@entities';
2+
import GatewayDecorator from './gateway-decorator';
3+
4+
export default class OrderDecorator extends GatewayDecorator {
5+
public async findOrderById(orderId: UniqueEntityID): Promise<Order> {
6+
const order = await this.abstractFind('Order', orderId);
7+
return order as Order;
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Product, UniqueEntityID } from '@entities';
2+
import GatewayDecorator from './gateway-decorator';
3+
4+
export default class ProductDecorator extends GatewayDecorator {
5+
public async findProductById(productId: UniqueEntityID): Promise<Product> {
6+
const product = await this.abstractFind('Product', productId);
7+
return product as Product;
8+
}
9+
}
+10-41
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,15 @@
11
import DetailOrderGateway from '../../application/use-cases/detail-order/detail-order.gateway';
2-
import { UniqueEntityID } from '@entities';
3-
import { Customer, Order, Product} from '@entities';
2+
import { Gateway, BaseGateway } from './base-gateway';
3+
import CustomerDecorator from './decorators/customer-decorator';
4+
import ProductDecorator from './decorators/product-decorator.rep';
5+
import OrderDecorator from './decorators/oder-decorator';
46

57

6-
interface CustomerRepositoryForDetailOrder {
7-
getCustomerById(customerId: UniqueEntityID): Promise<Customer>
8-
};
8+
let baseGateway: DetailOrderGateway;
9+
const g1 = new BaseGateway();
10+
const g2 = new CustomerDecorator(g1);
11+
const g3 = new ProductDecorator(g2);
12+
baseGateway = new OrderDecorator(g3);
913

10-
interface ProductRepositoryForDetailOrder {
11-
getProductById(productId: UniqueEntityID): Promise<Product>
12-
};
14+
detailOrderGateway.fin
1315

14-
15-
interface OrderRepositoryForDetailOrder {
16-
getOrderById(customerId: UniqueEntityID): Promise<Order>
17-
};
18-
19-
20-
export default class DetailOrderGatewayImpl implements DetailOrderGateway {
21-
private _customerRep: CustomerRepositoryForDetailOrder;
22-
private _productRep: ProductRepositoryForDetailOrder;
23-
private _orderRep: OrderRepositoryForDetailOrder;
24-
25-
constructor(
26-
customerRep: CustomerRepositoryForDetailOrder,
27-
productRep: ProductRepositoryForDetailOrder,
28-
orderRep: OrderRepositoryForDetailOrder
29-
) {
30-
this._customerRep = customerRep;
31-
this._productRep = productRep;
32-
this._orderRep = orderRep;
33-
}
34-
35-
async getCustomerById(customerId: UniqueEntityID): Promise<Customer> {
36-
return this._customerRep.getCustomerById(customerId);
37-
};
38-
39-
getProductById(productId: UniqueEntityID): Promise<Product> {
40-
return this._productRep.getProductById(productId);
41-
}
42-
43-
getOrderById(orderId: UniqueEntityID): Promise<Order> {
44-
return this._orderRep.getOrderById(orderId);
45-
}
46-
}

0 commit comments

Comments
 (0)