@@ -15,7 +15,6 @@ import {
1515import { UsageService } from '../usage/usage.service' ;
1616import { FileService } from './file.service' ;
1717import { TableService } from '../table/table.service' ;
18- import { ResTableDto } from '../table/dto/res-table.dto' ;
1918import { Connection } from 'mysql2/promise' ;
2019
2120@Injectable ( )
@@ -26,51 +25,13 @@ export class RecordService {
2625 private readonly fileService : FileService ,
2726 ) { }
2827
29- async validateDto (
30- createRandomRecordDto : CreateRandomRecordDto ,
31- connection : Connection ,
32- ) {
33- const tableInfo = await this . tableService . find (
34- connection ,
35- createRandomRecordDto . tableName ,
36- ) ;
37-
38- if ( ! tableInfo ?. tableName )
39- throw new BadRequestException (
40- `${ createRandomRecordDto . tableName } 테이블이 존재하지 않습니다.` ,
41- ) ;
42-
43- const baseColumns = tableInfo . columns ;
44- const columnInfos : RandomColumnInfo [ ] = createRandomRecordDto . columns ;
45-
46- columnInfos . forEach ( ( columnInfo ) => {
47- const targetName = columnInfo . name ;
48- const targetDomain = columnInfo . type ;
49- const baseColumn = baseColumns . find (
50- ( column ) => column . name === columnInfo . name ,
51- ) ;
52-
53- if ( ! baseColumn )
54- throw new BadRequestException (
55- `${ targetName } 컬럼이 ${ createRandomRecordDto . tableName } 에 존재하지 않습니다.` ,
56- ) ;
57-
58- if ( ! this . checkDomainAvailability ( baseColumn . type , targetDomain ) )
59- throw new BadRequestException (
60- `${ targetName } (${ baseColumn . type } ) 컬럼에 ${ targetDomain } 랜덤 값을 넣을 수 없습니다.` ,
61- ) ;
62- } ) ;
63- }
64-
65- checkDomainAvailability ( mysqlType : string , targetDomain : string ) {
66- const baseType = mysqlToJsType ( mysqlType ) ;
67- const targetType = DomainToTypes [ targetDomain ] ;
68- return ! ( baseType === 'number' && targetType === 'string' ) ;
69- }
7028 async insertRandomRecord (
71- req : any ,
29+ connection : Connection ,
30+ sessionId : string ,
7231 createRandomRecordDto : CreateRandomRecordDto ,
7332 ) : Promise < ResRecordDto > {
33+ await this . validateDto ( createRandomRecordDto , connection ) ;
34+
7435 const columnEntities : RandomColumnModel [ ] = createRandomRecordDto . columns
7536 . filter ( ( column ) => column . type !== 'default' )
7637 . map ( ( column ) => this . toEntity ( column ) ) ;
@@ -81,22 +42,28 @@ export class RecordService {
8142 createRandomRecordDto . count ,
8243 ) ;
8344 const affectedRows = await this . fileService . loadCsvToDB (
84- req ,
45+ connection ,
8546 csvFilePath ,
8647 createRandomRecordDto . tableName ,
8748 columnNames ,
8849 ) ;
8950
9051 await this . fileService . deleteFile ( csvFilePath ) ;
9152
92- await this . usageService . updateRowCount ( req ) ;
53+ await this . usageService . updateRowCount ( connection , sessionId ) ;
9354
9455 return new ResRecordDto ( {
9556 status : affectedRows === createRandomRecordDto . count ,
9657 text : `${ createRandomRecordDto . tableName } 에 랜덤 레코드 ${ affectedRows } 개 삽입되었습니다.` ,
9758 } ) ;
9859 }
9960
61+ private checkDomainAvailability ( mysqlType : string , targetDomain : string ) {
62+ const baseType = mysqlToJsType ( mysqlType ) ;
63+ const targetType = DomainToTypes [ targetDomain ] ;
64+ return ! ( baseType === 'number' && targetType === 'string' ) ;
65+ }
66+
10067 private toEntity ( randomColumnInfo : RandomColumnInfo ) : RandomColumnModel {
10168 let generator : RandomValueGenerator < any > ;
10269 if ( generalDomain . includes ( randomColumnInfo . type ) )
@@ -116,4 +83,40 @@ export class RecordService {
11683 blank : randomColumnInfo . blank ,
11784 } ;
11885 }
86+
87+ private async validateDto (
88+ createRandomRecordDto : CreateRandomRecordDto ,
89+ connection : Connection ,
90+ ) {
91+ const tableInfo = await this . tableService . find (
92+ connection ,
93+ createRandomRecordDto . tableName ,
94+ ) ;
95+
96+ if ( ! tableInfo ?. tableName )
97+ throw new BadRequestException (
98+ `${ createRandomRecordDto . tableName } 테이블이 존재하지 않습니다.` ,
99+ ) ;
100+
101+ const baseColumns = tableInfo . columns ;
102+ const columnInfos : RandomColumnInfo [ ] = createRandomRecordDto . columns ;
103+
104+ columnInfos . forEach ( ( columnInfo ) => {
105+ const targetName = columnInfo . name ;
106+ const targetDomain = columnInfo . type ;
107+ const baseColumn = baseColumns . find (
108+ ( column ) => column . name === columnInfo . name ,
109+ ) ;
110+
111+ if ( ! baseColumn )
112+ throw new BadRequestException (
113+ `${ targetName } 컬럼이 ${ createRandomRecordDto . tableName } 에 존재하지 않습니다.` ,
114+ ) ;
115+
116+ if ( ! this . checkDomainAvailability ( baseColumn . type , targetDomain ) )
117+ throw new BadRequestException (
118+ `${ targetName } (${ baseColumn . type } ) 컬럼에 ${ targetDomain } 랜덤 값을 넣을 수 없습니다.` ,
119+ ) ;
120+ } ) ;
121+ }
119122}
0 commit comments