Skip to content

Commit

Permalink
리팩토링 예제 코드 시작
Browse files Browse the repository at this point in the history
  • Loading branch information
jojoldu committed Nov 10, 2023
1 parent fcf8ca5 commit d3cfff7
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/product/Product.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export class Product {
private readonly id: string;
private readonly name: string;
private readonly price: number;
private readonly status: string;
private readonly description: string;
private readonly createdAt: Date;
private readonly updatedAt: Date;
private readonly deletedAt: Date;

constructor(id: string, name: string, price: number, status: string, description: string, createdAt: Date, updatedAt: Date, deletedAt: Date) {
this.id = id;
this.name = name;
this.price = price;
this.status = status;
this.description = description;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
this.deletedAt = deletedAt;
}

static newInstance (id: string, name: string, price: number, description: string) {
return new Product(
id,
name,
price,
"OPEN",
description,
new Date(),
null,
null
)
}
}
11 changes: 11 additions & 0 deletions src/product/ProductCreateDto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {Product} from "./Product";

export class ProductCreateDto {
private readonly name: string;
private readonly price: number;
private readonly description: string;

toEntity(id: string) {
return Product.newInstance(id,this.name, this.price, this.description)
}
}
42 changes: 42 additions & 0 deletions src/product/ProductService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Product } from "./Product";
import {ProductCreateDto} from "./ProductCreateDto";

export class ProductService {
async create_1 (createDtos: ProductCreateDto[]) {
const promiseList = [];
const results = [];
for(const dto of createDtos) {
const entity = dto.toEntity(generateId());
const queryParams = [];

const query = `INSERT INTO ${ CLASS_TABLE_NAME }` +
`(${ Object.keys(entity).map(k => convertCamelToSnakeName(k)) }) ` +
`VALUES ( ${convertUuidToBinParam()}, ${convertUuidToBinParam()}, ${convertUuidToBinParam()}, ?, ?, ?, ?, ?, ?, NOW(), NOW() )`;

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

await Promise.all(promiseList);
return results;
}
}

export async function save(product: Product) {
console.log(product);
}

export function generateId() {
return "uuid";
}

0 comments on commit d3cfff7

Please sign in to comment.