Skip to content

Commit

Permalink
리팩토링 순차적 진행
Browse files Browse the repository at this point in the history
  • Loading branch information
jojoldu committed Nov 11, 2023
1 parent 44fc517 commit e7c95d8
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 22 deletions.
44 changes: 23 additions & 21 deletions posts/리팩토링/좋은함수만들기_코드배치/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,37 @@

```ts
export class ProductService {
async oldCreates(classes: ClassDto[]): Promise<ClassDto[]> {
async create_1 (createDtos: ProductCreateDto[]) {
const promiseList = [];
const results = [];
for(const classItem of classes) {
const newClassEntity = classItem.toClassEntity(generateUuid());
const results: Product[] = [];
for(const dto of createDtos) {
const entity = dto.toEntity(generateId());
const queryParams = [];
const query = `INSERT INTO ${ CLASS_TABLE_NAME }` +
`(${ Object.keys(newClassEntity).map(k => convertCamelToSnakeName(k)) }) ` +
`VALUES ( ${convertUuidToBinParam()}, ${convertUuidToBinParam()}, ${convertUuidToBinParam()}, ?, ?, ?, ?, ?, ?, NOW(), NOW() )`;

const query =
`INSERT INTO product (${ Object.keys(entity) }) `
+ 'VALUES (?, ?, ?, ?, ?, NOW(), NOW() )';

queryParams.push(
newClassEntity.id,
newClassEntity.categoryId,
newClassEntity.teacherId,
newClassEntity.nameHash,
newClassEntity.name,
newClassEntity.price,
newClassEntity.state,
newClassEntity.isDeleted,
newClassEntity.description
entity.id,
entity.name,
entity.price,
entity.status,
entity.description
);
promiseList.push(this.dbConnection.query(query, queryParams))
results.push(ClassDto.fromEntity(newClassEntity));
results.push(entity);
}

await Promise.all(promiseList);
return results;
};
}
```
관련 있는 변수들을 근처로 옮긴다.
```ts

```
```ts
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/product/DbConnection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export class DbConnection {
async query(sql: string, params: string[]) {
async query(sql: string, params: any[]) {
console.log(sql);
return 1;
}
Expand Down
22 changes: 22 additions & 0 deletions src/product/ProductService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,28 @@ export class ProductService {
await Promise.all(promiseList);
return results;
}

async create_2 (createDtos: ProductCreateDto[]) {
const entites = createDtos.map(dto => dto.toEntity(generateId()));

await Promise.all(entites.map(entity => {
const query =
`INSERT INTO product (${ Object.keys(entity) }) `
+ 'VALUES (?, ?, ?, ?, ?, NOW(), NOW() )';

const params = [
entity.id,
entity.name,
entity.price,
entity.status,
entity.description
];

return this.dbConnection.query(query, params);
}));

return entites;
}
}

export async function save(product: Product) {
Expand Down

0 comments on commit e7c95d8

Please sign in to comment.